http://acm.hdu.edu.cn/showproblem.php?pid=1333
题意:定义smith数为其分解质因数后所有质因数的数根和与其本身的数根和相等的数。
给出一个数,要求输出比该数大的smith数。(一开始因为没看清要输出比它大的数,而输出了大于等于该数的smith数,WA了几次)
记录一下自己求质因数的方法吧=w=
要记住一个数的质因数有且仅有一个可能大于它本身的开方。
#include <iostream>
#include <cmath>
using namespace std;
int getroot(int n){
int root=0;
while (n){
root+=n%10;
n=n/10;
}
return root;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1333in.txt","r",stdin);
#endif
int n,k,self_root,prim_root,i;
bool isprime;
while (scanf("%d",&n)!=EOF&&n){
while (true){
n++;
k=n;
self_root=getroot(k);
prim_root=0;
isprime=true;
for (i=2;i<=sqrt((double)k);i++)
if (k%i==0){
isprime=false;
prim_root+=getroot(i);
k=k/i;
i=1;
}
prim_root+=getroot(k);
if (!isprime&&prim_root==self_root){
printf("%d\n",n);
break;
}
}
}
return 0;
}