PAT 甲级 1027 Colors in Mars (20 分) 进制转换问题 我的代码+别人的代码

╰+哭是因爲堅強的太久メ 2024-02-19 20:59 110阅读 0赞

1027 Colors in Mars (20 分)

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.

Sample Input:

  1. 15 43 71

Sample Output:

  1. #123456

我的代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. string convert(int n)
  6. {
  7. string s="";
  8. while(n!=0)
  9. {
  10. if(n%13>9)
  11. s+=(n%13-10+65);
  12. else
  13. s+=(n%13+'0');
  14. n/=13;
  15. }
  16. return s;
  17. }
  18. void print(string t)
  19. {
  20. if(t.length()==0)
  21. printf("00");
  22. else
  23. if(t.length()==1)
  24. printf("0%c",t[0]);
  25. else
  26. printf("%c%c",t[1],t[0]);
  27. }
  28. int main()
  29. {
  30. int a,b,c,i,j,k;
  31. string t;
  32. cin>>a>>b>>c;
  33. printf("#");
  34. t=convert(a);
  35. print(t);
  36. t=convert(b);
  37. print(t);
  38. t=convert(c);
  39. print(t);
  40. printf("\n");
  41. return 0;
  42. }

别人的代码:

差距还是有的!

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. char radix[13] ={
  6. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'
  7. };
  8. int main() {
  9. int r, g, b;
  10. scanf("%d%d%d", &r, &g, &b);
  11. printf("#");
  12. printf("%c%c", radix[r / 13], radix[r % 13]);
  13. printf("%c%c", radix[g / 13], radix[g % 13]);
  14. printf("%c%c", radix[b / 13], radix[b % 13]);
  15. return 0;
  16. }

加油!少年

发表评论

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

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

相关阅读