PAT 甲级 1100
People on Mars count their numbers with base 13:
Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch 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:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
在pat甲级1100遇到这么一个问题,就是我要输入int类型n,之后我就要输入字符串。但问题来了,我要输入的有可能是一行字符串。
int main()
{
init(); //打表 :在程序中一次性计算出所有需要用的结果,之后的查询直接取这些结果就行
int T;
cin>>T;
while(T--){
string str;
getline(cin, str);//不能用cin,用“cin>>”从输入流提取数据,遇空格就终止
if(str[0] >= '0' && str[0] <= '9'){//字符转换成数字
int num = 0;
for(int i = 0; i < str.length(); i++){
num = num * 10 + (str[i] - '0');
}
cout << numToStr[num] << endl;
}
else{
cout << strToNum[str] << endl;
}
}
return 0;
}
但是这样直接运行会报字符串内存越界的错误,刚开始百思不得其解。后来经过调式发现,原来在我们开始输入T的时候就输了一个换行,而这个换行会被str读取,导致我们无法继续输入,并且程序也会报错。
然后解决这个问题呢,就是在cin>>T
的后面加一句getchar()
,让getcahr
读取换行。这样就避免输入时换行对str
的影响