1. 程式人生 > >PAT-ADVANCED1058——A+B in Hogwarts

PAT-ADVANCED1058——A+B in Hogwarts

我的PAT-ADVANCED程式碼倉:https://github.com/617076674/PAT-ADVANCED

原題連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805416519647232

題目描述:

知識點:進位制轉換、資料越界

思路:轉換為Knut相加,再轉換為Galleon.Sickle.Knut的格式輸出

需要用long long型變數處理資料,否則資料會越界,測試點2無法通過。

時間複雜度和空間複雜度分析對本題的意義不大。

C++程式碼:

#include<iostream>
#include<vector>

using namespace std;

long long changeToKnut(long long Galleon, long long Sickle, long long Knut);

int main(){
	long long Galleon1, Sickle1, Knut1, Galleon2, Sickle2, Knut2;
	scanf("%lld.%lld.%lld", &Galleon1, &Sickle1, &Knut1);
	long long AKnuts = changeToKnut(Galleon1, Sickle1, Knut1);
	scanf("%lld.%lld.%lld", &Galleon2, &Sickle2, &Knut2);
	long long BKnuts = changeToKnut(Galleon2, Sickle2, Knut2);
	long long totalKnuts = AKnuts + BKnuts;
	vector<long long> result;
	result.push_back(totalKnuts % 29);
	totalKnuts /= 29;
	result.push_back(totalKnuts % 17);
	result.push_back(totalKnuts / 17);
	printf("%lld.%lld.%lld\n", result[2], result[1], result[0]);
	return 0;
}

long long changeToKnut(long long Galleon, long long Sickle, long long Knut){
	return Galleon * 17 * 29 + Sickle * 29 + Knut;
}

C++解題報告: