Ultraviolet A 1583 digit generator (Mathematics)

Source: Internet
Author: User
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)

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.