1. 程式人生 > >PAT (Advanced Level) Practice 1027 Colors in Mars (20)(20 分)

PAT (Advanced Level) Practice 1027 Colors in Mars (20)(20 分)

1027 Colors in Mars (20)(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

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

Output

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 the left.

Sample Input

15 43 71

Sample Output

#123456

題解1:

把讀入的每個十進位制數轉換成13進位制數,依次輸出。這裡看到只有3個數,就偷懶沒有寫迴圈,結果自己粗心大意沒有把陣列下標改好,浪費了許多時間。

原始碼1:

#include <iostream>
using namespace std;

int main()
{
	int a, b, c;
	char res[8] = {"#000000"};

	cin >> a >> b >> c;
	if (a / 13 <= 9)	res[1] = a / 13+'0';
	else res[1] = a / 13 - 10 + 'A';
	if (b / 13 <= 9)	res[3] = b / 13 + '0';
	else res[3] = b / 13 - 10 + 'A';
	if (c / 13 <= 9)	res[5] = c / 13 + '0';
	else res[5] = c / 13 - 10 + 'A';
	if (a % 13 <= 9) res[2] = a % 13 + '0';
	else res[2] = a % 13 - 10 + 'A';
	if (b % 13 <= 9) res[4] = b % 13 + '0';
	else res[4] = b % 13 - 10 + 'A';
	if (c % 13 <= 9) res[6] = c % 13 + '0';
	else res[6] = c % 13 - 10 + 'A';
	for (int i = 0; i < 7; i++)
		cout << res[i];
	return 0;
}

題解2:

把13進位制數的每個數存入一個數組中,每次計算得到一個數,就輸出該陣列的該位數。

原始碼2:

#include <iostream>
using namespace std;

int main()
{
  int a[3];
  char s[14]={"0123456789ABC"};
  for(int i=0;i<3;i++)
  cin>>a[i];
  cout<<"#";
  for(int i=0;i<3;i++)
  cout<<s[a[i]/13]<<s[a[i]%13];

	return 0;
}