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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int MaxN = 101;
void StrReverse(char str[],int &L)
{
int i = 0; char ch;
for (; str[i]; ++i);
for (int j = 0; j < i / 2; ++j)
{
ch = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = ch;
}
L = i;
}
int main()
{
#ifdef _DEBUG
//freopen("data.txt", "r+", stdin);
fstream cin("data.txt");
#endif // _DEBUG
char A[MaxN] = { 0 }, B[MaxN] = { 0 }, res[MaxN] = { 0 };//均开始初始化为0,对计算有便利
char S[] = { '0','1','2','3','4','5','6','7','8','9','J','Q','K' },a,b;
int alen,blen,maxl;
cin >> A >> B;
StrReverse(A, alen), StrReverse(B, blen);
maxl = (alen > blen) ? alen : blen;
for (int i = 0; i < maxl; ++i)
{
a = A[i] ? A[i] : '0';
b = B[i] ? B[i] : '0';
if (!(i % 2))//奇数位
{
res[i] = S[(a + b - 2 * '0') % 13];
}
else
{
res[i] = (b - a) >= 0 ? ((b - a + '0')):((b - a + '0' + 10));
}
}
StrReverse(res, maxl);
printf("%s", res);
#ifdef _DEBUG
cin.close();
#ifndef _CODEBLOCKS
system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG
return 0;
}