2144 - qp kjf ejbo cbp

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

指挥部截获一份机密电报,已知电报英文部分的加密算法是将每个英文字母的ASCII码加上某个数n变成另一个英文字母,若加上这个数后大于zZ,则超出部分重新从aA开始算起,如n = 4;则E变成I,‘y’变成c’Z’变成’D’。但是这个n到底是多少呢?指挥部要求你设计一个程序:输入电报内容和一个整数n,输出解密后的内容。

题目输入

首先输入一串字符(长度<=1000),字符可以是大小写英文字母、数字、标点符号、运算符或其他制表符,下一行输入一个整数nn的含义见上述)。以输入“END”结束,“END”不做处理。

题目输出

输出解密后的内容,每个输出后面有一个空行

输入/输出样例

输入格式

IcD 1*2=3~@4%#5/>6
4
qp kjf ejbo cbp
1
END

输出格式

EyZ 1*2=3~@4%#5/>6

po jie dian bao

C语言解答

#include<stdio.h>
#include<string.h>
char judge(char a,int n)
{
		if(a>='a'&&a<='z')
		{
		if(a-n>='a')
		return a-n;
		else
		{
			if(a-n%26>='a')
			return a-n%26;
			else
			return 'z'-('a'-(a-n%26))+1;
		}
		}
		else if(a>='A'&&a<='Z')
		{
		if(a-n>='A')
		return a-n;
		else
		{
			if(a-n%26>='A')
			return a-n%26;
			else
			return 'Z'-('A'-(a-n%26))+1;
		}
		}
		else 
		return a;
}
main()
{
	char ch[1024],tmp;
	int n,i;
	while(gets(ch)!=NULL)
	{
		if(ch[0]=='E'&&ch[1]=='N'&&ch[2]=='D'&&strlen(ch)==3)
		break;
		scanf("%d",&n);
		getchar();
		for(i=0;i<strlen(ch);i++)
		ch[i]=judge(ch[i],n);
		printf("%s\n\n",ch);
	}
}

C++解答

#include <iostream>
#include <string>
using namespace std;

void Dn(string & , int );

int main()
{
	string str;
	string end = "END";
	int move;
	while (1){
		getline(cin, str);
		if (str == end)
			break;
		cin >> move;
		cin.get();
		Dn(str, move);
		cout << str << endl << endl;
	}
}

void Dn(string & str, int move){
	int len = str.size();
	if (len > 1000)
		len = 1000;
	move %= 26;
	for (int i = 0; i < len; i++){
		char c = str[i];
		if ( c >= 'a' && c <= 'z' ){
			c -= move;
			if (c < 97)
				c = 122 - (96 - c);
		}
		if ( c >= 'A' && c <= 'Z' ){
			c -= move;
			if (c < 65)
				c = 90 - (64 - c);
		}
		str[i] = c;
	}
}