您的位置:首页 > 编程语言 > C语言/C++

1048. 数字加密(20)(C++)

2018-02-12 11:09 453 查看
本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。输入格式:输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。输出格式:在一行中输出加密后的结果。输入样例:
1234567 368782971
输出样例:
3695Q8118
#include<iostream>
#include<cstdio>
#include<stack>
#include<string>
using namespace std;
char odd(char a1,char b1){
int a2=a1-'0';
int b2=b1-'0';
int t2=(a2+b2)%13;
char t3;
if(t2==10)
t3='J';
else if(t2==11)
t3='Q';
else if(t2==12)
t3='K';
else
t3=t2+'0';
return t3;
}
char even(char a1,char b1){
int a2=a1-'0';
int b2=b1-'0';
int t2;
bool flag=b2<a2?1:0;
t2=b2-a2+10*flag;
return t2+'0';
}
string a,b;
int main(){
cin>>a>>b;
char t;
stack<char>s;
int lena=a.length(),lenb=b.length();
if(lena>=lenb){
int i=1;
for(;i<=lenb;i++){
if(i%2!=0){
t=odd(a[lena-i],b[lenb-i]);
s.push(t);
}
if(i%2==0){
t=even(a[lena-i],b[lenb-i]);
s.push(t);
}
}
for(int j=lena-lenb;j>0;j--){
if(i%2!=0){
t=odd(a[j-1],'0');
s.push(t);
}
if(i%2==0){
t=even(a[j-1],'0');
s.push(t);
}
i++;
}
}
if(lena<lenb){
for(int i=1;i<=lena;i++){
if(i%2!=0){
t=odd(a[lena-i],b[lenb-i]);
s.push(t);
}
if(i%2==0){
t=even(a[lena-i],b[lenb-i]);
s.push(t);
}
}
for(int j=lenb-lena;j>0;j--){
t=b[j-1];
s.push(t);
}
}
while(!s.empty()){
cout<<s.top();
s.pop();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: