天天看點

1583—Digit Generator

For a positiveinteger N , the digit-sum of N is defined as the sum of Nitself and its digits. When M is the digitsum of N , we call N a generator ofM .

For example, thedigit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256.

Not surprisingly,some numbers do not have any generators and some numbers have more than onegenerator. For example, the generators of 216 are 198 and207.

You are to write aprogram to find the smallest generator of the given integer.

Input 

Your program is toread from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input.Each test case takes one line containing an integer N , 1N100, 000 .

Output 

Your program is towrite to standard output. Print exactly one line for each test case. The lineis to contain a generator of N for each testcase. If Nhas multiplegenerators, print the smallest. If N does not have anygenerators, print 0.

The followingshows sample input and output for three test cases.

Sample Input 

3

216

121

2005

Sample Output 

198

1979

代碼:

#include<iostream>

#include<memory.h>//清零下面的數組

using namespace std;

const intMAX=100100;//開的大一點防止越界

int arr[MAX];

int main()

{

    int test;

    cin>>test;

    memset(arr,0,sizeof(arr));

    for(int i=1;i<MAX;i++)//數組越界一點不會有錯誤,越界的部分用不到

    {

        int x=i,y=i;

        while(x>0)

        {

            y=y+x%10;

            x=x/10;

        }//實作了求X加上X的個位數字的和的功能

        if(arr[y]==0)//未被初始化

        {

            arr[y]=i;

        }

    }

    while(test--)

    {

        int num;

        cin>>num;

        cout<<arr[num]<<endl;

    }

}