如果x加上x的各个数字之和得到y,就说x是y的生成元。给出n(1=<n<=100000),求最小生成元。无解输出0。例如,n=216,121 ,2005时的解分别为:198,0,1979.
源代码
#include <iostream>
#include<cstring>
using namespace std;
int a[100010];
int main()
{
memset(a,0,sizeof(a)); //只能是0,别的会是乱码
for(int x=1;x<=99999;x++)
{
int c1=x;
int tmp=x;
while(tmp!=0)
{
c1=c1+tmp%10;
tmp=tmp/10;
}
if(c1<=10000)
{
if(a[c1]==0||x<a[c1])
a[c1]=x;
}
}
int n;
cin>>n;
cout<<a[n]<<endl;
return 0;
}
思路:数组号是数,而数组内容是序号