java 洛谷题单【入门5】字符串

P5733 【深基6.例1】自动修正

import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        String s = input.next();
        System.out.println(s.toUpperCase(Locale.ROOT));
 
    }
}

P1914 小书童——凯撒密码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = input.nextInt();
        String s = input.next();

        for (int i = 0; i < s.length(); i++) {

            int a = s.charAt(i) - 0;
            a += n;
            if (a > 122){
                a -= 26;
            }
            System.out.print((char) (a));
        }
    }
}

P1125 [NOIP2008 提高组] 笨小猴

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String s = input.next();
        int max = -1;int min = 1000000;
        int[] a = new int[26];

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            a[c - 97]++;
        }
        for (int i = 0; i < 26; i++) {
            if (a[i] < min && a[i] != 0){
                min = a[i];
            }
        }
        for (int i = 0; i < 26; i++) {
            if (a[i] > max){
                max = a[i];
            }
        }
        if (isPrime(max - min)){
            System.out.println("Lucky Word");
            System.out.println(max - min);
        }else {
            System.out.println("No Answer");
            System.out.println(0);
        }

    }

    public static boolean isPrime(int num) {
        if (num <= 3) return num > 1;
        if (num % 6 != 1 && num % 6 != 5){
            return false;
        }
        int sqrt = (int) Math.sqrt(num);
        for (int i = 5; i <= sqrt; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0){
                return false;
            }
        }
        return true;
    }
}

P1957 口算练习题


import java.util.Scanner;

/**
 * @Author HeShen.
 * @Date 2024/1/6 20:45
 */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char a = ' ';
        int n, c, d;
        String s, b;
        n = input.nextInt();
        for (int i = 0; i < n; i++) {
            b = input.next();
            if (b.charAt(0) >= 'a' && b.charAt(0) <= 'z') {
                a = b.charAt(0);
                c = input.nextInt();
                d = input.nextInt();
            } else {
                c = Integer.parseInt(b);
                d = input.nextInt();
            }
            s = "";
            if (a == 'a')
                s = c + "+" + d + "=" + (c + d);
            else if (a == 'b')
                s = c + "-" + d + "=" + (c - d);
            else if (a == 'c')
                s = c + "*" + d + "=" + (c * d);
            System.out.println(s);
            System.out.println(s.length());
        }
    }
}

P5015 [NOIP2018 普及组] 标题统计

import java.util.Scanner;

/**
 * @Author HeShen.
 * @Date 2024/1/6 20:45
 */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        char[] s = new char[100];
        String a = input.nextLine();
        int count = 0;

        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) >= 'A' && a.charAt(i) <= 'Z') {
                count++;
            }
            if (a.charAt(i) >= 'a' && a.charAt(i) <= 'z') {
                count++;
            }
            if (a.charAt(i) >= '0' && a.charAt(i) <= '9') {
                count++;
            }
        }
        System.out.println(count);
    }
}

P5734 【深基6.例6】文字处理软件

import java.util.Scanner;

/**
 * @Author HeShen.
 * @Date 2024/1/6 20:45
 */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n = input.nextInt();

        //java求子串的方法:substring(begin, end),不取到end
        //end参数省略时,取到结束

        StringBuilder str = new StringBuilder(input.next());

        int index;
        for (int i = 0; i < n; i++) {
            String ans = input.next();
            if (ans.charAt(0) == '1'){
                String tem = input.next();
                str.append(tem);
                System.out.println(str);
            }
            else if (ans.charAt(0) == '2'){
                int a = input.nextInt();
                int b = input.nextInt();
                str = new StringBuilder(str.substring(a, a + b));
                System.out.println(str);
            }
            else if (ans.charAt(0) == '3'){
                int a = input.nextInt();
                //s是3方法中的str
                //这里用next,用nextLine会把空格也算上
                String s = input.next();
                //如果a=0,就是直接在原字符串开头加上字符串就行
                if (a == 0){
                    str = new StringBuilder(s + str);
                    System.out.println(str);
                }else {
                    //保留前半部分
                    String temp = str.substring(a);
                    str = new StringBuilder(str.substring(0, a));

                    str.append(s);
                    str.append(temp);
                    System.out.println(str);
                }
            }
            else if (ans.charAt(0) == '4') {
                String tem = input.next();
                index = str.indexOf(tem);
//                if (str.equals("")){
//                    index = str.indexOf(tem);
//                }else
//                    index = str.indexOf(tem);
                if (tem.length() <= str.length()){
                    System.out.println(index);
                }else {
                    System.out.println(-1);
                }
            }
        }
    }
}

P1308 [NOIP2011 普及组] 统计单词数

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int t = 0, tt = 0;
        String ss = input.nextLine();
        String s = input.nextLine();
        
        for (int i = 0; i <= s.length() - ss.length(); i++) {
            boolean match = true;
            for (int j = 0; j < ss.length(); j++) {
                if (Character.toUpperCase(s.charAt(j + i)) != Character.toUpperCase(ss.charAt(j))) {
                    match = false;
                    break;
                }
                if (i > 0 && s.charAt(i - 1) != ' ') {
                    match = false;
                    break;
                }
            }
            if (match && (s.charAt(i + ss.length()) == ' ' || i + ss.length() == s.length())) {
                t++;
                if (t == 1) {
                    tt = i;
                }
            }
        }
        
        if (t == 0) {
            System.out.println("-1");
        } else {
            System.out.println(t + " " + tt);
        }
    }
}

P1765 手机

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String s = input.nextLine();
        int count = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                count++;
            }else if (s.charAt(i) == 'a' || s.charAt(i) == 'd' || s.charAt(i) == 'g' || s.charAt(i) == 'j'
                    || s.charAt(i) == 'm' || s.charAt(i) == 'p' || s.charAt(i) == 't' || s.charAt(i) == 'w'){
                count++;
            }else if (s.charAt(i) == 'b' || s.charAt(i) == 'e' || s.charAt(i) == 'h' || s.charAt(i) == 'k'
                    || s.charAt(i) == 'n' || s.charAt(i) == 'q' || s.charAt(i) == 'u' || s.charAt(i) == 'x'){
                count += 2;
            }else if (s.charAt(i) == 'c' || s.charAt(i) == 'f' || s.charAt(i) == 'i' || s.charAt(i) == 'l'
                    || s.charAt(i) == 'o' || s.charAt(i) == 'r' || s.charAt(i) == 'v' || s.charAt(i) == 'y'){
                count += 3;
            }else if (s.charAt(i) == 'z' || s.charAt(i) == 's'){
                count += 4;
            }
        }
        System.out.println(count);
    }
}

P3741 honoka的键盘

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        //这里会报多余的错误,但是删除后AC不了,
        //我也没搞懂,但是这样子就过了
        String a = input.nextLine();
        a = input.nextLine();
        int ans = 0;
        for (int i = 0; i < a.length() - 1; i++) {
            if (a.charAt(i) == 'V' && a.charAt(i + 1) == 'K') {
                ans++;
                // 修改字符串中的字符
                //Java中的字符串是不可变的,所以在修改字符串中的字符时
                //需要将其转换为字符数组,然后再转换回字符串
                char[] charArray = a.toCharArray();
                charArray[i] = 'X';
                charArray[i + 1] = 'X';
                a = String.valueOf(charArray);
            }
        }
        for (int i = 0; i < a.length() - 1; i++) {
            if (a.charAt(i) != 'X' && a.charAt(i) == a.charAt(i + 1)) {
                ans++;
                break;
            }
        }
        System.out.println(ans);
    }
}

P1321 单词覆盖还原

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int boy = 0, girl = 0;
        String str = input.next();
        for (int i = 0; i < str.length() - 2; i++) {
            if(str.charAt(i) == 'b' || str.charAt(i + 1) == 'o' || str.charAt(i + 2) == 'y') {
                boy++;
            }
        }
        for (int i = 0; i < str.length() - 3; i++) {
            if(str.charAt(i) == 'g' || str.charAt(i + 1) == 'i' || str.charAt(i + 2) == 'r' || str.charAt(i + 3) == 'l') {
                girl++;
            }
        }
        System.out.println(boy);
        System.out.println(girl);
    }
}

P1553 数字反转(升级版)


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.next();
        char c = 0; // 放符号
        int count = 0;
        //记录给出字符长度
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                count++;
            } else {
                //标记非数字,退出循环
                c = s.charAt(i);
                break;
            }
        }
        //记录第一个数末尾的后一个位置,就是符号的位置
        int x = count;
        count--;
        //去除字符中的前置0
        while (s.charAt(count) == '0' && count > 0) {
            count--;
        }
        //逆序输出前半部分字符
        for (int i = count; i >= 0; i--) {
            System.out.print(s.charAt(i));
        }
        //c=0即未出现特殊符号,直接退出程序
        if (c == 0) {
            return;
        } else if (c == '%') {
            //出现%,在%前的字符反转的情况下,再输出一个%,然后退出程序
            System.out.print(c);
            return;
        } else {
            //剩下 . 和 / 这两种情况都是一样的,去除前置0,反转字符
            System.out.print(c);
        }
        int m = s.length() - 1;
        //去除末尾0
        while (s.charAt(x + 1) == '0' && x < m - 1) {
            x++;
        }
        //去除前置0
        while (s.charAt(m) == '0' && m > x + 1) {
            m--;
        }
        //反转符号后半部分的字符
        for (int i = m; i > x; i--) {
            System.out.print(s.charAt(i));
        }
    }
}

P1603 斯诺登的密码

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static String[] dic = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "a", "both", "another", "first", "second", "third"};
    static int[] di = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 21, 44, 69, 96, 25, 56, 89, 24, 61, 0, 1, 4, 1, 1, 4, 9};
    static long[] a = new long[10];
    static int top, flag;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 6; i++) {
            //分割每个单词,遇到空格就停止
            String s = input.next();
            for (int j = 0; j < 26; j++) {
                if (s.equals(dic[j])) {
                    a[++top] = di[j];
                    break;
                }
            }
        }
        //排序,小的数在前
        Arrays.sort(a, 1, top + 1);
        for (int i = 1; i <= top; i++) {
            if (flag != 0) {
                System.out.printf("%02d", a[i]);
            } else {
                if (a[i] != 0) {
                    System.out.print(a[i]);
                    flag = 1;
                }
            }
        }
        if (flag == 0) {
            System.out.print("0");
        }
    }
}

P1200 [USACO1.1] 你的飞碟在这儿 Your Ride Is Here

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String str1 = input.next();
        String str2 = input.next();

        ready(str1, str2);

    }

    public static void ready(String str1, String str2) {
        int sum1 = 1, sum2 = 1;

        for (int i = 0; i < str1.length(); i++) {
            int x = str1.charAt(i) -64;
            sum1 *= x;
        }

        for (int i = 0; i < str2.length(); i++) {
            int x = str2.charAt(i) -64;
            sum2 *= x;
        }

        if (sum1 % 47 == sum2 % 47) {
            System.out.println("GO");
        }else System.out.println("STAY");
    }

}

P1597 语句解析

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] a = new int[4];
        int ans = 0;
        Scanner input= new Scanner(System.in);
        String str = input.next();

        //遍历字符串
        for (int i = 0; i < str.length(); i++) {
            if (i % 5 == 0) {
                if (str.charAt(i) == 'a') {
                    ans = 1;
                } else if (str.charAt(i) == 'b') {
                    ans = 2;
                } else {
                    ans = 3;
                }
            }

            //对数组元素赋值
            if (i % 5 == 3) {
                if (str.charAt(i) == 'a') {
                    a[ans] = a[1];
                } else if (str.charAt(i) == 'b') {
                    a[ans] = a[2];
                } else if (str.charAt(i) == 'c') {
                    a[ans] = a[3];
                } else {
                    //获取字符的int值
                    a[ans] = Character.getNumericValue(str.charAt(i));
                }
            }
        }

        System.out.println(a[1] + " " + a[2] + " " + a[3]);
    }
}

P1598 垂直柱状图

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] s = new int[26];
        String str ;
        char a;

        //处理每行的数据,统计26个字母的个数
        for(int i = 0;i < 4;i++) {
            str = input.nextLine();
            for(int j = 0; j < str.length(); j++) {
                a = str.charAt(j);
                if(a >= 'A' && a <= 'Z') {
                    s[a -'A']++;
                }
            }
        }

        //寻找出现的最大值
        int max=0;
        for(int i = 0; i < 26; i++) {
            if(s[i] > max) {
                max = s[i];
            }
        }

        //输出,从最高处输出
        for(int i = max; i > 0; i--) {
            for(int j = 0; j < 26; j++) {
                if(s[j] == i) {
                    s[j]--;
                    System.out.print("* ");
                }else
                    System.out.print("  ");
            }
            System.out.println();
        }
        
        //输出26个大写字母
        for (int i = 0; i < 26; i++) {
            System.out.print((char) (i + 'A') + " ");
        }
    }
}
  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeShen.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值