1. 程式人生 > >杭電OJ 2072、2081、2093、2091、1004、2057題

杭電OJ 2072、2081、2093、2091、1004、2057題

安卓開發及安全交流QQ群:838650234,感興趣的可以加群。

最近刷題甚是痛苦,一方面進入實驗室只能把零碎時間刷題;另一個方面感覺自己心浮氣躁、通過率不高且靜不下心來。

2072題:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            String strings_01 = sc_01.nextLine();
            //判斷有沒有#
            boolean flag = false;
            if (strings_01.contains("#")) {
                flag = true;
            }
            if (flag == true) break;
            //進行正常判斷
            int test_result = checkZimu(strings_01);
            System.out.println(test_result);

        }
    }

    private static int checkZimu(String strings_01) {
        String[] words = strings_01.split(" ");
        ArrayList<String> arrayList_01 = new ArrayList<String>();
        for (int i = 0; i < words.length; i++) {
            if (!arrayList_01.contains(words[i])) {
                arrayList_01.add(words[i]);
            }
        }
        if(arrayList_01.contains("")){
            return arrayList_01.size()-1;
        }else{
            return arrayList_01.size();
        }
    }
}

2081題:

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        int line_num = sc_01.nextInt();
        for(int i = 0;i<line_num;i++){
            String init_str = sc_01.next();
            System.out.println("6"+init_str.substring(6,11));//從下表為0的開始計數,下標為6,長度為5
        }
    }
}

2093題:

這道題我做的老有問題,別人的可以參考下。


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
    private static Scanner scanner;
    static List<Main.person> list;

    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        int probrom = scanner.nextInt();// 題目數量
        int pubScore = scanner.nextInt();// 單位罰分
        list = new ArrayList<Main.person>();
        while (scanner.hasNext()) {
            int ac = 0;// ac題目個數
            int score = 0;// 罰分
            String name = "";// 姓名

            String strings[] = new String[probrom + 1];
            // 輸入一個考生的資訊
            // strings[0]是名字
            for (int i = 0; i < strings.length; i++) {
                strings[i] = scanner.next();
            }
            name = strings[0];
            // 計算AC題目個數
            for (int i = 1; i < strings.length; i++) {
                char ch = strings[i].charAt(0);
                if (ch >= '1' && ch <= '9') {
                    ac++;
                    try {
                        score += Integer.parseInt(strings[i]);
                    } catch (Exception e) {
                    }
                }
            }
            // 計算分數
            for (int i = 1; i < strings.length; i++) {
                // 第i個元素的 最後一個字元是否為')'// 也可以使用contains
                char ch = strings[i].charAt(strings[i].length() - 1);
                if (ch == ')') {
                    String[] split = strings[i].split("[(,)]");
                    score += pubScore * Integer.parseInt(split[1]) + Integer.parseInt(split[0]);
                }
            }
            Main p = new Main();
            person per = p.new person(ac, score, name);
            list.add(per);
        }
        listSort();
        for (person person : list) {
            System.out.printf("%-10s %2d %4d", person.getName(), person.getAcNum(), person.getScore());
            System.out.println();
        }

    }

    // 排序
    private static void listSort() {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = i + 1; j < list.size(); j++) {
                String iName = list.get(i).getName();
                String jName = list.get(j).getName();
                int iAc = list.get(i).getAcNum();
                int jAc = list.get(j).getAcNum();
                int iScore = list.get(i).getScore();
                int jScore = list.get(j).getScore();

                if (iAc < jAc) {
                    sort(i, j);
                } else if (iAc == jAc) {
                    if (iScore > jScore) {
                        sort(i, j);
                    } else if (iScore == jScore) {
                        int compareTo = iName.compareTo(jName);
                        if (compareTo > 0) {
                            sort(i, j);
                        }
                    }
                }
            }
        }
    }

    // 交換
    public static void sort(int i, int j) {
        int temp = list.get(i).getAcNum();
        list.get(i).setAcNum(list.get(j).acNum);
        list.get(j).setAcNum(temp);

        temp = list.get(i).getScore();
        list.get(i).setScore(list.get(j).score);
        list.get(j).setScore(temp);

        String string = list.get(i).getName();
        list.get(i).setName(list.get(j).getName());
        list.get(j).setName(string);
    }

    class person {
        int acNum;
        int score;
        String name;

        public person(int acNum, int score, String name) {
            super();
            this.acNum = acNum;
            this.score = score;
            this.name = name;
        }

        public void setAcNum(int acNum) {
            this.acNum = acNum;
        }

        public void setScore(int score) {
            this.score = score;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAcNum() {
            return acNum;
        }

        public int getScore() {
            return score;
        }

        public String getName() {
            return name;
        }

    }
}

2091題:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        int flag = 0;//設定第一次的標誌位
        while (sc_01.hasNext()) {
            String chars = sc_01.next();
            if (!chars.equals("@")) {
                int chars_line = sc_01.nextInt();
                //根據標誌位判斷中間有沒有空行。和之前的格式不同,之前的格式都是第一行之後輸出空行的。
                if (flag != 0) {
                    System.out.println();
                }
                flag++;
                //如果只有一行的話
                if (chars_line == 1) {
                    System.out.println(chars);
                } else {
                    //若有多行的話
                    for (int i = 1; i < chars_line; i++) {
                        //前面的空格個數
                        for (int j = chars_line; j > i; j--) {
                            System.out.print(" ");
                        }
                        //輸出字串
                        System.out.print(chars);
                        //輸出其他字串
                        if (i != 1) {
                            for (int k = 1; k <= 2 * (i - 2) + 1; k++) {
                                System.out.print(" ");
                            }
                            System.out.print(chars);
                        }
                        //每一行空行
                        System.out.println();
                    }
                    //最後一行字串
                    for (int i = 1; i <= (chars_line * 2 - 1); i++) {
                        System.out.print(chars);
                    }
                    System.out.println();
                }
            } else {
                break;
            }
        }
    }
}

1004題:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int num_lin = sc_01.nextInt();
            //判斷是否包含
            ArrayList<String> arrayList_01 = new ArrayList<String>();
            //進行新增
            ArrayList<Counting> arrayList_02 = new ArrayList<Counting>();
            //輸入所有String字串
            if (num_lin != 0) {
                for (int i = 0; i < num_lin; i++) {
                    String strings = sc_01.next();
                    if (!arrayList_01.contains(strings)) {
                        //若輸入的字串不在該陣列中。
                        Counting counting_01 = new Counting();
                        counting_01.conting = 1;
                        counting_01.color_name = strings;
                        arrayList_02.add(counting_01);
                        arrayList_01.add(strings);
                    } else {
                        for (int j = 0; j < arrayList_02.size(); j++) {
                            if (arrayList_02.get(j).color_name.equals(strings)) {
                                arrayList_02.get(j).conting++;
                            }
                        }
                    }
                }
            } else {
                break;
            }
            int Max = arrayList_02.get(0).conting;
            String MaxStrings = arrayList_02.get(0).color_name;
            //統計
            for (int k = 0; k < arrayList_01.size() - 1; k++) {
                if (Max < arrayList_02.get(k + 1).conting){
                    //只儲存當時最大的值
                    Max = arrayList_02.get(k+1).conting;
                    MaxStrings = arrayList_02.get(k+1).color_name;
                }
            }
            System.out.println(MaxStrings);
        }
    }

    private static class Counting {
        private static String color_name;
        private static int conting;
    }
}

2057題:

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()){
            String num_01 = sc_01.next();
            String num_02 = sc_01.next();
            //1.先將字串轉為十六進位制,十六進位制轉為十進位制
            long result = Long.parseLong(num_01, 16)+ Long.parseLong(num_02,16);
            if(result>=0){
                System.out.println(Long.toHexString(result).toUpperCase());
            }else {
                System.out.println("-"+(Long.toHexString(Math.abs(result))).toUpperCase());
            }
        }
    }
}