B1048. 数字加密(20)

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
想了很久才AC掉,方法很粗暴,很迂回,是题集里第一个AC的,但昨天的考试肯定有人当场就AC掉的。。。由于最后用char数组的,最后一个测试点是当 B的数字比A短时,对应B的位置是空即为0,如果对应A的位置也是0的话,则要特殊处理下


#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;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值