Large Division 判断是否整除

题目:

Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c.

输入:

Input starts with an integer T (≤ 525), denoting the number of test cases.

Each case starts with a line containing two integers a (-10200 ≤ a ≤ 10200这里是10的200次方) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.

输出:

For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.

样例输入:

6

101 101

0 67

-101 101

7678123668327637674887634 101

11010000000000000000 256

-202202202202000202202202 -101

样例输出:

Case 1: divisible

Case 2: divisible

Case 3: divisible

Case 4: not divisible

Case 5: divisible

Case 6: divisible

这个题a的范围是10的20次方,所以用long long 的范围也不够,只能用数组求数。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
#define maxn 101000
int main()
{
    int t;
    ll b,c;
    int ans=1;
    char s[maxn];
    scanf("%d",&t);
    while(t--)
    {
        ll num=0;
        scanf("%s%lld",s,&b);
        int len=strlen(s);
//        if(b<0)//这里不写也AC了
//            b=-b;
            for(int i=0;i<len;i++)
            {
                if(s[i]=='-')
                    continue;
                num=(num*10+s[i]-'0')%b;//这里要好好解释一下,一开始的时候我想的是把num这个数求出来就好了,但是忘了我们之所以要用数组就是因为long long的范围不够,所以num的总数是求不出来的,所以就出现了这个格式,拿最后一个例子来说,-202202202202000202202202,第一个202可以整除101,第二个也能整除101.......,所以最后这个大数一定能整除101,最后num的值就是0
            }
     printf("Case %d: ",ans++);
        if(num==0)
            printf("divisible\n");
        else
            printf("not divisible\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhangjinlei321/article/details/81571393
今日推荐