首页 > 试题广场 >

密码翻译

[编程题]密码翻译
  • 热度指数:26728 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍然能防止情报被轻易的识别。我们给出一种最简的的加密方法,对给定的一个字符串,把其中从a-y,A-Y的字母用其后继字母替代,把z和Z用a和A替代,则可得到一个简单的加密字符串。

输入描述:
读取这一行字符串,每个字符串长度小于80个字符


输出描述:
对于每组数据,输出每行字符串的加密字符串。
示例1

输入

Hello! How are you!

输出

Ifmmp! Ipx bsf zpv!
示例2

输入

zzz

输出

aaa
#include<bits/stdc++.h>
using namespace std;
int main() {
	string input;
	while (cin >> input) {
		for (int i = 0; i < input.size(); i++) {
			if (input[i] == 'Z' || input[i] == 'z') {
				input[i] = input[i] - 'z' + 'a';
			}
			if (input[i] < 'z'&&input[i] >= 'a') {
				input[i] ++;
			}
			if (input[i] < 'Z'&&input[i] >= 'A') {
				input[i] ++;
			}		
		}
		cout << input<<" ";
	}
	return 0;
}

发表于 2020-02-19 13:16:19 回复(3)
这一题主要是对于字符串的处理,由于涉及到空格,应该用getline(cin,str)的方式读取,并且输入应该用string类型存储。其他的还有注意字符串转化为可以方便操作的char*型,应该用strcpy+s.c_str()
其他的就注意只有字母需要变化,其他的不变,所以对变化范围做一下限制。
整体比较简单,就是对字符串的读写、以及字符串各种操作(string和char*互转等)需要熟悉即可
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main() {
    string s;
    getline(cin, s);
    char ch[80];
    strcpy(ch, s.c_str());
    for (int i = 0; i < s.length(); i++) {
        if ((ch[i] >= 'a'&&ch[i] <= 'z') || ch[i] >= 'A'&&ch[i] <= 'Z')
            ch[i]++;
    }
    for (int i = 0; i < s.length(); i++)
        cout << ch[i];
    return 0;
}

发表于 2019-02-07 09:21:43 回复(3)

import java.util.Scanner;

/**

  • Created by wanyu on 2018/3/19.
    */
    public class K {
    public static void main(String[] args) {

     Scanner cin=new Scanner(System.in);
     String str=cin.nextLine();
     char a[]=str.toCharArray();
     for(int i=0;i<a.length;i++){
         int k=a[i];
         char b=1;
         if((k>=65&&k<=90)||(k>=97&&k<=122)){
              b=(char)(++k);
             System.out.print(b);
         }else {
             System.out.print(a[i]);
         }
    
     }
    

    }
    }

发表于 2018-03-19 15:40:17 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        char[] array = s.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : array) {
            if (c>='a'&&c<'z') builder.append((char)(c+1));
            else if (c>='A'&&c<'Z') builder.append((char)(c+1));
            else if (c=='Z') builder.append('A');
            else if (c=='z') builder.append('a');
            else builder.append(c);
        }
        System.out.println(builder.toString());
    }
}


发表于 2020-03-17 17:16:21 回复(0)
//收集一行字符串且中间有空格时,
//用getline(cin,string)
#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int CaseNumber;
    string str;

        while(getline(cin,str)){
            for(int i=0; i<str.size(); i++){
                if(str[i]=='z'){
                    str[i] = 'a';
                }
                else if(str[i]=='Z'){
                    str[i] = 'A';
                }
                else if(str[i]>='a'&&str[i]<='y'){
                    str[i] += 1;
                }
                else if(str[i]>='A'&&str[i]<='Y'){
                    str[i] += 1;
                }
            }
            cout<<str<<endl;
        }
    return 0;
}

发表于 2020-02-14 20:29:27 回复(2)
#include <iostream>
using namespace std;
int main()
{
	string str;
	getline(cin,str);
	for(int i=0;i<str.length();i++)
	{
		if((str[i]>='A'&&str[i]<='Y')||(str[i]>='a'&&str[i]<='y'))
			str[i]++;
		else if(str[i]=='Z'||str[i]=='z')
			str[i]-=35;
		}
	cout<<str;
 }

发表于 2020-02-14 11:39:50 回复(1)
try:
    while True:
        string = list(input())
        for i in range(len(string)):
            if string[i].isalpha():
                if string[i] == 'z':
                    string[i] = 'a'
                elif string[i] == 'Z':
                    string[i] = 'Z'
                else:
                    string[i] = chr(ord(string[i])+1)
        print(''.join(string))
except Exception:
    pass
编辑于 2018-10-09 09:39:34 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{     string st;     while(getline(cin,st))     {         for(int i=0;i<st.size();i++)         {             if(st[i]>='a'&&st[i]<='y')             st[i]++;             else if(st[i]>='A'&&st[i]<='Y')             st[i]++;             else if(st[i]=='z')             st[i]='a';             else if(st[i]=='Z')             st[i]='A';         }         cout<<st<<endl;     }     return 0;
}

发表于 2018-08-26 20:31:11 回复(1)
可以强行用一波正则
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
         Scanner scan=new Scanner(System.in);
         String str=scan.nextLine();
        Pattern p=Pattern.compile("[a-yA-Y]|[zZ]");
        Matcher m=p.matcher(str);
        StringBuffer sb=new StringBuffer();
        while(m.find()){
            if(m.group().matches("[zZ]")){
                m.appendReplacement(sb,(char)(m.group().charAt(0)-25)+"");
            }else{
                m.appendReplacement(sb,(char)(m.group().charAt(0)+1)+"");
            }
        }
        m.appendTail(sb);
        System.out.println(sb);
    }
}

发表于 2018-03-31 15:32:30 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        while(input.hasNext()){
            String str=input.nextLine();
            System.out.println(Tran(str));
        }
    }
    public static String Tran(String str){
        char c[]=str.toCharArray();
        StringBuffer buf=new StringBuffer();
        for(int i=0;i<c.length;i++){
            if(c[i]=='Z'){
                buf.append("A");
            }else if(c[i]=='z'){
                buf.append("a");
            }else if((c[i]>='a'&&c[i]<='y')||(c[i]>='A'&&c[i]<='Y')){
                buf.append((char)(c[i]+1));
            }else{
                buf.append(c[i]);
            }
        }
        return String.valueOf(buf);
    }
}

发表于 2018-03-01 12:57:25 回复(0)
package com.speical.improve;

import java.util.Scanner;

/** 
*
* @author special
* @date 2017年12月25日 上午11:07:52
*/
public class Pro32Improve1 {

    private static boolean isUpper(char ch) { return ch >= 'A' && ch <= 'Z'; }

    private static boolean isLower(char ch) { return ch >= 'a' && ch <= 'z'; }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            int n = input.nextInt();
            input.nextLine();
            while(n-- > 0){
                String password = input.nextLine();
                for(int i = 0; i < password.length(); i++){
                    char letter = password.charAt(i);
                    if(isUpper(letter)){
                        System.out.print((char)(letter != 'Z' ? letter + 1 : 'A'));
                    }else if(isLower(letter)){
                        System.out.print((char)(letter != 'z' ? letter + 1 : 'a'));
                    }else {
                        System.out.print(letter);
                    }
                }
                System.out.println();
            }
        }
    }

}
发表于 2017-12-25 11:11:59 回复(0)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

const int maxn = 80;

char ch[maxn] = {0};

int main()
{
    
    int n, len;
    while(gets(ch))
    {
        n = atoi(ch);
        while(n--)
        {
            gets(ch);
            len = strlen(ch);
            for(int i = 0; i < len; ++i)
            {
                // 'a'-'y' 和 'A'-'Y'
                if((ch[i] >= 'a' && ch[i] <= 'y') || (ch[i] >= 'A' && ch[i] <= 'Y'))
                {
                    cout << (char)(ch[i]+1);
                }
                else if(ch[i] == 'z')
                {
                    cout << 'a';
                }
                else if (ch[i] == 'Z')
                {
                    cout << 'Z';
                }
                else
                {
                    cout << ch[i];
                }
            }
            cout << endl;
        }
    }

    return 0;
}


发表于 2016-08-05 10:18:44 回复(1)

本套6道题全部pass的C++代码已挂到了我的GitHub(https://github.com/shiqitao/NowCoder-Solutions)
牛客网上的其他题目解答也在持续更新。
【题解】简单的字符串处理。

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char str[80];
    gets(str);
    for (int i = 0; i < strlen(str); i++) {
        if ((str[i] >= 'a'&&str[i] < 'z') || (str[i] >= 'A'&&str[i] < 'Z')) {
            str[i] += 1;
        }
        else if (str[i] == 'z' || str[i] == 'Z') {
            str[i] -= 25;
        }
    }
    cout << str;
    return 0;
}
编辑于 2018-03-20 11:26:30 回复(0)
importjava.util.Scanner;
publicclassMain {
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
//System.out.println("input secret:");
Scanner input = newScanner(System.in);
while(input.hasNext()){
String secret = input.nextLine();
transCode(secret);
}
input.close();
}
publicstaticvoidtransCode(String secret){
for(inti = 0; i<secret.length();i++){
charch = secret.charAt(i);
if(ch>='a'&&ch<='z')
System.out.print((char)(ch=='z'?'a':ch+1));
elseif(ch>='A'&&ch<='Z')
System.out.print((char)(ch=='Z'?'a':ch+1));
else
System.out.print(ch);
}
}
}
编辑于 2018-03-19 09:40:42 回复(0)
送分题,细节不出错就行
#include <stdio.h>
#include <string.h>
#define N 100

int main()
{
    char s[N];
    while(gets(s))
    {
        int len=strlen(s);
        for(int i=0; i<len; i++)
        {
            if('a'<=s[i]&&s[i]<='y') s[i]++;
            else if('A'<=s[i]&&s[i]<='Y') s[i]++;
            else if(s[i]=='z') s[i]='a';
            else if(s[i]=='Z') s[i]='A';
        }
        puts(s);
    }
    return 0;
}

发表于 2018-02-21 21:19:04 回复(0)

python solution:



while True:
    try:
        a=int(input())
        for i in range(a):
            b=input()
            res=""
            for i in b:
                if i.isalpha():
                    if ord(i)!=122 and ord(i)!=90:
                        res+=chr(ord(i)+1)
                    else:
                        res+=chr(ord(i)-25)
                else:
                    res+=i
            print(res)



    except:
        break
发表于 2017-10-04 08:18:04 回复(1)
//这个题有点问题 题目给的样例中第一行是一个数字,但是输出却没处理数字  我以为是表示要输入的数据规模,结果怎么搞
//都是运行时间超过限制,题目的测试案例应该是一行行的字符串 根本没数据规模的数字
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
    char c;
    while((c = getchar())!='\n'){
        if(isalpha(c)){
            if(c!='z'&&c!='Z'){
                putchar(c+1);
            }else putchar(c=='z'?'a':'A');
        }else putchar(c);
    }
    putchar('\n');
    return 0;
}

发表于 2020-02-20 12:31:18 回复(1)
不管是空格还是换行都逐个输入。若不是字母直接输出,可以保持空格和换行的一致。
#include<iostream>
using namespace std;
int main(){
	char ch;
	while(cin.get(ch)){
		if(ch=='z') cout<<"a";
		else if(ch=='Z') cout<<"A";
		else if(('a'<=ch&&ch<'z')||('A'<=ch&&ch<'Z')) cout<<char(ch+1);
		else cout<<ch;
	} 
	
	return 0;
}

发表于 2020-04-22 18:49:31 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string line2="bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA";
    string a;
    while(getline(cin,a)){
    for(int i=0;i<a.length();i++){
        if(line1.find(a[i])==-1){
        }else{
        a[i]=line2[line1.find(a[i])];
        }
    }
    cout<<a<<endl;
    }
    return 0;
}

发表于 2019-06-16 14:42:48 回复(0)
#include<cstring>
(803)#include<iostream>
using namespace std;


int main() {
    string s;
    getline(cin, s);
    for(int i=0; i<s.size(); ++i)
        s[i] = (s[i]==90 || s[i]==123)?s[i]-25:((s[i]>=65 && s[i]<90) || (s[i]>=97 && s[i]<123))?s[i]+1:s[i];
    cout<<s<<endl;
}
C++核心一行
发表于 2020-04-30 11:11:02 回复(1)