For a positive integer n, if its sum equals the sum of all of its factorization, then that number is called the Smith number. For example, the sum of 31257=3*3*23*151,31257 's figures is 3+1+2+5+7=18, and the sum of all its factorization figures is 3+3+2+3+1+5+1=18, so 31257 is a Smith number. Write a program to determine if the positive integer input is a smith number.
Input Description: There are multiple sets of data, each group of data has only one integer n (<100000, a row), 0 indicates the end of the input.
Output Description: For each set of data, output a Yes or no (indicates whether the number is Smith).
Input Sample:
31257
123
0
Output Sample:
Yes
No
#include <iostream> #include <vector> #include <string>using namespace std;int sum (int n)//sum of numbers {int Sum;for (sum=0;n;n/=10) Sum+=n%10;return sum;} int main () {vector<string> s;int n,i;int t1,t2,t=0;cin>>n;while (n) {int j=n;t1=sum (n); for (i=2;i<n;) {if (n%i==0) {t=t+sum (i); n=n/i;} else i++;} T2=t+sum (n), if (t1==t2&&n<j) s.push_back ("yes"), Else S.push_back ("no"); t=0;cin>>n;} for (int k=0;k<s.size (); k++) Cout<<s[k]<<endl;return 0;}
Huawei Machine Test-smith number