HDU1212 Big Number(大数取模)

HDU - 1212 Big Number

Problem Description

As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B.
To make the problem easier, I promise that B will be smaller than 100000.
Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

Input

The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.

Output

For each test case, you have to ouput the result of A mod B.

Sample Input

2 3
12 7
152455856554521 3250

Sample Output

2
5
1521

题解

题意

给定一个大数A(不超过1000位),给定一个不超过100000的数B,求A%B的值。

思路

秦九韶算法。例如1234 7 数据。计算方法为:

1234%7 = (((1*10%7+2)*10%7+3)*10%7+4)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
int T;

int B;
const int MAXN = 1000+10;
char str[MAXN];

int main() {
    while(~scanf("%s",str)){
    	scanf("%d",&B);
    	int len = strlen(str);
    	int ans = 0;
    	int cur = 0;
    	for(int i=0;i<len;i++){
			cur = cur*10+str[i]-'0';
			cur%=B;
		}
		printf("%d
",cur);
	}
}

原文地址:https://www.cnblogs.com/caomingpei/p/9695824.html