章三例题5——UVA 1583 Degit Generator

来源:互联网 发布:电气专业软件 编辑:程序博客网 时间:2024/06/14 21:26

题意就是如果x加上x的各个数字的和得到y,则说x是y的生成元,然后告诉y,求x的最小生成元。
然后因为n比较小,然后只用输入一组,直接枚举是可以过的。

据说可以打表,现在还没有接触到

http://blog.csdn.net/a27038/article/details/55048971

代码

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){    int n;    scanf("%d",&n);    while(n--)    {        int N;        scanf("%d",&N);        int len=1;        int flag=0;        int temp=N;        while(temp)        {            if(temp/10!=0)                len++;            temp=temp/10;        }        int j=0;        for(int i=N-len*10;i<N;i++)        {            j=i;            int g=i;            while(g)            {                j+=g%10;                g=g/10;            }            if(j==N)            {                flag=1;                cout<<i<<endl;                break;            }        }        if(flag==0)            printf("0\n");    }    return 0;}
0 0