PAT 乙级 1048 数字加密(20)

用力戳我直达原题~

这题很特别水。不过注意两个点,见注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin >> a >> b;
    //测试数据弱,但还是要考虑前导0
    for(int i = 0; i < a.size(); i++)
    {
        if(a[i] != '0')
        {
            a = a.substr(i,a.size());
            break;
        }
    }
    for(int i = 0; i < b.size(); i++)
    {
        if(b[i] != '0')
        {
            b = b.substr(i,b.size());
            break;
        }
    }
    map<int,char>mp;
    stack<char>sta;
    for(int i = 0; i <= 9; i++)
        mp[i] = '0' + i;
    mp[10] = 'J'; mp[11] = 'Q'; mp[12] = 'K';
    int i = a.size() - 1;
    int j = b.size() - 1;
    bool f = true;
    //注意有A>B 和B>A的情况!
    while(i >=0 || j >= 0)
    {
        int x = (i >= 0 ? a[i] - '0' : 0);
        int y = (j >= 0 ? b[j] - '0' : 0);
        if(f)
            sta.push( mp[(y + x) % 13] );
        else
            sta.push( mp[ y - x >= 0 ? y - x : 10 + y - x ] );
        i--,j--,f = !f;
    }
    while(!sta.empty())
    {
        cout << sta.top();
        sta.pop();
    }
    cout << endl;
}
原文地址:https://www.cnblogs.com/bestwzh/p/6399760.html