题目:判断一个数知否能被11整除。
方法:如果一个数的奇数位上的和与偶数位上的和的差可以被11整除,则说明是multiple of 11。
你的任務是,給你一個正整數 N,判定它是否是 11 的倍數。
Input
每列資料有一個正整數N,N 最大可能到 1000 位數。
若 N = 0 代表輸入結束。
Output
對每個輸入的數,輸出是否為 11 的倍數。輸出格式請參考 Sample Output。
Sample Input | Sample Output |
112233 30800 2937 323455693 5038297 112234 0 | 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. |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
while(1)
{
char arr[1001]={0}, str[1001]={0};
gets(arr);
if(arr[0]-'0'==0 || arr[0]==' ' && arr[1]-'0'==0) break; //出现"0"或者" 0"时终止
int i, j=0, odd=0, even=0;
for(i=0; i<strlen(arr); i++)
if(arr[i]>='0' && arr[i]<='9') str[j++]=arr[i]; //将数字放入一个新的阵列
for(i=0; i<strlen(str); i++)
{
if(i%2==1) odd+=str[i]-'0';
else even+=str[i]-'0';
}
printf("%s is ",str);
if(abs(odd-even)%11!=0) printf("not ");
printf("a multiple of 11.\n");
}
}