2019-04-14 19:29发布 生成海报
/*1813. M进制数问题 大意:给定M进制,转成10进制再求商和模 思路:转成10进制求商和模,再转成M进制 */ #include #include #include #include #include using namespace std; map m; map m1; void initMap(){ char k = '0'; for(int i=0; i<=9; i++){ m[k] = i; k++; } k = 'A' ; for(int i=10; i<=35; i++){ m[k] = i; k++; } } void initMap2(){ char k = '0'; for(int i=0; i<=9; i++){ m1[i] = k; k++; } k = 'A' ; for(int i=10; i<=35; i++){ m1[i] = k; k++; } } int transferTo10(string a, int M){ int result = 0; int length = a.length(); for(int i=length-1; i>=0; i--) { result += (int)(m[a[i]]*pow((double)M,length-1-i)); } return result; } string transferToM(int x, int M){ if(x == 0) return "0"; string result = ""; while(x !=0 ){ int l = x % M; result = m1[l] + result; x /= M; } return result; } int main() { initMap(); initMap2(); int testNum; int M; cin >> testNum; for(int i=1; i<= testNum; i++){ cin >> M; string a, b; cin >> a >> b; int A = transferTo10(a,M); int B = transferTo10(b,M); int d = A/B; int r = A%B; string strD = transferToM(d, M); string strR = transferToM(r, M); cout << strD << endl << strR << endl; } system("pause"); return 0; }