subject
Mars are counted in 13ary:
- Earth's 0 is called tret by Mars.
- The numbers 1 to 12 in Mars are jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec.
- The twelve high-digit numbers that Mars will advance to are called tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou.
For example, the number "29" of Earth Man is translated into Mars as "hel mar"; while the Mars word "elo nov" corresponds to the earth number "115".For easy communication, write a program to translate the Earth and Mars numbers.
Input format:
Enter the first line to give a positive integer N (<100), followed by N lines, each giving a number in the [0, 169] interval - either Geodesic or Mars.
Output format:
For each line of input, output the translated number in another language in one line.
Input sample:
4 29 5 elo nov tam
Output sample:
hel mar may 115 13
Ideas for implementation:
It's not difficult to think as a whole. Read in the data, decide which number to use, match and convert the output, mainly in detail.As you can see from the example, the Mars number is not the same as the Earth number, where a two-digit number like 20 is followed by 0. In addition, as a matching array of Mars numbers, at least one digit must remain to store the end'\0', otherwise the data output will be connected.
The code is as follows:
#include<stdio.h> #include<string.h> int main() { //Two storage matching arrays must reserve the end'\0'bit so that the first high bit can be left blank to match the number char szD[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"}; char szG[13][4]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"}; int iNum=0; scanf("%d",&iNum); getchar();//When scanf and gets are used together, be sure to note that the carriage return behind scanf is removed char szData[iNum][9]; for(int i=0;i<iNum;i++) { gets(szData[i]); } int iSum=0; for(int i=0;i<iNum;i++) { iSum=0; //Judge as Earth Number if(szData[i][0]>='0'&&szData[i][0]<='9') { //Get a numeric value for(int j=0;j<strlen(szData[i]);j++) { iSum=iSum*10+szData[i][j]-'0'; } //Classified Output if(iSum/13!=0&&iSum%13!=0) { printf("%s %s",szG[iSum/13],szD[iSum%13]); } else if(iSum/13!=0&&iSum%13==0) { printf("%s",szG[iSum/13]); } else if(iSum/13==0) { printf("%s",szD[iSum%13]); } } else { //With only one Mars number if(strlen(szData[i])<=4) { for(int j=0;j<13;j++) { if(strcmp(szData[i],szD[j])==0) { iSum=j; break; } else if(strcmp(szData[i],szG[j])==0) { iSum=j*13; break; } } } //Two Mars Numbers else { for(int j=0;j<13;j++) { if(strncmp(szData[i],szG[j],3)==0)//Compare the top three, the higher { iSum+=13*j; } if(strcmp(&szData[i][4],szD[j])==0) { iSum+=j; } } } printf("%d",iSum); } if(i!=iNum-1) { printf("\n"); } } return 0; }
Error-prone points:
1. Mars 10 does not output 0;
2. Combining scanf with gets in the middle will leave the first gets empty and the last one missing;
3. Use of StrCmp and strncmp.