程序筆記

1.交替輸出12A…5152Z

package 線程;


//兩個線程,輸出:12A 34B 56C ... 5152Z
//同步
public class synchronized控制輸出 {
    public static void main(String[] args) {
        //同步鎖,使用Object的wait和notifyAll方法
        Flag flag = new Flag();

        //之一,必須是鎖對象flag調用wait和notify

        //new MyThread01(flag).start(); //線程1
        new Thread(new MyThread01(flag), "線程1").start();
        //new MyThread02(flag).start(); //線程
        new Thread(new MyThread02(flag), "線程2").start();
    }
}

// 同步鎖
class Flag{
    public boolean flag = true;
}

//輸出1-52
class MyThread01 implements Runnable{
    private Flag flag;
    public MyThread01(){

    }
    //傳引用,與MyThread01中的flag爲同一flag,實現同步
    public MyThread01(Flag flag){
        this.flag = flag;
    }

    public void run() {
        //true輸出數字
        synchronized (this.flag){
            // 輸出1 - 52
            for (int i = 1; i < 53; i++) {
                if(this.flag.flag){
                    System.out.print(i);
                    if (i%2 == 0){
                        //兩次後變爲false,不輸出
                        this.flag.flag = false;
                        //喚醒
                        flag.notifyAll();
                    }
                }else {
                    try {
                        //等待
                        flag.wait();
                        //for運行了,故i-1
                        i--;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

// 輸出A-Z
class MyThread02 implements Runnable{
    private Flag flag;
    public MyThread02(){
    }
    //傳引用,與MyThread01中的flag爲同一flag,實現同步
    public MyThread02(Flag flag){
        this.flag = flag;
    }

    public void run() {
        //false輸出字母
        synchronized (this.flag){
            for (int i = 0; i < 26; i++) {
                if(!this.flag.flag){
                    System.out.print((char)(i+65)+" ");
                    this.flag.flag = true;
                    //喚醒
                    flag.notifyAll();
                }else {
                    try {
                        //等待
                        flag.wait();
                        //for運行了,i-1
                        i--;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

2.Shopee的辦公室

package 算法;

import java.util.Scanner;

public class J01Shopee的辦公室 {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int n = sc.nextInt();
        int a[][] = new int[n][2];
        int map[][] = new int[x+1][y+1];
        for(int i=0; i<n; i++){
            a[i][0] = sc.nextInt();
            a[i][1] = sc.nextInt();
            map[a[i][0]][a[i][1]] = -1;
        }

        f(map, x, y, 0, 0);
        System.out.println(count);
    }

    static int count = 0;
    public static void f(int map[][], int x, int y, int nowx, int nowy){
        if (nowx == x && nowy == y){
            count++;
            return;
        }
        if (map[nowx][nowy] != -1){
            if (nowx < x) f(map,x,y,nowx+1, nowy);
            if (nowy < y) f(map,x,y,nowx, nowy+1);
        }

    }
}

3.萬萬沒想到之聰明的編輯

package 算法;

import java.util.Scanner;

public class J02萬萬沒想到之聰明的編輯 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = sc.next();
        }

        for (int i = 0; i < n; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < strings[i].length(); j++) {
                if (j==0 || j==1) stringBuilder.append(strings[i].charAt(j));
                else if (stringBuilder.length() == 2){
                    if ( f1(stringBuilder.charAt(stringBuilder.length()-1-1), stringBuilder.charAt(stringBuilder.length()-1), strings[i].charAt(j)) ){
                        stringBuilder.append(strings[i].charAt(j));
                    }
                }
                else if (stringBuilder.length() >= 3){
                    if ( f1(stringBuilder.charAt(stringBuilder.length()-1-1), stringBuilder.charAt(stringBuilder.length()-1), strings[i].charAt(j))
                            && f2(stringBuilder.charAt(stringBuilder.length()-1-2), stringBuilder.charAt(stringBuilder.length()-1-1),
                            stringBuilder.charAt(stringBuilder.length()-1), strings[i].charAt(j)) ){
                        stringBuilder.append(strings[i].charAt(j));
                    }
                }
            }
            strings[i] = stringBuilder.toString();
        }
        for (int i = 0; i < n; i++) {
            System.out.println(strings[i]);
        }
    }

    public static boolean f1(char c1, char c2, char c3){
        if (c1 == c2 && c2 == c3) return false;
        else return true;
    }
    public static boolean f2(char c1, char c2, char c3,char c4){
        if (c1 ==c2 && c3 == c4) return false;
        else return true;
    }
}

4. 無重複字符的最長子串

https://blog.csdn.net/djdjdjcux/article/details/104645003

5.劃分出所有單詞並且記錄每一個單詞第一次出現的位置從一開始

package 算法;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class 劃分出所有單詞並且記錄每一個單詞第一次出現的位置從一開始 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(s);

        //自己實現
        // 作爲是否在單詞中的判斷
        boolean inword = false;
        StringBuilder stringBuilder = new StringBuilder();
        //count作爲map的鍵,記錄出現的位置
        int count = 0;
        String word = "";
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < s.length(); i++) {
            char now = s.charAt(i);
            //否是字母
            inword = isalp(now);
            if (inword == false){
                //不是字母,且stringBuilder中長度大於0
                if (stringBuilder.length()>=1){
                    word = stringBuilder.toString();
                    //如果不包含該單詞再加入
                    if (!map.containsKey(word)) map.put(word, ++count);
                    //清空stringBuilder
                    stringBuilder.delete(0,stringBuilder.length());
                }
            }
            else{
                //是字母,加入到stringBuilder
                stringBuilder.append(s.charAt(i));
            }
        }
        //萬一者人沒有句號直接結束??最後一個單詞也不能忘
        if (stringBuilder.length( )>= 1){
            word = stringBuilder.toString();
            // 如果不包含該單詞再加入
            if (!map.containsKey(word)) map.put(word, ++count);
            //清空stringBuilder
            stringBuilder.delete(0,stringBuilder.length());
        }
        System.out.println(map);
    }

    public static boolean isalp(char now){
        if ( (now>='A'&&now<='Z')||(now>='a'&&now<='z') ) return true;
        else return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章