Multiple of 17(湖南省第六届ACM竞赛题)含有java大数模板

来源:互联网 发布:c语言输出字符串 编辑:程序博客网 时间:2024/05/29 10:31
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/C
Multiple of 17
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice UVA 11879

Description


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.

Sample Input

34201209876541317171717171717171717171717171717171717171717171717180

Sample Output

1010



Problemsetter: Rujia Liu, Special Thanks: Yiming Li


大数,可以直接用java写,后面还有一个用C写的。

javaAC代码:

import java.math.BigInteger;import java.util.*;import java.math.*;public class Main {public static void main(String args[])      {      Scanner sc = new Scanner(System.in);      while(true)      {      BigInteger a = sc.nextBigInteger();      if(a.equals(BigInteger.ZERO))      {      break;      }      BigInteger ans ;      BigInteger b = a.mod(BigInteger.valueOf(10));      a = a.divide(BigInteger.valueOf(10));      a = a.subtract(b.multiply(BigInteger.valueOf(5)));      a = a.abs();      ans = a.mod(BigInteger.valueOf(17));      if(ans.equals(BigInteger.ZERO))      {      System.out.println(1);      }      else      {      System.out.println(0);      }      }      }}

C代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int b[105];char a[105];int c[5];void jianfa(int lenb,int lenc){    for(int i=0;i<lenc;i++)    {        if(b[i]-c[i]<0)        {            b[i+1]--;            b[i]=10+b[i];        }        b[i]=b[i]-c[i];    }    for(int i=2;i<lenb-1;i++)    {        if(b[i]<0)        {            b[i+1]--;            b[i]=10+b[i];        }    }}int main(){    while(scanf("%s",&a)!=EOF)    {        if(a[0]=='0') break;        for(int i=0;i<105;i++)        {            if(i<5){b[i]=c[i]=0;}            else b[i]=0;        }        int lenb=strlen(a);        int t=lenb;        for(int i=0;i<lenb;i++)        {            b[--t]=a[i]-'0';        }        int i=0;        while(i<lenb-3)        {            t=b[0]*5;            int lenc=0;            if(t>=10)            {                c[lenc++]=t%10;                c[lenc++]=t/10;            }            else c[lenc++]=t;            for(int j=0;j<lenb-i;j++)                b[j]=b[j+1];            jianfa(lenb-i-1,lenc);            i++;        }        int k=1;        int ans=0;        for(int j=0;j<3;j++)        {            ans+=k*b[j];            k*=10;        }        if(ans%17) printf("0\n");        else printf("1\n");    }}


原创粉丝点击