SOJ 1020 Big Integer

题目大意: 输入T,表示测试样例总数。对每个样例,首先输入n(n ≤ 10),接下来有n个数输入,计作b1...bn,最后输入x,其中x是一个非常大不超过400字符的长正整数。

限制条件:1 < bi ≤ 1000,  1 ≤ i ≤ n 且 gcd(bi, bj) = 1 ,  (1 ≤ i, j ≤ n, i ≠ j)

     让M = b1 * b2 * ... * bn , x < M

输出:x % bi  = ri

  格式: (r1,r2,...,rn)

 

解题思路:

  由于长正整数x非常大,不能用基本类型保存。模拟除法运算求余数。

 

代码如下:

 1 #include <iostream>
 2 #include <string>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int char2Int(char ch) {
 7     return ch - '0';
 8 }
 9 
10 int main() {
11     string str;
12     int T, n;
13     const int MAXN = 105;
14     int b[MAXN];
15     int m[MAXN];
16     memset(b, sizeof b, 0);
17     // memset(m, sizeof m, 0);
18 
19     cin >> T;
20     while (T--) {
21         // 输入
22         cin >> n;
23         for (int i = 0; i < n; i++) {
24             cin >> b[i];
25         }
26         cin >> str;
27 
28         // 计算
29         memset(m, 0, sizeof m);    // 太久没用memset,给忘了,没有正确初始化。
30         for (int i = 0; i < str.length(); i++) {
31             int next = char2Int(str[i]);
32             for (int j = 0; j < n; j++) {
33                 // cout << j << ": ( " << m[j] << " * 10 + " << next << " ) % " << b[i] << " = ";
34                 m[j] = (m[j] * 10 + next) % b[j];
35                 // cout << m[j] << endl;
36             }
37         }
38 
39         // 输出
40         cout << "(" << m[0];
41         for (int i = 1; i < n; i++) {
42             cout << "," << m[i];
43         }
44         cout << ")" << endl;
45     }
46     
47     return 0;
48 }

 

posted @ 2015-09-23 19:13  MchCyLh  阅读(217)  评论(0编辑  收藏  举报