1048 数字加密 (20 分)

对AB的长度不一致处理时不能用swap()函数,必须分情况单独处理,因为偶数位要求用B的数字减去A的数字,交换AB会导致减法次序错误。

string a,b;
char mp[]={'J','Q','K'};

char trans(int x)
{
    if(x<10) return '0'+x;
    else return mp[x-10];
}

int main()
{
    cin>>a>>b;

    while(a.size() < b.size()) a='0'+a;
    while(b.size() < a.size()) b='0'+b;

    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());

    string res;
    for(int i=0;i<a.size();i++)
    {
        int ta=a[i]-'0',tb=b[i]-'0';

        if(i+1 & 1) res+=trans((ta+tb)%13);
        else
        {
            int t=tb-ta;
            if(t<0) t+=10;
            res+='0'+t;
        }
    }

    reverse(res.begin(),res.end());

    cout<<res<<endl;
   //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14400910.html