1020. Big Integer

一直没写过技术博客,此博客到大三才开实在是晚。

此题为“算法分析与设计”的作业第一题,是sicily题库里的编号为1020的题目。

早上老师课上刚说分治法,看题目要求是大数求模,估计用int之类的存不下了,所以用string存后再处理。


以下是我AC的答案:

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
	int tests;
	cin >> tests;
	while(tests--) {
		int basisSize;
		cin >> basisSize;
		
		int basis[basisSize];
		for(int i = 0; i < basisSize; i++) {
			cin >> basis[i];
		}
		
		string veryLongInteger;
		cin >> veryLongInteger;
		
		int representation[basisSize];
		for(int i = 0; i < basisSize; i++) {
			int temp = 0;
	    	for(int j = 0; j < veryLongInteger.length(); j++) {
				temp = temp * 10 + (veryLongInteger[j] - '0');
				temp %= basis[i];
			}
			representation[i] = temp;
		}
		
		cout << '(';
		for(int i = 0; i < basisSize; i++) {
			if(i < basisSize - 1) {
			    cout << representation[i] << ',';
		    }
		    else {
		    	cout << representation[i] << ')';
		    }
		}
		cout << endl;
	}
	return 0;
}



AC前有份WA的,一直想不通(主要是懒),求大神指点:


#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    int tests;
    cin >> tests;
    while(tests--) {
        int basisSize;
        cin >> basisSize;
        
        int basis[basisSize];
        for(int i = 0; i < basisSize; i++) {
            cin >> basis[i];
        }
        
        string veryLongInteger;
        cin >> veryLongInteger;
        
        int bitsOfInteger[100] = {0};
        int bits = 0;
        for(int i = 0; i < 100; i++) {
            if(veryLongInteger[i] != '\0') {
                bitsOfInteger[i] = static_cast<int>(veryLongInteger[i]) - 48;
                bits++;
            }
            else {
                break;
            }
        }
        
        int representation[basisSize];
        memset(representation, 0, sizeof(representation));
        for(int i = 0; i < bits; i++) {
            for(int j = 0; j < basisSize; j++) {
                representation[j] = representation[j] * 10 + bitsOfInteger[i];
                representation[j] %= basis[j];
            }
        }
        
        cout << '(';
        cout << representation[0];
        for(int i = 1; i < basisSize; i++) {
            cout << ',' << representation[i];
        }
        cout << ')' << endl;
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值