[PAT][Basic Level]1048

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;

}


上一篇:1048 数字加密 (20 分) javascript


下一篇:1048 最长字符串链