UVa 10929 You can say 11 (数论)
来源:互联网 发布:手机货币兑换软件 编辑:程序博客网 时间:2024/05/29 08:42
10929 - You can say 11
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1870
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.
方法1:直接从个位数开始mod 11计算。
/*0.026s*/#include<cstdio>#include<cstring>char s[1005];int main(void){int remainder;while (gets(s), strcmp(s, "0")){remainder = 0;for (int i = 0; s[i]; i++)remainder = (remainder * 10 + (s[i] & 15)) % 11;if (remainder) printf("%s is not a multiple of 11.\n", s);else printf("%s is a multiple of 11.\n", s);}return 0;}
方法2:若一个整数的奇位数字之和与偶位数字之和的差能被11整除,则这个数能被11整除。
证明:比如num = 10000a4+1000a3+100a2+10a1+a0=(a4+a2+a0-a3-a1)+(9999a4+99a2+1001a3+11a1)
因为11 | 99,所以11 | (9900+99)
因为11 | 1111,所以11 | (1111-110)
所以...
/*0.025s*/#include<cstdio>#include<cstring>char s[1005];int main(void){int l, i, sum;while (gets(s), strcmp(s, "0")){l = strlen(s), sum = 0;for (i = 0; i < l; i += 2) sum += s[i] & 15;for (i = 1; i < l; i += 2) sum -= s[i] & 15;if (sum % 11) printf("%s is not a multiple of 11.\n", s);else printf("%s is a multiple of 11.\n", s);}return 0;}
- UVa 10929 You can say 11 (数论)
- UVa OJ 10929-You can say 11
- UVA 10929 - You can say 11
- UVa 10929 - You can say 11
- UVa -- 10929 You can say 11
- uva 10929 - You can say 11
- UVa 10929 - You can say 11
- COJ 1102 - You Can Say 11 题解
- Say You, Say Me
- Say you say me
- UVa 10642 - Can You Solve It?
- hdu 5478 Can you find it(数论)
- i can say ABC(2006-7-11)
- You don't say!
- what can i say?
- What can I say
- what can i say.
- what can i say
- SqlServer 使用小技巧
- alibaba2014
- Android和FTP服务器交互,上传下载文件(实例demo)
- paip.Adblock屏蔽onlinedown华军软件园的4秒下载广告总结..
- Visual C#命名空间详解
- UVa 10929 You can say 11 (数论)
- java匿名内部类
- Stanford Algorithms学习:Clustering 2
- JMS 实例讲解
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解
- 程序员,我要为了这个名号而疯狂
- 单文档mfc添加树形控件
- C# 连接数据库1
- 华为2014届机试题之田忌赛马