The topics are as follows:
People in Mars represent the colors in their computers in a similar as the Earth people. That was, a color is represented by a 6-digit number, where the first 2 digits was for Red, and the middle 2 digits for Green, and the last 2 digits for Blue. The only difference are that they use radix (0-9 and A-c) instead of 16. Now given a-color in three decimal numbers (each between 0 and 168), you is supposed to output their Mars RGB values.
Input
Each of the 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 "#" and then followed by a 6-di Git number where all the 中文版 characters must be upper-cased. If a single color was only 1-digit long and you must print a "0" to the left.
Sample Input
15 43 71
Sample Output
#123456
The essence of this topic is to examine the addition of K-rest method, the method is to use the decimal number to remove the radix K, each time the quotient as the next value, the remainder is written next to the quotient is less than the cardinality, the end of the operation. And then from bottom to top. All the numbers from the last quotient to the first remainder form the result, such as converting the decimal number 38 to 13 binary (0-9,A-C)
From bottom to top, each is 2, 12, this number is 2C. Therefore, 38 of the 13 in the form of 2C, according to this method to design the program.
Since the 13 binary involves letters, use a string to store the number. Each time a character is inserted in the string header.
#include <iostream> #include <vector> #include <string> #include <string.h>using namespace std; char Int2char (int n) { if (n <= 9) { return ' 0 ' + N; } else{ return ' A ' + (n-10); }} string convertmars (int value) { string temp; int Shang,yu; int radix =; while (1) { Shang = Value/radix; Yu = value% Radix; Temp.insert (Temp.begin (), Int2char (Yu)); Value/= radix; if (value < radix) { Temp.insert (Temp.begin (), Int2char (value)); break; } } return temp;} int main () { string mr,mg,mb; int r,g,b; CIN >> R >> G >> B; MR = Convertmars (R); MG = Convertmars (G); MB = Convertmars (B); cout << "#" << MR << mG << MB; return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
1027. Colors in Mars (20)