我是靠谱客的博主 震动滑板,这篇文章主要介绍LightOJ 1214 Large Division(大整数取模),现在分享给大家,希望可以做个参考。

题目链接:
LightOJ 1214 Large Division
题意:
大整数取模。输入a,b(-10^200 <= a <= 10^200, b != 0, b是int),判断a能否整除b。
分析:
把大整数写成“从左向右”的形式:如:1234 = ((1 * 10 + 2) * 10 + 3) * 10 + 4.然后根据(n + m) % p = ((n % p) + (m % p)) % p,每步去模。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <climits> #include <cmath> #include <ctime> #include <cassert> #define IOS ios_base::sync_with_stdio(0); cin.tie(0); using namespace std; typedef long long ll; const int MAX_N = 250; int T, cases = 0, p; char s[MAX_N]; int main() { scanf("%d", &T); while(T--){ scanf("%s%d", &s, &p); printf("Case %d: ", ++cases); int len = strlen(s); if(s[0] == '0' && len == 1){ printf("divisiblen"); continue; } if(p < 0) p = -p; int ans = 0, st = 0; if(s[0] == '-') st = 1; for(int i = st; i < len; i++){ ans = (int)(((ll) ans * 10 + s[i] - '0') % p); } if(ans == 0) printf("divisiblen"); else printf("not divisiblen"); } return 0; }

最后

以上就是震动滑板最近收集整理的关于LightOJ 1214 Large Division(大整数取模)的全部内容,更多相关LightOJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部