UVa1583

题意:某个数加上该数的各个位数的和等于另一个数,那么这个数就是另一个数的生成元。求生成元,不存在则输出0;

KEY:打表,减少运算;

/*打表,一般不是很复杂的数学题试试打表找到规律*/
#include <iostream>
#include <stdio.h>
#include <string.h>
const int maxn = 100000 + 5;
int ans[maxn];

using namespace std;

int main()
{
    int t, n;
    memset(ans, 0, sizeof(ans));    
    for(int i = 1; i < maxn; i++){
        int x = i;
        int y = i;
        while(x > 0){
            y += x % 10;
            x /= 10;
        }
        if(ans[y] == 0)
            ans[y] = i;
    }
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        printf("%d
", ans[n]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Joe962850924/p/4297398.html