Digit Generator (water)

Source: Internet
Author: User

Title Link: http://acm.tju.edu.cn/toj/showp2502.html 2502. Digit Generator Time limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2426 Accepted Runs: 1579



For a positive integer N, the digit-sum of Nis defined as the sum of Nitself and its digits. When Mis the digitsum of N, we call NA GeneratorOf 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 is given on 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 a generator of N for each test case. If N has multiple generators, print the smallest. If N does not has any generators, print 0.

The following shows sample input and output for three test cases.

Sample Input
32161212005
Output for the Sample Input
19801979
The array applies the water question directly to the code:
1#include <cstdio>2#include <cstring>3#include <string>4 using namespacestd;5 intMain ()6 {7     Charnum[ One];8     intT;9scanf"%d",&T);Ten      while(t--) One     { Ascanf"%s", num); -         intTM =9*strlen (num); -         intSum =num[0]-'0'; the          for(inti =1; i < strlen (num); i++) -         { -sum*=Ten; -sum+= (num[i]-'0'); +         } -         inttt = SUM-TM; +         inti; A          for(i = TT; i < sum; i++) at         { -             intGA =i; -             intFlag =0; -              while(Ga >0) -             { -flag+=ga%Ten; inGA = ga/Ten; -             } to             if((i+flag) = =sum) +             { -printf"%d\n", i); the                  Break; *             } $         }Panax Notoginseng         if(I==sum) puts ("0"); -     } the     return 0 ; +}


Digit Generator (water)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.