版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16554583/article/details/84100180
原题链接:传送门
题意:给你两个数a和b,问你a是否能整除b。(-10200 ≤ a ≤ 10200),b是32位有符号整型。
思路:用一个数1234举例,可以写成 ( ( ( ( (1 * 10) + 2) * 10) + 3 ) * 10 ) + 4,而根据同余定理(m+n)%Mod = (m%Mod) + (n%Mod) ) % Mod,可以求出b是否可被a整除
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char str[210];
int main(){
int t,d,Case = 1;
scanf("%d\n", &t);
while(t--){
scanf("%s%d",str,&d);
int len = strlen(str);
if(str[0] == '0' && len == 1)
printf("Case %d: divisible\n",Case++ );
else {
ll ans = 0;
int pos = 0;
if(d < 0)
d = -d;
if(str[0] == '-')
pos = 1;
for(;pos < len;pos++){
ans = (ans * 10 + (str[pos] - '0'))%d; //ans 这里会超int,所以要用long long
}
if(ans == 0)
printf("Case %d: divisible\n",Case++ );
else printf("Case %d: not divisible\n",Case++ );
}
}
return 0;
}