1048_数字加密

1048 数字加密(20 分)

本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。

输入格式:

输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:

1
1234567 368782971

输出样例:

1
3695Q8118

代码实现:

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
50
#include
#include
#include
using namespace std;

int main(){
	string a, b;
	cin >> a >> b;
	int asi = a.size();
	int bsi = b.size();
	int c[101], d[101];
	for (int i = 0; i < asi; i++){
		c[i] = a[i] - '0';
	}
	for (int i = 0; i < bsi; i++){
		d[i] = b[i] - '0';
	}
	reverse(c, c + asi);
	reverse(d, d + bsi);
	string ans[101];
//	string s[13] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "J", "Q", "K"};
	char s[13] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};
	int m = asi < bsi ? asi : bsi;

	int tmp = 0;
	for (int i = 0; i < m; i++){
		if (i % 2 == 0){
			ans[i] = s[((c[i] + d[i]) % 13)];
		}
		else{
			if (d[i] - c[i] < 0)
				ans[i] = d[i] - c[i] + 10 + '0';
			else
				ans[i] = d[i] - c[i]+'0';
		}
		tmp++;
	}
	if (tmp bsi ? asi : bsi;
	reverse(ans, ans + x);
	for (int i = 0; i < x; i++){
		cout << ans[i];
	}
	return 0;
}

下面这个是柳婼的代码:

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
#include 
using namespace std;
int main() {
    string a, b, c;
    cin >> a >> b;
    int lena = a.length(), lenb = b.length();
    for (int i = 0; i <= lena / 2 - 1; i++)
        swap(a[i], a[lena-1-i]);
    for (int i = 0; i <= lenb / 2 - 1; i++)
        swap(b[i], b[lenb-1-i]);
    if (lena > lenb)
        b.append(lena - lenb, '0');
    else
        a.append(lenb - lena, '0');
    char str[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};
    for (int i = 0; i < a.length(); i++) {
        if (i % 2 == 0) {
            c += str[(a[i] - '0' + b[i] - '0') % 13];
        } else {
            int temp = b[i] - a[i];
            if (temp < 0) temp = temp + 10;
            c += str[temp];
        }
    }
    for (int i = c.length() - 1; i >= 0; i--)
        cout << c[i];
    return 0;
}

个人思考:

看完柳婼的代码,就会发现我的代码略微繁琐了.

你可能感兴趣的:(1048_数字加密)