Zookeeper
If the number on all digits of a is equal to B, A is called B's generator and calculates the minimum generator of the given number.
Given the number N is less than 100,000 considering all the digits and the maximum number of 99,999 digits and also only 45 so we only need to go from N-45 to N enumerations.
#include<cstdio> #include<cstring> using namespace std; int t, n, a, b, ans, l; int main() { scanf ("%d", &t); while (t--) { scanf ("%d", &n); ans = 0; for (int i = n-50; i < n; ++i) { a = b = i; while (b) { a += b % 10; b /= 10; } if (a + b == n) { ans = i; break; } } printf ("%d\n", ans); } return 0; }
For a positive integerN, The digit-sumNIs defined as the sumNItself and its digits. WhenMIs the digitsumN, We callNA generatorM.
For example, the digit-sum245Is256 (= more than 245 + 2 + 4 + 5). Therefore,245Is a generator256.
Not surprisingly, some numbers do not have any generators and some numbers have more than one generator. For example, the generators216Are198And207.
You are to write a program to find the smallest generator of the given integer.
Input
Your program is to read from standard input. The input consistsTTest cases. The number of test casesTIs given in the first line of the Input. Each test case takes one line containing an integerN, 1N100,000.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to contain a generatorNFor each test case. IfNHas multiple generators, print the smallest. IfNDoes not have any generators, print0.
The following shows sample input and output for three test cases.
Sample Input
3 216 121 2005
Sample output
198 0 1979
Ultraviolet A 1583 digit generator (Mathematics)