2019秋_计导练习5 G

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45746839/article/details/102758901

问题描述:
某个公司想用电话来传送数据,但又担心被人窃听。数据都是由四位数字组成的整数。为了使数据传送更加安全一些,公司要你编写一个加密数据和解密数据的程序。程序读取一个四位整数,然后按如下规则加密:每位数字都加上7,然后用和除以10的余数取代该数字;再把第1位与第3位交换,第2位与第4位交换。说明:将最高位视作第1位。如整数1234,则第1位是1,第4位是4。

输入与输出要求:
输入两个整数n和f,n代表待加密或解密的整数(0000<=n<=9999),f为标志整数,当f为1时代表将整数n加密,当f为0时代表将整数n解密。输出加密数字或解密数字,加密结果输出为“After encrypting the number is XXXX”, 解密结果输出为“After decrypting the number is XXXX”。

程序运行效果:
Sample 1:
1890 1↙
After encrypting the number is 6785↙
Sample 2:
6785 0↙
After decrypting the number is 1890↙

#include<stdio.h>
#include<string.h>
#include<math.h>
int main(){
	int n,p;
	scanf("%d %d",&n,&p);
	int i,t=n,xn,sum=0;
	for(i=0;i<4;i++){
		t=n/pow(10,i);
		xn=t%10;
		if(p==1){
			xn+=7;
			if(xn>=10) xn%=10;
		}
		if(p==0){
			if(xn<7) xn+=3;
			else xn-=7;
		}
		switch(i){//交换位数
			case 0:sum+=xn*100;
			break;
			case 1:sum+=xn*1000;
			break;
			case 2:sum+=xn;
			break;
			case 3:sum+=xn*10;
			break;
			default:break;
		}
	}
	if(p==1) printf("After encrypting the number is %04d\n",sum);
	else printf("After decrypting the number is %04d\n",sum);
	//注意是密码,位数不够时左边补0
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45746839/article/details/102758901
5G