注意点:
这题不能用gets(会把空格吃掉);
#include<cstdio> #include<cstring> void reverse(char s[]){ int len = strlen(s); for(int i=0;i<len/2;i++){ char temp = s[i]; s[i] = s[len-i-1]; s[len-i-1] = temp; } } int main(){ char A[110],B[110],ans[110]={0}; scanf("%s %s",A,B); reverse(A); reverse(B); int lenA = strlen(A); int lenB = strlen(B); int len = lenA > lenB ? lenA:lenB; for(int i=0;i<len;i++){ int numA = i<lenA? A[i]-'0':0; int numB = i<lenB? B[i]-'0':0; //这里再次把字符转换为数字,并且位数较少的那个数,补0 if(i%2==0){ //偶数的情况 int temp = numA+numB; temp = temp%13; if(temp==10) ans[i] = 'J'; else if(temp==11) ans[i] = 'Q'; else if(temp==12) ans[i] = 'K'; else ans[i] = temp+'0'; }else{ int temp = numB-numA; if(temp<0) temp+=10; ans[i] = temp+'0'; } } reverse(ans); puts(ans); return 0; }