【PAT】B1044 火星數字(20 分)
阿新 • • 發佈:2018-08-18
字符 == 火星文 names stdio.h ace %d algorithm main
/* 火星文有兩位,第二位為0不輸出 */ #include<stdio.h> #include<algorithm> #include<string.h> #include<ctype.h> using namespace std; char arr[20]={0}; char data[13][2][5]={ {"tret","tret"}, {"jan","tam"}, {"feb","hel"}, {"mar","maa"}, {"apr","huh"}, {"may","tou"}, {"jun","kes"}, {"jly","hei"}, {"aug","elo"}, {"sep","syy"}, {"oct","lok"}, {"nov","mer"}, {"dec","jou"} }; void prr(char *str){ if(str[3]==‘t‘&&str[0]==‘t‘&&str[1]==‘r‘&&str[2]==‘e‘){ printf("0");return; } if(islower(str[0])){//輸入為火星文 int high=0,low=0; for(int i=0;i<13;i++){ if(data[i][1][0]==str[0]&&data[i][1][1]==str[1]&&data[i][1][2]==str[2])//前邊的字符只可能是高位 high+=i*13; if(data[i][0][0]==str[0]&&data[i][0][1]==str[1]&&data[i][0][2]==str[2])//後邊的字符不確定 low+=i; if(data[i][0][0]==str[4]&&data[i][0][1]==str[5]&&data[i][0][2]==str[6]&&str[3]!=‘\0‘) low+=i; } printf("%d",low+high); } else{//輸入為數字 int temp; sscanf(str,"%d",&temp); if(temp<13){ printf("%s",&data[temp][0]); } else{ printf("%s",&data[temp/13][1]); if(temp%13!=0) printf(" %s",&data[temp%13][0]); } } } int main(){ int N;scanf("%d",&N); for(int i=0;i<N;i++){ scanf(" %[^\n]",arr); if(i!=0) printf("\n"); prr(arr); } return 0; }
【PAT】B1044 火星數字(20 分)