題目
Problem C - You can say 11
Time Limite: 1 second
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
相關資訊
2~11的倍數判別法+ 3和11倍數證明法
2的判別 : 尾數為偶數或0即是。
3的判別 : "各"位數加起來為3的倍數即是。
證明 : 設 x 為3的倍數 且 x = a b c d e
x = 10000 a + 1000 b + 100 c + 10 d + e
= (9999 a + a) + (999 b + b) + (99 c + c) + (9 d + d) + e
= (9999 a + 999 b + 99 c + 9 d) +( a + b + c + d + e )
∵ 9999 a + 999 b + 99 c + 9 d 為 3 的倍數
∴ a + b + c + d + e 必為 3 的倍數
4的判別 : 末兩位為4的倍數即是。
5的判別 : 末位為5或0即是。
6的判別 : "2且3"的倍數即是。 (因為6=2×3)
7的判別 : 3位一節,相減為7的倍數即是。
8的判別 : 末3位為8的倍數即是。
9的判別 : "各"位數相加為9的倍數即是。(道理同3的倍數)
10的判別 : 末位為0即是。
11的判別 : "奇"位相加和"偶"位相加 相差為11的倍數即是。
證明 : 設 y 為11的倍數 且 y = a b c d
y = 1000 a + 100 b + 10c + d
= ( 1001 - 1 ) a + ( 99 + 1 ) b + ( 11 - 1 ) c + d
=( 1001 a + 99 b +11 c ) + ( - a + b - c + d )
∵1001 a + 99 b +11 c為 11 的倍數
∴ ( - a + b - c + d )必為11的倍數
資訊來源
http://tw.group.knowledge.yahoo.com/math-etm/article/view?aid=52
答案
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
char num[1000];
int i=0;
while(scanf("%s",num)!=EOF)
{
if(num[0]=='0' && num[1]=='\0')
break;
int first=0,second=0,n=strlen(num);
for(i=0;i<n;i++)
{
if( i%2 == 0 )
first += (num[i]-48);
else
second += (num[i]-48);
}
if((first-second)%11 == 0)
printf("%s is a multiple of 11.\n",num);
else
printf("%s is not a multiple of 11.\n",num);
}
return 0;
}
/*wihile判斷錯誤一直造成wrong answer
原本:while(scanf("%s",num)!=EOF && (num[0]!='0' && num[1]!='\0'))
應該改成:while(scanf("%s",num)!=EOF && !(num[0]=='0' && num[1]=='\0'))
*/