牛客网刷题第一天 | HJ29 字符串加解密、HJ31 单词倒排

HJ29 字符串加解密

大写字母的 ASCII 码值范围是 65 到 90,而小写字母的 ASCII 码值范围是 97 到 122。

然看清题目要求

输入描述:

第一行输入一串要加密的密码
第二行输入一串加过密的密码

输出描述:

第一行输出加密后的字符
第二行输出解密后的字符

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //输出加密
        String str = in.nextLine();
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            if ( c >= 'a' && c <= 'z') {
                if (c == 'z') {
                    sb.append('A');
                } else {
                    sb.append((char)(c - 31));
                }
            } else if (c >= 'A' && c <= 'Z') {
                if (c == 'Z') {
                    sb.append('a');
                } else {
                    sb.append((char)(c + 33));
                }
            } else if (c >= '0' && c <= '9') {
                if (c == '9') {
                    sb.append('0');
                } else {
                    sb.append((char)(c + 1));
                }
            }
        }
        System.out.println(sb);
        //输出解密
        String str1 = in.nextLine();
        for (char c : str1.toCharArray()) {
            if ( c >= 'a' && c <= 'z') {
                if (c == 'a') {
                    System.out.print('Z');
                } else {
                    System.out.print((char)(c-33));
                }
            } else if (c >= 'A' && c <= 'Z') {
                if (c == 'A') {
                    System.out.print('z');
                } else {
                    System.out.print((char)(c+31));
                }
            } else if (c >= '0' && c <= '9') {
                if (c == '0') {
                    System.out.print('9');
                } else {
                    System.out.print((char)(c - 1));
                }
            }
        }
    }
}

HJ31 单词倒排

读取字符将非法字符转换为空格,根据空格分割字符字符串,存储到字符串数组中,倒序输出字符串数组

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            String str = in.nextLine();
            StringBuilder sb = new StringBuilder();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (Character.isLetter(chars[i])) {
                    sb.append(chars[i]);
                } else {
                    sb.append(' ');
                }
            }
            String[] arr = sb.toString().split(" ");
            for (int i = arr.length  - 1; i >= 0; i--) {
                System.out.print(arr[i] + " ");
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值