题目详情 - L2-029 特立独行的幸福 (pintia.cn)
模拟题。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 10010;
bool book[N],st[N];
vector<int> ans;
int num[N];
bool is_prime(int x){
for(int i = 2; i*i <= x; i++){
if(x % i == 0) return false;
}
return true;
}
int cmp(int x){
int res = 0;
while(x){
res += (x%10) * (x%10);
x /= 10;
}
return res;
}
int main(){
int a,b; cin>>a>>b;
for(int i = a; i <= b; i++){
int t = i, cnt = 0;
memset(book,0,sizeof book);
while(t != 1 && !book[t]){//book用来判断会不会出现死循环
cnt++;
book[t] = true;
t = cmp(t);
st[t] = true;//st用来标记i迭代过程中出现的数,用来判断是否在区间[a,b]中独立
}
if(t == 1){
ans.push_back(i);
num[i] = cnt;
}
}
bool flag = false;
for(int i = 0; i < ans.size(); i++){
int tmp = ans[i];
if(!st[tmp]){
flag = true;
cout<<ans[i]<<' ';
if(is_prime(tmp)) cout<<num[tmp]*2;
else cout<<num[tmp];
cout<<endl;
}
}
if(!flag) puts("SAD");
return 0;
}