您的位置:首页 > 理论基础 > 数据结构算法

1048. 数字加密(20)/YHF/2016.11.22

2016-11-22 12:49 246 查看


1048. 数字加密(20)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

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

输入格式:

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

输出格式:

在一行中输出加密后的结果。
输入样例:
1234567 368782971

输出样例:

3695Q8118

#include <iostream>
#include<algorithm>
#include<sstream>
#include <string>
using namespace std;
int main()
{
int i, j, temp, haha = 0;
string s1;
string s2;
string s3;
string p = { 'J','Q','K' };
cin >> s1 >> s2;
i = 0;
while (s1[i] == '0')
i++;
s1 = s1.substr(i, s1.length());

i = 0;
while (s2[i] == '0')
i++;
s2 = s2.substr(i, s2.length());
if (s1.length() < s2.length()) {
swap(s1, s2); haha = 1;
}
//cout << s1 <<" "<< s2;
s3 = s1;
for (i = s1.length() - 1; i >= 0; i--) {
j = s2.length() - s1.length() + i;
if (j < 0) {
j = 1;
s2[j] = '0';
}
if ((s1.length() - i) % 2 == 1) {
temp = ((s1[i] - '0') + (s2[j] - '0')) % 13;
if (temp >= 10)
s3[i] = p[temp - 10];
else
s3[i] = temp + '0';
}
else if (haha == 1) {
temp = s1[i] - s2[j];
if (temp < 0)
temp += 10;
s3[i] = temp + '0';
}
else {
temp = s2[j] - s1[i];
if (temp < 0)
temp += 10;
s3[i] = temp + '0';
}

}
cout << s3;
}

感想:

1.注意,当B位数小于A的时候,高位默认为0(不知道是怎么默认的,反正我是被坑了有一个小时)

2.奇数偶数位是从个位开始算了

注意到这两点直接死算就出来了

a8a3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat-数据结构