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 }

  • 相关阅读:
    sql sever 的两种写法
    多站点IIS用户安全权限设置图解教程
    phpmyadmin“无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装。”报错解决办法
    天下无难事只怕有心人
    apache配置上传目录禁止运行php的方法
    C语言|博客作业03
    C语言|博客作业07
    C语言|博客作业05
    2019秋季第一周作业
    C语言|博客作业06
  • 原文地址:https://www.cnblogs.com/shanyr/p/4721595.html
Copyright © 2011-2022 走看看