zoukankan      html  css  js  c++  java
  • Digit Generator(水)

    题目链接: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 N is defined as the sum of N itself and its digits. When M is the digitsum of N, 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, 1 ≤ N ≤ 100,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
    

    Output for the Sample Input

    198
    0
    1979
    数组应用水题直接给代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 using namespace std;
     5 int main()
     6 {
     7     char num[11];
     8     int T;
     9     scanf("%d",&T);
    10     while(T--)
    11     {
    12         scanf("%s",num);
    13         int tm = 9*strlen(num);
    14         int sum =num[0]-'0';
    15         for(int i =1 ;i < strlen(num) ;i++)
    16         {
    17             sum*=10;
    18             sum+=(num[i]-'0');
    19         }
    20         int tt = sum - tm;
    21         int i;
    22         for( i = tt ; i < sum ; i++)
    23         {
    24             int ga = i;
    25             int flag = 0;
    26             while(ga > 0)
    27             {
    28                 flag+=ga%10;
    29                 ga = ga/10;
    30             }
    31             if((i+flag)==sum)
    32             {
    33                 printf("%d
    ",i);
    34                 break;
    35             }
    36         }
    37         if(i==sum) puts("0");
    38     }
    39     return 0 ;
    40 }

  • 相关阅读:
    ASP.NET MVC 异常捕获
    Jquery 扩展方法
    Spring.NET笔记1
    ASP.NET MVC Ninject 实现依赖注入
    ASP.NET MVC Unity实现依赖注入
    windows service
    反射用法
    抽象工厂核心反射
    (C#)中的DataSet、string、DataTable等对象转换成Json
    .NET批量删除代码前的行号
  • 原文地址:https://www.cnblogs.com/shanyr/p/4721595.html
Copyright © 2011-2022 走看看