11879 - Multiple of 17

Theorem: If you drop the last digit d of an integer n (n$ \ge$10), subtract 5d from the remaining integer, then the difference is a multiple of 17 if and only if n is a multiple of 17.

For example, 34 is a multiple of 17, because 3-20=-17 is a multiple of 17; 201 is not a multiple of 17, because 20-5=15 is not a multiple of 17.

Given a positive integer n, your task is to determine whether it is a multiple of 17.

Input 

There will be at most 10 test cases, each containing a single line with an integer n ( 1$ \le$n$ \le$10100). The input terminates with n = 0, which should not be processed.

Output 

For each case, print 1 if the corresponding integer is a multiple of 17, print 0 otherwise.

定理:输入一个数,移除一个大于等于10的整数n 的最后一个位数d,其值再减去5d之后,若为17的倍数,则n 亦为17的倍数。例如:34为17的倍数,因为3-20=-17为17的倍数;201非17的倍数,因为20-5=15非17的倍数。给定一正整数n,请你判断n 是否为17的倍数。

Sample Input 

34
201
2098765413
1717171717171717171717171717171717171717171717171718
0

Sample Output 

1
0
1
0

 解题思路:先判断除开最后一位前几位是否为17倍数,余数再减去最后一位的5倍来判断是否为17倍数

#include<stdio.h>
#include<string.h>
int main()
{char a[1000];
int b[1000],i,flag,n;
while(scanf("%s",a) && a[0]!='0'){
                                flag=0;
                                n=strlen(a);
                                for(i=0;i<n;i++)
                                {b[i]=a[i]-'0';}
                                for(i=0;i<n-1;i++){
                                                   flag=flag*10+b[i];
                                                   flag=flag%17;
                                                   }
                                flag=(flag-b[n-1]*5)%17;
                                if(flag) printf("0\n");
                                else printf("1\n");
                                }
return 0;
}

 

posted on 2013-02-22 20:03  喂喂还债啦  阅读(414)  评论(0编辑  收藏  举报