PAT甲级1019题(回文数)


1.题目介绍

1019

decimal:十进制

palindromic:回文的

2.考察点,难度

进制转换,回文判断,难度易

3.解题代码

#include<cstdio>
#include <iostream>
using namespace std;
 
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 <iostream>
//#include <string>
//#include <algorithm>
//using namespace std;
//
//string rev(int N, int b){
//	string s="";
//	do{
//		s+=to_string(N%b);
//		N=N/b;
//	}while(N>0);
//	return s;
//}
//
//int main(){
//	int N,b;
//	string Real, Rev;
//	scanf("%d %d",&N,&b);
//	Rev = Real = rev(N,b);
//	reverse(Rev.begin(),Rev.end()); 
//	Real==Rev?cout<<"Yes"<<endl:cout<<"No"<<endl;
//	for(int i=0;i<Rev.size();i++){
//		i==Rev.size()-1?printf("%c",Rev[i]):printf("%c ",Rev[i]);		
//	}
//	return 0;
//} 

4.原题地址

https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984


文章作者: Peyton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Peyton !
  目录