UVA10929 - You can say 11

第一篇结题报告,献给我让我今天不用吃蛋的水题:-D

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Introduction to the problem Your job is, given a positive number N, determine if it is a multiple of eleven.
Description of the input The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits.
Description of the output The output of the program shall indicate, for each input number, if it is a multiple of eleven or not.
Sample input: 112233 30800 2937 323455693 5038297 112234 0
Sample output 112233 is a multiple of 11. 30800 is a multiple of 11. 2937 is a multiple of 11. 323455693 is a multiple of 11. 5038297 is a multiple of 11. 112234 is not a multiple of 11.

 

题意:给你若干个1000位以内的数(字符串“0”为输入结束标志),判断是否为11的倍数。

脑残思路(可无视):从字符串的最高位开始不断减去11,若最高位为0则舍去(如:08999->8999),最终只剩2位时,比较这2位是否相等。

吐槽: 由于所给的时间比较宽松(3000ms),这题就这么被我水过了,但最终judge用时1106 ms,其他人都是30+ms 囧RZ。

上代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 char s[1005];
 4 bool caculat()
 5 {
 6     int n,m;
 7     while((n=strlen(s))>2)
 8     {
 9         int temp=0,m=--n;
10         if(s[0]!='0')
11         {
12             if(s[1]>'0')
13             {
14                 s[0]--;
15                 s[1]--;
16             }
17             else if(s[0]>'1')
18             {
19                 s[0]-=2;
20                 s[1]='9';
21             }
22             else if(s[2]>='1')
23             {
24                 s[0]='0';
25                 s[1]='9';
26                 s[2]--;
27             }
28             else
29             {
30                 s[0]='0';
31                 s[1]='8';
32                 s[2]='9';
33             }
34         }
35         while(s[0]=='0')
36         {
37             for(int j=0; j<m; j++)s[j]=s[j+1];
38             s[m]='\0';
39         }
40        //printf("%s\n",s);
41     }
42 
43 
44     if(s[0]==s[1])return true;
45     else return false;
46 }
47 int main()
48 {
49     char temp[1005];
50     //freopen("in.txt","r",stdin);
51     while(scanf("%s",s)&&strcmp(s,"0"))
52     {
53         strcpy(temp,s);
54         if(caculat())printf("%s is a multiple of 11.\n",temp);
55         else printf("%s is not a multiple of 11.\n",temp);
56     }
57 }
View Code

正解思路:大数求模的方法,不断对最高位加上原来的数乘10取模11保存(如:121->1%11=1,(1*10+2)%11=1,(1* 10+1)%11=0),若最终结果为0则为11的倍数。

              用时22ms

上代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 int main()
 4 {
 5     char s[1005];
 6     int n;
 7     //freopen("in.txt","r",stdin);
 8     while(scanf("%s",s)&&strcmp(s,"0"))
 9     {
10         int m=0,i=0;
11         while(s[i]!='\0')
12         {
13             m=(10*m+(s[i++]-'0'))%11;
14         }
15         if(!m)printf("%s is a multiple of 11.\n",s);
16         else printf("%s is not a multiple of 11.\n",s);
17 
18     }
19 }
View Code

 

 

posted @ 2013-08-08 21:58  code_farmer_cyk  阅读(346)  评论(0编辑  收藏  举报