zoukankan      html  css  js  c++  java
  • 紫书第三章训练 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 }
  • 相关阅读:
    新学期随笔——脚踏实地
    买书方案
    课程总结和建议
    梦断代码阅读笔记03
    梦断代码阅读笔记02
    构建之法阅读笔记06
    【洛谷5284】[十二省联考2019] 字符串问题(后缀树优化建边)
    【BZOJ3514】Codechef MARCH14 GERALD07加强版(LCT_主席树)
    【BZOJ1487】[HNOI2009]无归岛(仙人掌 DP)
    【洛谷3239_BZOJ4008】[HNOI2015] 亚瑟王(期望 DP)
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6801456.html
Copyright © 2011-2022 走看看