• 紫书第三章训练 UVA 1583 Digit Generator by 16 BobHuang


    For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum ofN , 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 do not have any generators and some numbers have more than one generator. For example, the generators of 216 are 198 and 207.

    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 consists of T test cases. The number of test cases T is given in the first line of the input. Each test case takes one line containing an integer N , 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 generator of N for each test case. If N has multiple generators, print the smallest. If N does not have 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
    这道题题意也很简单啦,就是找一个比n小的数,这个数和它的每一位相加的和等于n,不就是暴力穷举么,不过从哪一位开始需要考虑下,一个数字最大9,它的每一位都是9
    的话,也就是9*n的位数,这个就是你穷举的开始,然后直接暴力一下就好的。
     1 #include<stdio.h>
     2 int main()
     3 {   int t;
     4     scanf ("%d", &t);
     5     while (t--)
     6     {   int n;
     7         scanf ("%d", &n);
     8         int ans = 0;
     9         int a=n,w=0;
    10         while(a){
    11             w++;
    12             a/=10;
    13         }
    14         for (int i = n-w*9; i < n; i++)
    15         {
    16             int a =i, b = i;
    17             while (b)
    18             {
    19                 a += b % 10;
    20                 b /= 10;
    21             }
    22             if (a == n)
    23             {
    24                 ans = i;
    25                 break;
    26             }
    27         }
    28         printf ("%d
    ", ans);
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    学生信息录入系统
    作业11(增删改查listview)
    作业10(qq增删改查)
    作业9
    作业8
    作业7
    右键打开Windows Terminal
    axure 动态面板中滚动条
    TortoiseSVN服务端的配置与使用
    hfs使用小技巧
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6801456.html
Copyright © 2020-2023  润新知