当前位置: 代码迷 >> 综合 >> [PAT][Basic Level]1048
  详细解决方案

[PAT][Basic Level]1048

热度:58   发布时间:2023-12-05 22:56:50.0

NOT AC CODE:`
``cpp
#include
#include
#include
#include
using namespace std;
#define M 100+1
int main()
{
char strA[M];
char strB[M];
char str_output[M+10];
cin>>strA>>strB;
char to_char[13]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘J’,‘Q’,‘K’};

int len = strlen(strA)> strlen(strB) ?strlen(strA):strlen(strB);
int len_output = 0;
int count = 0;
int i = 0 ,j = 0;
for(i = strlen(strA)-1,j = strlen(strB)-1 ; i >-1 && j > -1; i -- , j --){count++ ; // count 的意义是位数char ch_A = strA[i];char ch_B = strB[j];//cout<<ch_A<<" "<<ch_B<<endl;if(count%2!=0){   // 奇数位int sum = ch_A -'0' + ch_B - '0';sum = sum %13;str_output[len_output++] = to_char[sum];//cout<<sum<<endl;}else if(count%2==0){int ans = ch_B-'0'- ch_A +'0';if(ans < 0 ) {ans+=10 ;}str_output[len_output++] = ans +'0';}/*for(int j = 0 ; j < len_output ; j ++){cout<<str_output[j]<<" ";}*/
}if(len == strlen(strB)){while(j>-1){str_output[len_output++] = strB[j--];}
}else if(len == strlen(strA)){while(i>-1){str_output[len_output++] = strA[i--]; }
}//cout<<str_output<<endl;for(int k = len_output-1 ; k > -1 ; k --){cout<<str_output[k];
}
return 0;

}


  相关解决方案