概述
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
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) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.
Output
For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.
Sample Input
6
101 101
0 67
-101 101
7678123668327637674887634 101
11010000000000000000 256
-202202202202000202202202 -101
Sample Output
Case 1: divisible
Case 2: divisible
Case 3: divisible
Case 4: not divisible
Case 5: divisible
Case 6: divisible
题意:
给你俩个数 a , b .如果 b 可以被 a 整除就输出 divisible , 否则输出 not divisible
这里用到大数取余 和 同于定理的应用
一开始有在纠结 , 为什么要sum = sum * 10 + s[i] 然后 sum = sum % b。 这样不会改变答案吗
但是例如:
157 % 7 = ((1 * 10 + 5 )*10 + 7 )% 7 ,这样就可以看出来同于定理的应用啦~
下面代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
ll b , sum;
char s[200];
cin >> t;
for(int i = 1 ; i <= t ; i++)
{
sum = 0;
scanf("%s %lld" , &s , &b);
int len = strlen(s);
if(b < 0) b = -b;
printf("Case %d: " , i);
if(len == 1 && s[0] == '0')
{
printf("divisiblen");
continue;
}
for(int j = 0 ; j < len ; j++)
{
if(s[j] == '-')
continue;
sum = sum * 10 + (s[j]-'0');
sum = sum % b;
}
if(sum == 0)
{
printf("divisiblen");
}
else
{
printf("not divisiblen");
}
}
return 0;
}
最后
以上就是懵懂音响为你收集整理的Large Division LightOJ - 1214的全部内容,希望文章能够帮你解决Large Division LightOJ - 1214所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复