PAT甲级 进制转换题_C++题解

进制转换题

PAT (Advanced Level) Practice 进制转换题

目录

  • 《算法笔记》 重点摘要
  • 1015 Reversible Primes (20)
  • 1019 General Palindromic Number (20)
  • 1027 Colors in Mars (20)
  • 1058 A+B in Hogwarts (20)
  • 1100 Mars Numbers (20)

《算法笔记》 3.5 进制转换 重点摘要

P进制转换为Q进制,分两步

  • P进制转换为10进制
// 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;
}
  • 10进制转换为Q进制
// 数组 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] 即为所求

1015 Reversible Primes (20)

#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;
}
  • 记忆判断素数函数
  • 记忆进制转换函数
  • 注意:10进制转D进制后得到的D进制数是数组高位到地位,即反向,故转换回10进制时只要从低位开始加即可得到反转的D进制对应的10进制

1019 General Palindromic Number (20)

#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;
}
  • 记忆进制转换函数,注意用 do while 保证 n 为0情况

1027 Colors in Mars (20)

#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;
}
  • char数组输出问题
    • 有初始化时尽量不要对字符数组长度进行定义,应该把这个长度交给系统来进行分配
    • 若用 %s 输出,要遇到 '\0' 符号的时候,才会停止输出,所以初始化字符数组的时候最好以 '\0' 结尾
    • 若不以 '\0' 作字符数组结尾,用 %s 输出会出错,只能逐个输出。
    • 使用字符串初始化字符数组时,比用字符逐个赋值要多占用一个字节,用于存放 '\0' ,同时字符数组的长度不会去计算 '\0' ,但是数组占有的字节数会将 '\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;
}
  • 输入一个数处理一个数,逐个字符进行输出

1058 A+B in Hogwarts (20)

#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;
}
  • 注意没有输入的数据要初始化,如carrySickle、carryGalleon
  • 注意要先保存进位再求余更新本位,否则会丢失进位信息

1100 Mars Numbers (20)

#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;
}
  • 题目坑点:13的整倍数输出时不用在低位加"tret",如39,应该输出maa而不是maa tret。
  • 注意:输入火星数字时中间可能有空格,需要用getline(),要用getchar()吸收前面输入N后的换行符。
posted @ 2019-08-12 11:17  鲸90830  阅读(388)  评论(0编辑  收藏  举报