//如果x和x各位的和等于y,那么x是y的生成元。给出n,求最小生成元
#include <stdio.h>
#include <stdlib.h>
int dightsum(int number)
{
int sum=0;
while(number){
sum+=number%10;
number/=10;
}
return sum;
}
int main()
{
int n;
int times;//测试次数
int flag;
scanf("%d",×);
while(times--){
scanf("%d",&n);
flag=0;
for(int i=n-45;i<=n-1;i++){//分析可知,100000以内的数,各位数字和不超过45
if(i+dightsum(i)==n){
printf("%d\n",i);
flag=1;
break;
}
}
if(flag==0)
printf("0\n",n);
}
return 0;
}
/*
打表
// UVa1583(LA3355) Digit Generator
// Rujia Liu
#include<stdio.h>
#include<string.h>
#define maxn 100005
int ans[maxn];
int main() {
int T, n;
memset(ans, 0, sizeof(ans));
for(int m = 1; m < maxn; m++) {
int x = m, y = m;
while(x > 0) { y += x % 10; x /= 10; }
if(ans[y] == 0 || m < ans[y]) ans[y] = m;
}
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
}
return 0;
}
*/
UVa 1583 Dighter Generator(生成元)
最新推荐文章于 2019-08-28 16:57:36 发布