首页 > 其他 > 详细

Digit Generator UVa1583

时间:2018-08-02 15:30:13      阅读:122      评论:0      收藏:0      [点我收藏+]

技术分享图片

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!