UVa 1583

From Algorithmist
Jump to: navigation, search

1583 - Digit Generator

Summary

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-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 one generator. For example, the generators of 216 are 198 and 207. You are to write a program to find the smallest generator of the given integer.

Explanation

It's a very easy problem. You have to check just back 100 numbers of N. Suppose, we have 216 then we have to iterate from 216-100=116. You have to take a for loop from 116 to 215, then you will get your ans.


Notes

  • You can get TLE if you iterate more numbers. For example: N=216, You start iterate from 100 then you will get TLE.

Implementations

Normally check behind 100 numbers of N and check every result is equeal to N or not. If equeal then break and print the iterate value.

Input

3 
216 
121 
2005

Output

198 
0 
1979