第三章 进制
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:
Sample Output:
123456结尾无空行
#include <iostream>using namespace std;char get(int x){if(x <= 9) return '0' + x;return 'A' + x -10;}int main(){int a[3];for (int i = 0; i < 3; i ++){cin >> a[i];}cout << '#';for (int i =0 ; i < 3; i ++){cout << get(a[i]/13) << get(a[i] % 13);}}
就是十进制转换成13进制,用get函数进行了转换,
1100 Mars Numbers (20 分)
People on Mars count their numbers with base 13:
- Zero on Earth is called “tret” on Mars.
- The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively.
- For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively.
For examples, the number 29 on Earth is called “hel mar” on Mars; and “elo nov” on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then _N_ lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
Sample Input:
Sample Output:
hel mar may 115 13结尾无空行
#include <iostream>#include <sstream>using namespace std;char name[][5] ={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou",};int get(string word){for (int i =0; i < 25; i ++){if(name[i] == word){if(i <=12 ) return i;return (i - 12 ) * 13;}}}int main(){int n;cin >> n;getchar();while(n --){string line;getline(cin ,line);stringstream ssin(line);if(line[0] <= '9'){int v;ssin >> v;if(v <=13) cout << name[v]<< endl;else if(v % 13 ==0) cout << name[12 + v/13] << endl;else cout << name[12 +v/13] <<' '<< name[v %13] << endl;}else{int res = 0;string word;while(ssin >> word) res += get(word);cout << res << endl;}}}
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 … _a_0”. Notice that there must be no extra space at the end of output.
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
No 4 4 1结尾无空行
鸣谢网友“CCPC拿不到牌不改名”修正数据!
#include <iostream>
#include <vector>
using namespace std;
vector<int> nums;
bool check(){
for(int i = 0, j = nums.size()-1; i < j; i ++, j --){
if(nums[i] !=nums[j]) return false;
}
return true;
}
int main(){
int n, b;
cin >> n >> b;
if(!n) nums.push_back(0);
while(n) nums.push_back(n % b), n /= b;
if(check()) puts("Yes");
else puts("No");
cout << nums.back();
for(int i = nums.size() -2; i >= 0; i --) cout << ' ' << nums[i];
return 0;
}
