FC数学模拟-L2-029 特立独行的幸福
作者:
小花猪
,
2023-04-17 15:12:35
,
所有人可见
,
阅读 121
L2-029 特立独行的幸福
#include <iostream>
#include <cstring>
#include <map>
#include <cmath>
using namespace std;
const int N = 1e5+10;
map<int, int> ans, tans;
bool st[N];
int a, b;
int get(int n){//迭代
int res = 0;
while(n){
res += pow((n%10), 2);
n /= 10;
}
return res;
}
bool isprimes(int n){//判断质数
if(n < 2) return false;
for(int i=2; i<=n/i; i++){
if(n % i == 0) return false;
}
return true;
}
int check(int u){//求数u的迭代过程并记录中间的数
map<int ,int> m;
int cnt = 0;
int t = u;
while(t != 1){
m[t] = 1;
t = get(t);
tans[t] = 1;//记录依赖于u的数(不管是不是幸福数)
if(m.count(t)) return 0;//迭代回到本身
cnt ++;
}
return cnt;//迭代次数
}
int main(){
cin>>a>>b;
for(int i=a; i<=b; i++){
int cnt = check(i);
if(cnt) ans[i] = cnt;//幸福数
}
for(auto i : ans){
if(tans[i.first] != 0) continue;
if(isprimes(i.first)) i.second *= 2;
cout<<i.first<<" "<<i.second<<endl;
}
if(!ans.size()) cout<<"SAD"<<endl;
return 0;
}