PAT (Advanced Level) Practice 進制轉換題ios
P進制轉換爲Q進制,分兩步算法
// y 爲要求的 10進制數 // x 爲 P 進制數,循環中每次取一位 // product 在循環中不斷乘 P,獲得 P 的冪次 int y = 0, product = 1; while (x != 0){ y = y + (x % 10) * product; x = x / 10; product = product * P; }
// 數組 z 用來存放所求 Q進制數的每一位 // num 爲 Q進制數的位數 int z[40], num = 0; do { z[num++] = y % Q; // 除基取餘法 y = y / Q; } while (y != 0); // z 數組從 z[num-1] 到 z[0] 即爲所求
#include<cstdio> bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if ( n % i == 0) return false; return true; } int revD(int n, int D) { int n2D[20], num = 0, result = 0; do { n2D[num++] = n % D; n = n / D; } while(n > 0); for (int i = 0; i < num; i++) result = result * D + n2D[i]; return result; } int main() { int n, D; scanf("%d", &n); while (n >= 0){ scanf("%d", &D); printf("%s",isPrime(n) && isPrime(revD(n,D)) ? "Yes\n" : "No\n"); scanf("%d", &n); } return 0; }
#include<cstdio> int main() { int n, b, num = 0; scanf("%d%d", &n, &b); int bn[31] = {0}; do { bn[num++] = n % b; n = n / b; } while (n > 0); bool isPalindromic = true; for (int i = 0; i < num/2; i++){ if (bn[i] != bn[num-1-i]){ isPalindromic = false; break; } } printf("%s", isPalindromic ? "Yes\n" : "No\n"); printf("%d", bn[num-1]); for (int i = num - 2; i >= 0; i--) printf(" %d", bn[i]); return 0; }
#include<cstdio> int main() { int r, g, b; scanf("%d%d%d", &r, &g, &b); char RGB[8]; char dec213[] = "0123456789ABC"; RGB[0] = '#'; RGB[1] = dec213[r/13]; RGB[2] = dec213[r%13]; RGB[3] = dec213[g/13]; RGB[4] = dec213[g%13]; RGB[5] = dec213[b/13]; RGB[6] = dec213[b%13]; RGB[7] = '\0'; printf("%s",RGB); return 0; }
#include <cstdio> using namespace std; int main() { char c[14] = {"0123456789ABC"}; printf("#"); for(int i = 0; i < 3; i++) { int num; scanf("%d", &num); printf("%c%c", c[num/13], c[num%13]); } return 0; }
#include<cstdio> int main() { int Galleon1, Sickle1, Knut1, Galleon2, Sickle2, Knut2, carrySickle = 0, carryGalleon = 0; scanf("%d.%d.%d %d.%d.%d", &Galleon1, &Sickle1, &Knut1, &Galleon2, &Sickle2, &Knut2); int Knut = Knut1 + Knut2; if (Knut >= 29){ carrySickle = Knut / 29; Knut = Knut % 29; } int Sickle = Sickle1 + Sickle2 + carrySickle; if (Sickle >= 17){ carryGalleon = Sickle / 17; Sickle = Sickle % 17; } int Galleon = Galleon1 + Galleon2 + carryGalleon; printf("%d.%d.%d", Galleon, Sickle, Knut); return 0; }
#include<string> #include<map> #include<iostream> using namespace std; int main() { string ones[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string tens[13] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; map<string, int> M2E; for (int i = 0; i < 13; i++) M2E.insert(pair<string, int>(ones[i], i)); for (int i = 1; i < 13; i++) M2E.insert(pair<string, int>(tens[i], i*13)); int N; cin >> N; getchar(); string s; for (int i = 0; i < N; i++){ getline(cin, s); if (s[0] >= '0' && s[0] <= '9'){ int earth = stoi(s); if (earth/13) cout << tens[earth/13]; if (earth/13 && earth%13) cout << " "; if (earth%13 || earth == 0) cout << ones[earth%13]; cout << endl; } else{ int earth = 0; if (s.length() > 4) earth = M2E[s.substr(0,3)] + M2E[s.substr(4,3)]; else earth = M2E[s]; cout << earth << endl; } } return 0; }