PAT 甲级 1019 General Palindromic Number (20 分) 进制转换问题+回文问题 我的代码+别人的代码

矫情吗;* 2024-02-19 20:55 99阅读 0赞

1019 General Palindromic Number (20 分)

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N>0 in base b≥2, where it is written in standard notation with k+1 digits ai as ∑i=0k(aibi). Here, as usual, 0≤ai<b for all i and ak is non-zero. Then N is palindromic if and only if ai=ak−i for all i. Zero is written 0 in any base and is also palindromic by definition.

Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤109 is the decimal number and 2≤b≤109 is the base. The numbers are separated by a space.

Output Specification:

For each test case, first print in one line Yes if N is a palindromic number in base b, or No if not. Then in the next line, print N as the number in base b in the form “ak ak−1 … a0”. Notice that there must be no extra space at the end of output.

Sample Input 1:

  1. 27 2

Sample Output 1:

  1. Yes
  2. 1 1 0 1 1

Sample Input 2:

  1. 121 5

Sample Output 2:

  1. No
  2. 4 4 1

鸣谢网友“CCPC拿不到牌不改名”修正数据!

主要问题就两点:

进制转换

判断回文

但是有一个坑:不能用字符存转换后的结果,因为b的范围很大(意味着每一位的余数可能大于9,很大很大,2、4测试点卡这个),用字符只能存0~9,

我的代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. int len=0;
  6. int a[50];
  7. void convert(int n,int b)
  8. {
  9. while(n!=0)
  10. {
  11. a[len++]=(n%b);
  12. n/=b;
  13. }
  14. }
  15. int judge(int a[])
  16. {
  17. int i=0,j;
  18. int f=1;
  19. j=len-1;
  20. while(i<j)
  21. {
  22. if(a[i]!=a[j])
  23. {
  24. f=0;
  25. break;
  26. }
  27. i++;
  28. j--;
  29. }
  30. return f;
  31. }
  32. int main()
  33. {
  34. int i,n,m,k,l;
  35. cin>>n>>m;
  36. convert(n,m);
  37. k=judge(a);
  38. if(k==1)
  39. {
  40. printf("Yes\n");
  41. l=len;
  42. for(i=l-1;i>=0;i--)
  43. if(i==l-1)
  44. printf("%d",a[i]);
  45. else
  46. printf(" %d",a[i]);
  47. }
  48. else
  49. {
  50. printf("No\n");
  51. l=len;
  52. for(i=len-1;i>=0;i--)
  53. if(i==l-1)
  54. printf("%d",a[i]);
  55. else
  56. printf(" %d",a[i]);
  57. }
  58. printf("\n");
  59. return 0;
  60. }

别人的代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. bool Judge(int z[], int num) {
  6. for(int i = 0; i <= num / 2; i++) {
  7. if(z[i] != z[num - i - 1]) {
  8. return false;
  9. }
  10. }
  11. return true;
  12. }
  13. int main() {
  14. int n, b, z[40], num = 0;
  15. scanf("%d %d", &n, &b);
  16. do {
  17. z[num++] = n % b;
  18. n /= b;
  19. }while(n != 0);
  20. bool flag = Judge(z, num);
  21. if(flag) {
  22. printf("Yes\n");
  23. } else {
  24. printf("No\n");
  25. }
  26. for(int i = num - 1; i >= 0; i--) {
  27. printf("%d", z[i]);
  28. if(i != 0) {
  29. printf(" ");
  30. }
  31. }
  32. return 0;
  33. }

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读