UVA 1583 B-digit Generator (violence)

Source: Internet
Author: User

B-digit GeneratorTime limit:3000MS Memory Limit:0KB 64bit IO Format:%lld &%llu SubmitStatus

Description

For a positive integer n<tex2html_verbatim_mark>, the digit-sum of n<tex2html_ Verbatim_mark> is defined as the sum of N<tex2html_verbatim_mark> itself and its digits. When M<tex2html_verbatim_mark> is the digitsum of N<tex2html_verbatim_mark>, we Call N<tex2html_verbatim_mark> agenerator of M<tex2html_verbatim_ mark>.

For example, the digit-sum of 245 is (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of the.

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<tex2html_verbatim_mark> test Cases. The number of test cases T <tex2html_verbatim_mark> is given in the first line of the input. Each test case takes one line containing an integer n<tex2html_verbatim_mark>, 1n100, 000<tex2html_verbatim_mark>.

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<tex2html_verbatim_mark> for each test case. If N<tex2html_verbatim_mark> has multiple generators, print the smallest. If N<tex2html_verbatim_mark> does not has any generators, print 0.

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

Sample Input

3 216 121 2005

Sample Output

198 0 1979

Starting from 1 The enumeration, the tle.
Think of it a little bit and you'll find out. Maximum of 6 digits. So the target number and n are the most bad 6*9==54
So you can enumerate directly from n-54 to N.
1 /*************************************************************************2 > File name:code/uva/1583.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 September 15 Tuesday 16:47 20 seconds6  ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + #defineY1 HUST111QQZ A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - #defineLR DYING111QQZ - using namespacestd; - #definefor (i, n) for (int i=0;i<int (n); ++i) -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to intN; + BOOLJudgeintXinty) - { the     intR; *     intsum =0; $     intTMP = yx;Panax Notoginseng      while(x) -     { the             +r = x%Ten; Ax = x/Ten; thesum = sum +R; +     } -      $     if(sum==tmp)return true; $     return false; - } - intMain () the { - #ifndef Online_judgeWuyiFreopen ("In.txt","R", stdin);  the   #endif -     intT; WuCin>>T; -      while(t--) About     { $scanf"%d",&n); -  -     intAns =0 ; -     intST = n Wu; A      for(inti = st; I <= N; i++) +     { the         if(judge (i,n)) -         { $Ans =i; the          Break; the         } the     } theprintf"%d\n", ans); -     } in    the    the #ifndef Online_judge About fclose (stdin); the   #endif the     return 0; the}
View Code

UVa 1583 B-digit Generator (violence)

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.