第一章-字符串处理(一)

1473. A + B 格式
在这里插入图片描述

方法1:库函数

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int sum = a + b;
        DecimalFormat df = new DecimalFormat("#,###");
        System.out.println(df.format(sum));
    }
}

方法2:模拟
将 sum 的绝对值变成字符串模式,用链表 list 存储答案,对字符串 s 从尾到首枚举,挨个加到列表中,每枚举 3 位加一个 “,”,注意的是枚举到头的第一个时不需要添加 “,”,若 sum 是负数,则需要在前面添加 “-”,最后对链表的元素进行输出

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int sum = a + b;
        String str = String.valueOf(Math.abs(sum));
        List<Character> list = new ArrayList<>();
        int cnt = 0;//计数器
        for (int i = str.length() - 1; i >= 0; i--, cnt++) {
            char x = str.charAt(i);
            if (cnt > 0 && cnt % 3 == 0) list.add(',');
            list.add(x);
        }
        if (sum < 0) System.out.print('-');
        for (int i = list.size() - 1; i >= 0; i--) {
            System.out.print(list.get(i));
        }
        System.out.println();
    }
}

1477. 拼写正确
在这里插入图片描述

照着题意模拟

import java.util.Scanner;

public class Main {
    static String[] read = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int res = 0;
        for (int i = 0; i < n.length(); i++) {
            res += n.charAt(i) - '0';
        }
        String str = String.valueOf(res);
        for (int i = 0; i < str.length(); i++) {
            int index = str.charAt(i) - '0';
            System.out.print(read[index] + " ");
        }
        System.out.println();
    }
}

1478. 签到与签出
在这里插入图片描述

方法1:
将时间统一换成 : 秒,用两个优先队列分别存储 签到时间—人名,和签出时间–人名

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //q1按照签到时间,升序
        PriorityQueue<Pair> q1 = new PriorityQueue<>(new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                return o1.time - o2.time;
            }
        });
        //q2按照签出时间,降序
        PriorityQueue<Pair> q2 = new PriorityQueue<>(new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                return o2.time - o1.time;
            }
        });
        int m = sc.nextInt();
        while (m-- > 0) {
            String name = sc.next();
            String[] ru = sc.next().split(":");
            int h1 = Integer.parseInt(ru[0]);
            int m1 = Integer.parseInt(ru[1]);
            int s1 = Integer.parseInt(ru[2]);
            int t1 = h1 * 3600 + m1 * 60 + s1;
            q1.add(new Pair(t1, name));
            String[] chu = sc.next().split(":");
            int h2 = Integer.parseInt(chu[0]);
            int m2 = Integer.parseInt(chu[1]);
            int s2 = Integer.parseInt(chu[2]);
            int t2 = h2 * 3600 + m2 * 60 + s2;
            q2.add(new Pair(t2, name));
        }
        String n1 = q1.poll().name;//开门人
        String n2 = q2.poll().name;//锁门人
        System.out.println(n1 + " " + n2);
    }
}
class Pair {
    int time;
    String name;

    public Pair(int time, String name) {
        this.time = time;
        this.name = name;
    }
}

方法2:
调用字符串比较的库函数

import java.util.Scanner;

public class Main {
    static int m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        //开门人,开门时间
        String inName = "", inTime = "";
        //锁门人,锁门时间
        String outName = "", outTime = "";
        for (int i = 0; i < m; i++) {
            String name = sc.next();
            String ru = sc.next();
            String chu = sc.next();
            //inTime > ru,说明ru是更早的开门时间
            if (i == 0 || inTime.compareTo(ru) > 0) {
                inTime = ru;//更新
                inName = name;
            }
            if (i == 0 || outTime.compareTo(chu) < 0) {
                outTime = chu;
                outName = name;
            }
        }
        System.out.println(inName + " " + outName);
    }
}

1519. 密码
在这里插入图片描述

遍历旧密码的每个字符,如果需要修改,则修改该字符,最后生成新密码,设置标志位 change,如果发生修改,则置为 true
输出的时候注意一下, n==1 的情况需要特判

import java.util.Scanner;

public class Main {
    static int N = 1010;
    static int n, m;
    static String[] s1 = new String[N];//新密码
    static String[] s2 = new String[N];//人名

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            String name = sc.next();
            String pwd = sc.next();
            StringBuilder newPwd = new StringBuilder();
            boolean change = false;
            for (int j = 0; j < pwd.length(); j++) {
                char c = pwd.charAt(j);
                if (c == '1') {
                    c = '@';
                    change = true;
                } else if (c == '0') {
                    c = '%';
                    change = true;
                } else if (c == 'l') {
                    c = 'L';
                   change = true;
                } else if (c == 'O') {
                    c = 'o';
                    change = true;
                }
                newPwd.append(c);
            }
            if (change) {
                s1[m] = newPwd.toString();//存下新密码
                s2[m] = name;//对应的人名
                m++;
            }
        }
        if (m == 0) {//没发生修改
            if (n == 1) {
                System.out.println("There is 1 account and no account is modified");
                return;
            } else {
                System.out.printf("There are %d accounts and no account is modified\n", n);
                return;
            }
        }
        System.out.println(m);
        for (int i = 0; i < m; i++) {
            System.out.println(s2[i] + " " + s1[i]);
        }
    }
}

1520. 男孩 vs 女孩
在这里插入图片描述

方法1:用男女两个数组存储,并按照成绩分别排序。

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

public class Main {
    static int N = 110;
    static Stu[] man = new Stu[N];
    static int m;//man数组的游标
    static Stu[] woman = new Stu[N];
    static int w;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean has_man = false;
        boolean has_woman = false;
        for (int i = 0; i < n; i++) {
            String name = sc.next();
            String sex = sc.next();
            String id = sc.next();
            int score = sc.nextInt();
            if (sex.equals("F")) {
                has_woman = true;
                woman[w++] = new Stu(name, sex, id, score);
            } else {
                has_man = true;
                man[m++] = new Stu(name, sex, id, score);
            }
        }
        //降序
        Arrays.sort(woman, 0, w, new Comparator<Stu>() {
            @Override
            public int compare(Stu o1, Stu o2) {
                return o2.score - o1.score;
            }
        });
        //升序
        Arrays.sort(man, 0, m, new Comparator<Stu>() {
            @Override
            public int compare(Stu o1, Stu o2) {
                return o1.score - o2.score;
            }
        });
        if (has_man && has_woman) {//有男有女
            Stu badman = man[0];
            Stu bestwoman = woman[0];
            System.out.println(bestwoman.name + " " + bestwoman.id);
            System.out.println(badman.name + " " + badman.id);
            System.out.println(Math.abs(bestwoman.score - badman.score));
        } else if (!has_man) {//没有男的
            Stu bestwoman = woman[0];
            System.out.println(bestwoman.name + " " + bestwoman.id);
            System.out.println("Absent");
            System.out.println("NA");
        } else {
            System.out.println("Absent");
            Stu badman = man[0];
            System.out.println(badman.name + " " + badman.id);
            System.out.println("NA");
        }
    }
}
class Stu {
    String name;
    String sex;
    String id;
    int score;

    public Stu(String name, String sex, String id, int score) {
        this.name = name;
        this.sex = sex;
        this.id = id;
        this.score = score;
    }
}

方法2:
在这里插入图片描述

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();

        int scoreG = -1;//女生的最高成绩
        String nameG = "###";
        String idG = "###";

        int scoreB = 101;//男生的最低成绩
        String nameB = "###";
        String idB = "###";

        for (int i = 0; i < n; i++) {
            String name = scan.next();
            String sex = scan.next();
            String id = scan.next();
            int score = scan.nextInt();
            if (sex.equals("F") && score > scoreG) {
                nameG = name;
                idG = id;
                scoreG = score;
            }
            if (sex.equals("M") && score < scoreB) {
                nameB = name;
                idB = id;
                scoreB = score;
            }
        }
        if (scoreG == -1) {
            System.out.println("Absent");
        } else {
            System.out.println(nameG + " " + idG);
        }

        if (scoreB == 101) {
            System.out.println("Absent");
        } else {
            System.out.println(nameB + " " + idB);
        }

        if (scoreG == -1 || scoreB == 101) {
            System.out.println("NA");
        } else {
            System.out.println(Math.abs(scoreG - scoreB));
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值