码迷,mamicode.com
首页 > 其他好文 > 详细

Digit Generator UVa1583

时间:2018-08-02 15:58:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:include   return   src   http   std   情况   generator   \n   clu   

技术分享图片

code:

 1 #include<stdio.h>
 2 
 3 //count the number of the digit.
 4 int count_digits(int a)
 5 {
 6     int count = 0;
 7     while(a)
 8     {
 9         a /= 10;
10         count++;
11     }
12     return count;
13 }
14 
15 //the function to be used to judge the digit
16 /*
17   yes : return the generator
18   no  : retur 0.
19 */
20 int judgement(int a)
21 {
22     int i = 0;
23     int count = count_digits(a);
24     for(i = a - count*9; i < a; i++)
25     {
26         int sum = i;
27         int temp = i;
28         while(temp > 0)
29         {            
30             sum += temp%10;
31             temp /= 10;
32         }
33         if(sum == a) return i;
34     }
35     return 0;
36 }
37 
38 int main()
39 {
40     int T, a;
41     scanf("%d",&T);
42     int i = 0;
43     for(; i < T; i++)
44     {
45         scanf("%d",&a);
46         printf("%d\n",judgement(a));
47     }
48     return 0;
49 
50 }

ps:

  1. 刚开始写的时候出现了一点问题,出现了死循环,在这种情况下应该冷静地插入多个标记来看是哪里发生了死循环,并且尽快更正。

Digit Generator UVa1583

标签:include   return   src   http   std   情况   generator   \n   clu   

原文地址:https://www.cnblogs.com/dreamworldclark/p/9407313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!