/*编写一个程序计算100以内的smith数,smith数的概念:
一个非素数,其各位数之和等于其所有质因数的个位数之和。
例如:4=2*2,4=2+2
22=2*11 2+2=2+1+1
27=3*3*3 2+7=3+3+3
*/
#include <iostream>
using namespace std;
int m=0;//定义全局变量
int a[100],b[10];
bool verdict(int n) //判断是否是质因数
{
if(n==2)
{
return 1;
}
else if(n>2)
{
for(int j=2;j<n;j++)
{
if(n%j==0)
return 0;
}
}
return 1;
}
void coutzys(int n,int a[]) //计算出n以内的质数
{
for(int i=2;i<n;i++)
{
if(verdict(i))
{
a[m]=i;
m++;
}
}
}
int temp1=0;
void coutadd(int n,int b[],int t) //对满足条件的n进行各位数求和
{
if(n/10!=0)
{
b[t]=n%10;
temp1+=b[t];
coutadd(n/10,b,t+1);
}
else
{
b[t]=n%10;
temp1+=b[t];
}
}
int temp=0;
int smith(int n,int t,int n1)
{
if(n>1&&t<m)
{
if(n%a[t]==0)
{
if(n/a[t]!=1)
{
if(verdict(n/a[t]))
{
if(n/a[t]/10!=0)
{
temp+=a[t]+n/a[t]/10+n/a[t]%10;
}
else
{
temp+=a[t]+n/a[t];
}
return temp;
}
else
{
temp=a[t]+temp;
return smith(n/a[t],0,n1);
}
}
}
smith(n,t+1,n1);
}
return 0;
}
void main()
{
coutzys(100,a);
for(int k=2;k<100;k++)
{
coutadd(k,b,0);
smith(k,0,k);
if(temp==temp1)
cout<<k<<endl;
temp=0;
temp1=0;
}
}
一个非素数,其各位数之和等于其所有质因数的个位数之和。
例如:4=2*2,4=2+2
22=2*11 2+2=2+1+1
27=3*3*3 2+7=3+3+3
*/
#include <iostream>
using namespace std;
int m=0;//定义全局变量
int a[100],b[10];
bool verdict(int n) //判断是否是质因数
{
if(n==2)
{
return 1;
}
else if(n>2)
{
for(int j=2;j<n;j++)
{
if(n%j==0)
return 0;
}
}
return 1;
}
void coutzys(int n,int a[]) //计算出n以内的质数
{
for(int i=2;i<n;i++)
{
if(verdict(i))
{
a[m]=i;
m++;
}
}
}
int temp1=0;
void coutadd(int n,int b[],int t) //对满足条件的n进行各位数求和
{
if(n/10!=0)
{
b[t]=n%10;
temp1+=b[t];
coutadd(n/10,b,t+1);
}
else
{
b[t]=n%10;
temp1+=b[t];
}
}
int temp=0;
int smith(int n,int t,int n1)
{
if(n>1&&t<m)
{
if(n%a[t]==0)
{
if(n/a[t]!=1)
{
if(verdict(n/a[t]))
{
if(n/a[t]/10!=0)
{
temp+=a[t]+n/a[t]/10+n/a[t]%10;
}
else
{
temp+=a[t]+n/a[t];
}
return temp;
}
else
{
temp=a[t]+temp;
return smith(n/a[t],0,n1);
}
}
}
smith(n,t+1,n1);
}
return 0;
}
void main()
{
coutzys(100,a);
for(int k=2;k<100;k++)
{
coutadd(k,b,0);
smith(k,0,k);
if(temp==temp1)
cout<<k<<endl;
temp=0;
temp1=0;
}
}