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<cstring>
using namespace std;
int main()
{
char c[101],d[101];
// gets(a); gets(b);
cin>>c>>d;
int i=0,j=0,len1=strlen(c),len2=strlen(d);
int a[101]={0},b[101]={0};
char ans[101]={'0'};
int cnt=0;
for(i=0; i<len1; i++)
{
// a[i]=c[len1-1-i]-'0';
a[i]=c[i]-'0';
// cout<<a[i];
}
//cout<<" ";
for(j=0; j<len2; j++)
{
//b[j]=d[len2-1-j]-'0';
b[j]=d[j]-'0';
//cout<<b[j];
}
if(len1<=len2)
{
for(i=len1-1,j=len2-1; i>=0,j>=len2-len1; i--,j--)
{
if((len1-i)%2 && (len2-j)%2)
{
int sum=a[i]+b[j];
if(sum%13<10)
{
ans[++cnt]=sum%13+'0';
//cnt++;
}
else if(sum%13==10)
ans[++cnt]='J';
else if(sum%13==11)
ans[++cnt]='Q';
else if(sum%13==12)
ans[++cnt]='K';
}
else if((len1-i)%2==0 && (len2-j)%2==0)
{
int cha=b[j]-a[i];
if(cha<0)
ans[++cnt]=cha+10+'0';
else if(cha>=0)
ans[++cnt]=cha+'0';
}
}
for(int k=len2-len1-1; k>=0; k--)
{
ans[++cnt]=b[k]+'0';
}
}
else
{
for(i=len1-1,j=len2-1; j>=0,i>=len1-len2; i--,j--)
{
if((len1-i)%2 && (len2-j)%2)
{
int sum=a[i]+b[j];
if(sum%13<10)
ans[++cnt]=sum%13+'0';
else if(sum%13==10)
ans[++cnt]='J';
else if(sum%13==11)
ans[++cnt]='Q';
else if(sum%13==12)
ans[++cnt]='K';
}
else if((len1-i)%2==0 && (len2-j)%2==0)
{
int cha=b[j]-a[i];
if(cha<0)
ans[++cnt]=cha+10+'0';
else if(cha>=0)
ans[++cnt]=cha+'0';
}
}
for(i=len1-len2-1; i>=0; i--)
{
if((len1-i)%2==0)
{
if(a[i]==0)
ans[++cnt]='0';
else
ans[++cnt]=10-a[i]+'0';
}
else
ans[++cnt]=a[i]+'0';
}
}
//cout<<cnt<<endl;
for(i=cnt;i>=1;i--)
{
cout<<ans[i];
}
return 0;
}
后来一直没时间好好改下,今天看到别人的代码,才发现自己的好low....
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string A,B,sum;
cin>>A>>B;
int i = A.length()-1, j = B.length()-1,count = 1;
if(i<j)
{
int c = j - i;
while(c!=0)
{
A = '0'+A;
c--;
}
}
else
{
int c = i - j;
while(c!=0)
{
B = '0'+B;
c--;
}
}
i = A.length()-1, j = B.length()-1;
while(i!=-1&&j!=-1)
{
if(count%2==1)
{
sum = "0123456789JQK"[(A[i]+B[j]-'0'-'0')%13]+sum;
}else{
sum = "1234567890123456789"[(B[j]-'0'-(A[i]-'0'))+9] +sum;
}
i--;j--;count++;
}
cout<<sum<<endl;
return 0;
}