首页 > 试题广场 >

最难的问题

[编程题]最难的问题
  • 热度指数:4570 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
NowCoder生活在充满危险和阴谋的年代。为了生存,他首次发明了密码,用于军队的消息传递。假设你是军团中的一名军官,需要把发送来的消息破译出来、并提
供给你的将军。
消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A 都分别替换成字母F),其他字符不 变,并且消息原文的所有字母都是大写的。密码中的字母与原文中的字母对应关系如下。
密码字母:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
原文字母:V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

输入描述:
输入包括多组数据,每组数据一行,为收到的密文。
密文仅有空格和大写字母组成。


输出描述:
对应每一组数据,输出解密后的明文。
示例1

输入

HELLO WORLD<br/>SNHJ

输出

CZGGJ RJMGY<br/>NICE
// write your code here
import java.util.*;

public class Main{
    
        public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String str2 = "VWXYZABCDEFGHIJKLMNOPQRSTU";
        while (scanner.hasNextLine()) {
            String des = scanner.nextLine();
            String ret = "";
            for (int i = 0; i < des.length(); i++) {
                char ch = des.charAt(i);
                if(ch==' '){
                    ret+=' ';
                    continue;
                }
                for (int j = 0; j < str1.length(); j++) {
                    if (ch == str1.charAt(j)) {
                        ret += str2.charAt(j);
                    }
                }
            }
            System.out.println(ret);
        }
    }

}
发表于 2022-09-10 01:22:42 回复(0)
// write your code here
import java.util.*;
public class Main{
    public static void main(String[] args){
       //密码字母:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
       //原文字母:V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
       //输入密码 输出明文!
        //对照表:
        String table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            StringBuilder sb = new StringBuilder();
            for(int i = 0;i<str.length();i++){
                if(str.charAt(i)==' '){//空格直接保存!
                    sb.append(' ');
                    continue;
                }
                 int index =(table.indexOf(str.charAt(i))-5+26)%26;
                //我们先找到这个密文在table的下标 - 5后得到明文位置下标,
                //有可能,下标会向前越界所以+26 最后%26反正向后越界!
                //我们就得到了对应明文的下标位置!
                sb.append(table.charAt(index));
            }
            System.out.println(sb);
        }
    }
}

发表于 2022-05-13 20:31:07 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            char[] ch = str.toCharArray();
            StringBuilder sb = new StringBuilder();
            for(int i = 0;i < ch.length;i++){
                if(ch[i] >= 'A' && ch[i] <= 'E'){
                    sb.append((char)((int)ch[i] + 21));
                }else if(ch[i] > 'E' && ch[i] <= 'Z'){
                    sb.append((char)((int)ch[i] - 5));
                }else{
                    sb.append(" ");
                }
            }
            System.out.println(sb.toString());
        }
    }
}

应用StringBulider
编辑于 2022-05-08 16:33:33 回复(0)
package mycode.array;

import java.util.Scanner;

public class Miyao {

    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         while(sc.hasNext()){
             String Value=sc.nextLine();
             System.out.println(Value);
             char [] s=Value.toCharArray();
             for(int i=0;i<s.length;i++){
                 char aa=s[i];
                 if(aa>='F') aa=(char)(aa-5);
                 if(aa<'F') aa=(char)(aa+21);
                 s[i]=aa;
             }
             System.out.print(new String(s));
         }
        

    }

}


为什么我这个输出都没有空格了呀
发表于 2020-01-27 17:04:34 回复(0)
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  StringBuffer sb = new StringBuffer();
  while (scanner.hasNextLine()) {
   char[] arr = scanner.nextLine().toCharArray();
   for (int i = 0; i < arr.length; i++) {
    if (arr[i] >= 70 && arr[i] <= 90) {
     sb.append((char)(arr[i] - 5));
    } else if (arr[i] >= 65 && arr[i] <= 70) {
     sb.append((char)(arr[i] + 21));
    } else {
     sb.append(arr[i]);
    }
   }
   System.out.println(sb.toString());
   sb = new StringBuffer();
  }
  scanner.close();
 }
}
发表于 2018-03-29 16:19:46 回复(0)