实验7-1-9 数字加密

题目:

实验7-1-9 数字加密 (15分)

题目要求:

输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。例如输入1257,经过加9取余后得到新数字0146,再经过两次换位后得到4601。

输入格式:

输入在一行中给出一个四位的整数x,即要求被加密的数。

输出格式:

在一行中按照格式“The encrypted number is V”输出加密后得到的新数V。

输入样例:

1257

输出样例:

The encrypted number is 4601

解题代码:

#include<stdio.h>
int main()
{
	int x;
	scanf( "%d", &x );
	
	int a[4], i = 0;
	
	do
	{
		a[i] = x % 10;
		x = x / 10;	
		i++;
	}while( x > 0 );
	
	if( i < 4 )
	{
		while( i < 4 )
		{
			a[i] = 0;
			i++;
		}
	}
	
	for( i = 0; i < 4; i++ )
	{
		a[i] = ( a[i] + 9 ) % 10;
	}

	int n;
	
	n = a[1] * 1000 + a[3] * 10 + a[2] * 1 + a[0] * 100;
	
	if( a[1] == 0 )
	printf( "The encrypted number is 0%d\n", n );
	
	else if( a[1] == 0 && a[3] == 0 )
	printf( "The encrypted number is 00%d\n", n );
	
	else if( a[1] == 0 && a[3] == 0 && a[2] == 0 )
	printf( "The encrypted number is 000%d\n", n );
	
	else if( a[1] == 0 && a[3] == 0 && a[2] == 0 && a[0] == 0 )
	printf( "The encrypted number is 000%d\n", n );
	
	else
	printf( "The encrypted number is %d\n", n );
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值