B1048 数字加密

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110;
char a[maxn],b[maxn];
char ans[maxn]={0};
int main()
{
	scanf("%s %s",a,b);
	int lenA=strlen(a);
	int lenB=strlen(b);
	reverse(a,a+lenA);
	reverse(b,b+lenB);
	int chang=max(lenA,lenB);
	for(int i=0;i<chang;i++)
	{
		int numA=i<lenA?a[i]-'0':0;
		int numB=i<lenB?b[i]-'0':0;
		if(i%2==0)
		{
			int temp=(numA+numB)%13;
			if(temp==10) ans[i]='J';
			else if(temp==11) ans[i]='Q';
			else if(temp==12) ans[i]='K';
			else ans[i]=temp+'0';///****
			
		}else if(i%2==1)
		{
			int temp=numB-numA;
			if(temp<0) temp+=10;
			ans[i]=temp+'0';	
		}
	}
	reverse(ans,ans+strlen(ans));
	puts(ans);
	return 0;
}

总结:

1.本题使用标准库函数reverse,max,注意添加头部

2.个位为第一位,即下标为偶对应奇数位,正好倒过来

3,定义的是字符数组,想反转不能用a.begin(),直接用a即可

4.ans是字符数组,故将求出的整数放入时,+‘0’不能丢

5.对于字符串长度不等,短的应用0补上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值