1. 程式人生 > >PAT-BASIC1074——宇宙無敵加法器

PAT-BASIC1074——宇宙無敵加法器

題目描述:

知識點:進位制轉換

思路:實時考慮進位的影響

注意點:

即使遍歷完了兩個字串input2和input3,只要進位符號flag有值,就需要繼續計算

再進一步,即使遍歷完了字串input1,只要進位符號flag有值,就要繼續計算,只不過這時當作10進位制計算

時間複雜度是O(20)。空間複雜度是O(1)。

C++程式碼:

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string reverseString(string s);

int main() {
	string input1;
	string input2;
	string input3;

	getline(cin, input1);
	getline(cin, input2);
	getline(cin, input3);

	string reverseInput1 = reverseString(input1);
	string reverseInput2 = reverseString(input2);
	string reverseInput3 = reverseString(input3);

	int index = 0;
	int flag = 0;
	string result = "";
	while(true) {
		if(index >= reverseInput2.length() && index >= reverseInput3.length()) {
			break;
		} else if(index < reverseInput2.length() && index >= reverseInput3.length()) {
			int sum = reverseInput2[index] + flag - '0';
			int system = reverseInput1[index] - '0';
			if(system == 0) {
				system = 10;
			}
			if(sum >= system) {
				flag = sum / system;
				int output = sum - system * flag;
				char outputChar = '0' + output;
				result = outputChar + result;
			} else {
				flag = 0;
				int output = sum;
				char outputChar = '0' + output;
				result = outputChar + result;
			}
		} else if(index >= reverseInput2.length() && index < reverseInput3.length()) {
			int sum = reverseInput3[index] + flag - '0';
			int system = reverseInput1[index] - '0';
			if(system == 0) {
				system = 10;
			}
			if(sum >= system) {
				flag = sum / system;
				int output = sum - system * flag;
				char outputChar = '0' + output;
				result = outputChar + result;
			} else {
				flag = 0;
				int output = sum;
				char outputChar = '0' + output;
				result = outputChar + result;
			}
		} else if(index < reverseInput2.length() && index < reverseInput3.length()) {
			int sum = reverseInput3[index] + flag - '0' + reverseInput2[index] - '0';
			int system = reverseInput1[index] - '0';
			if(system == 0) {
				system = 10;
			}
			if(sum >= system) {
				flag = sum / system;
				int output = sum - system * flag;
				char outputChar = '0' + output;
				result = outputChar + result;
			} else {
				flag = 0;
				int output = sum;
				char outputChar = '0' + output;
				result = outputChar + result;
			}
		}
		index++;
	}
	while(flag != 0 && index < reverseInput1.length()) {
		int sum = flag;
		int system = reverseInput1[index] - '0';
		if(system == 0) {
			system = 10;
		}
		if(sum >= system) {
			flag = sum / system;
			int output = sum - system * flag;
			char outputChar = '0' + output;
			result = outputChar + result;
		} else {
			flag = 0;
			int output = sum;
			char outputChar = '0' + output;
			result = outputChar + result;
		}
	}
	while(flag != 0){
		int sum = flag;
		int system = 10;
		if(sum >= system) {
			flag = sum / system;
			int output = sum - system * flag;
			char outputChar = '0' + output;
			result = outputChar + result;
		} else {
			flag = 0;
			int output = sum;
			char outputChar = '0' + output;
			result = outputChar + result;
		}
	}

	int i = 0;
	while(result[i] == '0'){
		i++;
		if(i == result.length()){
			cout << 0 << endl;
			return 0;
		}
	}
	for(int j = i; j < result.length(); j++){
		cout << result[j];
	}

	return 0;
}

string reverseString(string s) {
	string result = "";
	for(int i = s.length() - 1; i >= 0; i--) {
		result += s[i];
	}
	return result;
}

C++解題報告: