For a positive integer n, the digit-sum of n is defined as the sum of n itself and its digits. When m was the Digitsumof 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 does not has any generators and some numbers has more than one generator. For example, the generators of 216 is 198 and 207.
You is to write a program to find the smallest generator of the given integer.
Input
Your program was to read from standard input. The input consists of T test Cases. The number of test cases T Isgiven in the first line of the input. Each test case is takes one line containing an integer n, 1≤ n ≤100,000.
Output
Your program is-to-write to standard output. Print exactly one line for each test case. The line was to contain agenerator of N for each test case. If N has multiple generators, print the smallest. If N does not has anygenerators, print 0.
The following shows sample input and output for three test cases.
Sample Input
32161212005
Output for the Sample Input
19801979
The requirement is to find its smallest generator based on the number of inputs, I started with an enumeration, but too much, and wanted to use judgment to narrow the scope of the enumeration. Set the number of inputs num, when num=99999 the maximum and the number of bits is 45, that is, as long as the [Num-45,num] This interval enumeration, it can be relatively easy to get answers. The following answers are written in C:
#include <stdio.h>int main () {int ans=0,turns,num,i;scanf ("%d", &turns), while (turns>0) {scanf ("%d", &num); for (i=num-45;i<num;i++) {if ((i+i%10000/1000+i%1000/100+i%100/10+i%10) ==num) {ans=i; break;}} if (!ans) printf ("No answers\n"), Else printf ("%d\n", ans); turns--;ans=0;} return 0;}
This article is from the "9938713" blog, please be sure to keep this source http://9948713.blog.51cto.com/9938713/1696484
Digit Generator, Acm/icpc Seoul 2005, UVa1583