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

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. String s = sc.nextLine();
  6. System.out.println(s.toUpperCase());
  7. }
  8. }

P1914 小书童——凯撒密码

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int n = Integer.parseInt(sc.nextLine()) % 26;
  6. char[] raw = sc.nextLine().toCharArray();
  7. for (int i = 0; i < raw.length; i++) {
  8. int temp = raw[i] + n - 'z';
  9. if(temp<=0){
  10. raw[i] += n;
  11. }else {
  12. raw[i] = (char)('a' + temp - 1);
  13. }
  14. }
  15. System.out.println(new String(raw));
  16. }
  17. }

P1125 [NOIP2008 提高组] 笨小猴

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5. int[] table = new int[26];
  6. char[] chs = sc.nextLine().toCharArray();
  7. for(char c : chs){
  8. table[c-'a']++;
  9. }
  10. int min = 1000;
  11. int max = 1;
  12. for(int i : table){
  13. if(i>max){
  14. max = i;
  15. }
  16. if(i<min&&i!=0){
  17. min = i;
  18. }
  19. }
  20. if(isPrime(max-min)){
  21. System.out.println("Lucky Word");
  22. System.out.println(max-min);
  23. }else {
  24. System.out.println("No Answer");
  25. System.out.println(0);
  26. }
  27. }
  28. public static boolean isPrime(int n){
  29. if(n<2){
  30. return false;
  31. }
  32. for (int i = 2; i <= Math.sqrt(n); i++) {
  33. if (n % i == 0) {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. }

P1957 口算练习题

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        String[] output = new String[n];//最终输出结果
        String operator = "";//当前操作运算符,用于没有abc的情况
        //循环遍历
        for (int i = 0; i < n; i++) {
            int result = 0;//计算结果
            //获取一行,分割
            String line = sc.nextLine();
            String[] s = line.split(" ");
            //进行数据处理
            int a = 0;//a,b为两个操作的数字
            int b = 0;
            //含有运算符时
            if(s.length == 3){
                a = Integer.parseInt(s[1]);
                b = Integer.parseInt(s[2]);
                if(s[0].equals("a")){
                    result =  a+b;
                    operator = "a";
                    output[i] = a+"+"+b+"="+result;
                }
                if(s[0].equals("b")){
                    result = a-b;
                    operator = "b";
                    output[i] = a+"-"+b+"="+result;
                }
                if(s[0].equals("c")){
                    result = a*b;
                    operator = "c";
                    output[i] = a+"*"+b+"="+result;
                }
            }
            //不含有运算符时
            if(s.length == 2){
                a = Integer.parseInt(s[0]);
                b = Integer.parseInt(s[1]);
                if(operator.equals("a")){
                    result = a+b;
                    output[i] = a+"+"+b+"="+result;
                }
                if(operator.equals("b")){
                    result = a-b;
                    output[i] = a+"-"+b+"="+result;
                }
                if(operator.equals("c")){
                    result = a*b;
                    output[i] = a+"*"+b+"="+result;
                }
            }
        }
        for(String s : output){
            System.out.println(s);
            System.out.println(s.length());
        }
    }
}

P5015 [NOIP2018 普及组] 标题统计

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char[] chs = sc.nextLine().toCharArray();
        int count = 0;
        for(char c : chs){
            if(c!=' '){
                count++;
            }
        }
        System.out.println(count);
    }
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        StringBuilder sb = new StringBuilder(sc.nextLine());
        for (int i = 0; i < n; i++) {
            String operator = sc.next();
            switch (operator){
                case "1":
                    sb.append(sc.next());
                    System.out.println(sb);
                    break;
                case "2":
                    int begin = Integer.parseInt(sc.next());
                    int len = Integer.parseInt(sc. next());
                    sb = new StringBuilder(sb.substring(begin,begin+len));
                    System.out.println(sb);
                    break;
                case "3":
                    int index = Integer.parseInt(sc.next());
                    sb.insert(index,sc.next());
                    System.out.println(sb);
                    break;
                case "4":
                    int count = sb.indexOf(sc.next(),0);
                    System.out.println(count);
                    break;
            }
        }
    }
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int first = 0;
        int count = 0;
        boolean flag = true;
        String target = sc.nextLine();
        String text = sc.nextLine();
        String[] words = text.split(" ");
        for (String s : words) {
            if (s.equalsIgnoreCase(target)) {
                count++;
                flag = false;
            }
            if (flag) {
                //没有查到就往后移动first
                first += s.length() + 1;//加1是为了到达下个单词的第一个字母
            }
        }
        if(count==0){
            System.out.println(-1);
        }else {
            System.out.println(count + " " + first);
        }
    }
}

P1765 手机

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int count = 0;
        char[] chs = sc.nextLine().toCharArray();
        for(char c : chs){
            if(c=='a'|| c=='d' || c=='g' || c=='j' || c=='m' || c=='p' || c=='t' || c=='w' || c==' '){
                count += 1;
            }
            if(c=='b'|| c=='e' || c=='h' || c=='k' || c=='n' || c=='q' || c=='u' || c=='x' ){
                count += 2;
            }
            if(c=='c'|| c=='f' || c=='i' || c=='l' || c=='o' || c=='r' || c=='v' || c=='y' ){
                count += 3;
            }
            if(c=='s' || c=='z'){
                count += 4;
            }
        }
        System.out.println(count);
    }
}

P3741 honoka的键盘

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        char[] chs = sc.nextLine().toCharArray();
        int count = 0;
        //第一次遍历:把VK记录,并标记为‘*’
        for (int i = 0; i < n-1; i++) {
            if(chs[i] == 'V' && chs[i+1] == 'K'){
               count++;
               chs[i] = '*';
               chs[i+1] = '*';
            }
        }
        //第二次遍历:找到KK或者VV的情况,计数一次(KV无法改动一次成为VK)
        for (int i = 0; i < n - 1; i++) {
            if(chs[i] != '*' && chs[i]==chs[i+1]){
                count++;
                break;
            }
        }
        System.out.println(count);
    }
}

P1321 单词覆盖还原

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //究极暴力
        Scanner sc = new Scanner(System.in);
        int boy = 0;
        int girl = 0;
        String line = sc.nextLine();
        //1.先把boy,girl剔除并计数。
        while (line.contains("boy")) {
            line = line.replaceFirst("boy","...");
            boy++;
        }
        while (line.contains("girl")) {
            line = line.replaceFirst("girl","....");
            girl++;
        }
        //2.把残缺的girl剔除并计数(残缺的情况:gir,irl,gi,ir,rl)。先不管单个字母的情况。
        while(line.contains("gir")){
            line = line.replaceFirst("gir","...");
            girl++;
        }
        while(line.contains("irl")){
            line = line.replaceFirst("irl","...");
            girl++;
        }
        while(line.contains("gi")){
            line = line.replaceFirst("gi","..");
            girl++;
        }
        while(line.contains("ir")){
            line = line.replaceFirst("ir","..");
            girl++;
        }
        while(line.contains("rl")){
            line = line.replaceFirst("rl","..");
            girl++;
        }
        //3.把残缺的boy剔除并计数(残缺的情况:bo,oy)。先不管单个字母的情况。
        while(line.contains("bo")){
            line = line.replaceFirst("bo","..");
            boy++;
        }
        while(line.contains("oy")){
            line = line.replaceFirst("oy","..");
            boy++;
        }
        //4.把最后残缺的单个字母剔除并计数
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            if(c=='b' || c=='o' || c=='y'){
                boy++;
            }
            if(c=='g' || c=='i' || c=='r' || c=='l'){
                girl++;
            }
        }
        System.out.println(boy);
        System.out.println(girl);
    }
}

P1553 数字反转(升级版)

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        //思路:通过符号分为两部分,分别处理后拼接。(分情况,注意小数点的情况比较特殊)
        if(line.contains("%")){
            String[] ss = line.split("%");
            System.out.println(change(ss[0]) + "%");
        }else if(line.contains("/")){
            String[] ss = line.split("/");
            System.out.println(change(ss[0]) + "/" + change(ss[1]));
        }else if (line.contains(".")){  //最特殊的情况,要注意。
            String[] ss = line.split("\\.");
            String s = change(ss[0]) + "." + change(ss[1]);
            if(ss[1].equals("0")){
                //小数点后是0,如:123.0输出应该是321.0
                System.out.println(s);
            }else {
                //小数点后不是0,如:600.084输出应该是6.48(要剔除最后面多余的0)
                BigDecimal bd = new BigDecimal(s).stripTrailingZeros();
                System.out.println(bd.toPlainString());
            }
        }else {
            System.out.println(change(line));
        }
    }

    //反转数字的函数
    public static String change(String num){
        if(num.equals("0")){
            return "0";
        }
        //反转
        StringBuilder sb = new StringBuilder(num);
        sb.reverse();
        //清除开头的0
        int i = 0;
        while(sb.charAt(i)=='0'){
            i++;
        }
        if(i!=0){
            sb.delete(0,i);
        }
        return sb.toString();
    }
}

P1603 斯诺登的密码

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        String[] normal = {"one","two","three","four","five","six","seven","eight",
                "nine","ten", "eleven","twelve","thirteen","fourteen","fifteen",
                "sixteen","seventeen","eighteen","nineteen", "twenty"};
        String line = sc.nextLine();
        String[] ss = line.split(" ");
        for (String s : ss) {
            if(s.equalsIgnoreCase("a") || s.equalsIgnoreCase("another")
                    || s.equalsIgnoreCase("first")){
                list.add(1);
            }else if(s.equalsIgnoreCase("both")||s.equalsIgnoreCase("second")){
                list.add(4);
            }else if(s.equalsIgnoreCase("third")){
                list.add(9);
            }else {
                for (int i = 0; i < normal.length; i++) {
                    if(s.equalsIgnoreCase(normal[i])){
                        list.add(((i+1)*(i+1))%100);
                        break;
                    }
                }
            }
        }
        if(list.isEmpty()){
            System.out.println(0);
        }else {
            list.sort(Comparator.naturalOrder());
            StringBuilder raw = new StringBuilder();
            for (Integer i : list) {
                if(i<10){
                    raw.append("0");
                }
                raw.append(i);
            }
            char[] result = raw.toString().toCharArray();
            int t = 0;
            while(result[t] == '0'){
                t++;
            }
            for (int i = t; i < result.length; i++) {
                System.out.print(result[i]);
            }
        }
    }
}

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] star = sc.nextLine().toCharArray();
        char[] team = sc.nextLine().toCharArray();
        int starNum = 1;
        int teamNum = 1;
        for (char c : star) {
            starNum *= c-'A'+1;
        }
        starNum %= 47;
        for (char c : team) {
            teamNum *= c-'A'+1;
        }
        teamNum %= 47;
        if(starNum==teamNum){
            System.out.println("GO");
        }else {
            System.out.println("STAY");
        }
    }
}

P1597 语句解析

import java.util.Scanner;

public class Main {
    static int a = 0;
    static int b = 0;
    static int c = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line =  sc.nextLine();
        String[] all = line.split(";");
        for (String s : all){
            solution(s);
        }
        System.out.println(a + " " + b + " " +c);
    }

    public static void solution(String s){
        char c1 = s.charAt(0);
        char c2 = s.charAt(3);
        if(c1==c2){
            return;
        }
        if(c1 == 'a'){
            if(c2 == 'b'){
                a = b;
            }else if(c2 == 'c'){
                a = c;
            }else {
                a = Integer.parseInt(""+c2);
            }
        }
        if(c1 == 'b'){
            if(c2 == 'a'){
                b = a;
            }else if(c2 == 'c'){
                b = c;
            }else {
                b = Integer.parseInt(""+c2);
            }
        }
        if(c1 == 'c'){
            if(c2 == 'a'){
                c = a;
            }else if(c2 == 'b'){
                c = b;
            }else {
                c = Integer.parseInt(""+c2);
            }
        }
    }
}

P1598 垂直柱状图

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //计数
        int[] count = new int[1000];
        for (int i = 0; i < 4; i++) {
            char[] chs = sc.nextLine().toCharArray();
            for(char c : chs){
                if((c-'A')>=0){
                    count[c-'A']++;
                }
            }
        }
        int max = 0;//计数最大值
        for (int i = 0; i < 26; i++) {
            if(count[i] > max){
                max = count[i];
            }
        }
        //构建图形的数组
        char[][] matrix = new char[max][26];
        for(char[] chs : matrix){
            //填满 '*'
            Arrays.fill(chs,'*');
        }
        //核心代码
        for (int j = 0; j < 26; j++) {//先遍历列
            //每一列(对应每一个字母),比最大个数少多少,就填几个空格
            for (int i = 0; i < max-count[j]; i++) {
                matrix[i][j] = ' ';
            }
        }
        //打印结果
        for (int i = 0; i < max; i++) {
            for (int j = 0; j < 26; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("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");
    }
}