藍橋杯基礎Java--FJ字符串&&楊輝三角&&回形取數&&哈夫曼&&時間轉換

FJ字符串

問題描述
  FJ在沙盤上寫了這樣一些字符串:
  A1 = “A”
  A2 = “ABA”
  A3 = “ABACABA”
  A4 = “ABACABADABACABA”
  … …
  你能找出其中的規律並寫所有的數列AN嗎?

思路:Ai可以看成是 Ai-1 i Ai-1 其中A1是A。

import java.util.Scanner;

public class FJString {

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

		int n = scn.nextInt();
		String s = "";
		for (int i = 65; i < 65 + n; i++) {
			s = s + (char) i + s;
		}
		System.out.println(s);

	}

}

楊輝三角

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[][] arr = new int[n][n];
		arr[0][0] = 1;
		System.out.println(arr[0][0]);
		for (int i = 1; i < arr.length; i++) {
			arr[i][0] = 1;
			System.out.print(arr[i][0] + " ");
			for (int j = 1; j < i + 1; j++) {
				arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}

	}

}

回形取數

回形取數就是沿矩陣的邊取數,若當前方向上無數可取或已經取過,則左轉90度。一開始位於矩陣左上角,方向向下。
思路:順着逆時針走的時候,我們維護一個total用於計算一共走過多少個點。並且將走過的點賦值爲-1。這樣,當我們再走的時候,每次都要先判斷下一個值是否已經走過以及是否走到盡頭。

import java.util.Scanner;

public class HuiXingQushu {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int m = scn.nextInt();
		int n = scn.nextInt();
		int[][] a = new int[m][n];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				a[i][j] = scn.nextInt();
			}
		}

		int tot = 0, x = -1, y = 0;
		while (tot < m * n) {
		//
			while (x + 1 < m && a[x + 1][y] != -1) {
				System.out.print(a[++x][y] + " ");
				a[x][y] = -1;
				++tot;
			}
			while (y + 1 < n && a[x][y + 1] != -1) {
				System.out.print(a[x][++y] + " ");
				a[x][y] = -1;
				++tot;
			}
			while (x - 1 >= 0 && a[x - 1][y] != -1) {
				System.out.print(a[--x][y] + " ");
				a[x][y] = -1;
				++tot;
			}

			while (y - 1 >= 0 && a[x][y - 1] != -1) {
				System.out.print(a[x][--y] + " ");
				a[x][y] = -1;
				++tot;
			}

		}

	}
}

Huffman(哈夫曼樹)

本題任務:對於給定的一個數列,現在請你求出用該數列構造Huffman樹的總費用。
思路:其實就是給一個數列,找在其中兩個最小的數,將這兩個數加起來,在數列中添加兩數之和,然後兩個數都去掉。
維護一個sum求總費用
利用ArrayList能夠實現排序,添加元素,刪除元素。

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

public class Huffman {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		ArrayList<Integer> l = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			l.add(scn.nextInt());
		}
		Integer e = 0;

		while (l.isEmpty() == false) {
			if (l.size() == 1) {
				System.out.println(e);
				l.remove(0);
			}

			else {
				l.sort(null);
				e = e + l.get(0) + l.get(1);
				l.add((l.get(0) + l.get(1)));
				l.remove(0);
				l.remove(0);//由於第一個已經被刪除,所以第二個變成了第一個

			}

		}

	}

}

時間轉換

給定一個以秒爲單位的時間t,要求用 “< H >:< M >:< S >” 的格式來表示這個時間。< H >表示時間,< M>表示分鐘,而< S >表示秒,它們都是整數且沒有前導的“0”。例如,若t=0,則應輸出是“0:0:0”;若t=3661,則輸出“1:1:1”。

import java.util.Scanner;

public class TimeTans {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int t = scn.nextInt();
		int a = 0, b = 0, c = 0;
		a = t / 3600;
		b = (t - 3600 * a) / 60;
		c = t - 3600 * a - 60 * b;
		System.out.println(a + ":" + b + ":" + c);

	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章