蓝桥杯基础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);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凭栏听雨客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值