蓝桥杯vip之基础练习

1.阶乘计算

题目描述
在这里插入图片描述
代码实现

public class JieCheng {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int i=1;
		BigInteger res=new BigInteger("1");
		while (i<=n) {
			String s=String.valueOf(i);
			res=res.multiply(new BigInteger(s));
			i++;
		}
		System.out.println(res);
	}
}

2.高精度加法

题目描述
在这里插入图片描述
代码实现

public class GaoJingDuJiSuan {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a = sc.next();
		String b = sc.next();
		BigInteger res = new BigInteger("0");
		res = new BigInteger(a).add(new BigInteger(b));
		System.out.println(res);
	}
}

3. Huffuman树

题目描述
在这里插入图片描述在这里插入图片描述
代码实现

public class Huffuman {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		int arr[] = new int[size];
		int res = 0;
		for (int i = 0; i < size; i++) {
			int n = sc.nextInt();
			arr[i] = n;
		}
		Arrays.sort(arr);
		for (int i = 1; i < arr.length; i++) {
			arr[i] = arr[i] + arr[i - 1];
			res += arr[i];
			arr[i - 1] = 0;
			Arrays.sort(arr);
		}
		System.out.println(res);
	}
}

4.报时助手

题目描述
在这里插入图片描述样例输入

0 15

样例输出

zero fifteen

代码实现

public class BaoShiJiShu {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int m = sc.nextInt();
		String s[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
				"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
				"twenty one", "twenty two", "twenty three", "twenty four" };
		String case1[] = { "o'clock", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
		String case2[] = { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty" };

		String strRes = "";
		strRes = s[h];
		if (m < 20 && m >= 0) {
			strRes = strRes + " " + case1[m];
		}
		if (m >= 20 && m <= 60) {
			if (m % 10 == 0) {
				strRes = strRes + " " + case2[m / 10];
			} else {
				strRes = strRes + " " + case2[m / 10] + " " + case1[m % 10];
			}
		}
		System.out.println(strRes);
	}
}

5.回形取数

题目描述

问题描述
  回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
  
输入格式
  输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
  
输出格式
  输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。

样例输入
3 3
1 2 3
4 5 6
7 8 9

样例输出
1 4 7 8 9 6 3 2 5

样例输入
3 2
1 2
3 4
5 6

样例输出
1 3 5 6 4 2

代码实现

public class HuiXingQuShu {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int rows = sc.nextInt();
		int cols = sc.nextInt();
		int arr[][] = new int[rows][cols];
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				arr[i][j] = sc.nextInt();
			}
		}

		int cur = 0, x = -1, y = 0;
		while (cur < rows * cols) {
			while (x + 1 < rows && arr[x + 1][y] != -1) {
				System.out.print(arr[++x][y] + " ");
				arr[x][y] = -1;
				++cur;
			}
			while (y + 1 < cols && arr[x][y + 1] != -1) {
				System.out.print(arr[x][++y] + " ");
				arr[x][y] = -1;
				++cur;
			}
			while (x - 1 >= 0 && arr[x - 1][y] != -1) {
				System.out.print(arr[--x][y] + " ");
				arr[x][y] = -1;
				++cur;
			}
			while (y - 1 >= 0 && arr[x][y - 1] != -1) {
				System.out.print(arr[x][--y] + " ");
				arr[x][y] = -1;
				++cur;
			}

		}

	}

}

6.龟兔赛跑预测

题目描述

问题描述
  话说这个世界上有各种各样的兔子和乌龟,但是研究发现,所有的兔子和乌龟都有一个共同的特点——喜欢赛跑。于是世界上各个角落都不断在发生着乌龟和兔子的比赛,小华对此很感兴趣,于是决定研究不同兔子和乌龟的赛跑。他发现,兔子虽然跑比乌龟快,但它们有众所周知的毛病——骄傲且懒惰,于是在与乌龟的比赛中,一旦任一秒结束后兔子发现自己领先t米或以上,它们就会停下来休息s秒。对于不同的兔子,t,s的数值是不同的,但是所有的乌龟却是一致——它们不到终点决不停止。
  然而有些比赛相当漫长,全程观看会耗费大量时间,而小华发现只要在每场比赛开始后记录下兔子和乌龟的数据——兔子的速度v1(表示每秒兔子能跑v1米),乌龟的速度v2,以及兔子对应的t,s值,以及赛道的长度l——就能预测出比赛的结果。但是小华很懒,不想通过手工计算推测出比赛的结果,于是他找到了你——清华大学计算机系的高才生——请求帮助,请你写一个程序,对于输入的一场比赛的数据v1,v2,t,s,l,预测该场比赛的结果。
  
输入格式
  输入只有一行,包含用空格隔开的五个正整数v1,v2,t,s,l,其中(v1,v2<=100;t<=300;s<=10;l<=10000且为v1,v2的公倍数)
输出格式
  输出包含两行,第一行输出比赛结果——一个大写字母“T”或“R”或“D”,分别表示乌龟获胜,兔子获胜,或者两者同时到达终点。
  第二行输出一个正整数,表示获胜者(或者双方同时)到达终点所耗费的时间(秒数)。

样例输入
10 5 5 2 20
样例输出
D
4
样例输入
10 5 5 1 20
样例输出
R
3
样例输入
10 5 5 3 20
样例输出
T
4

代码实现

public class GuiTuSaiPao {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int v1 = sc.nextInt();// 兔子的速度
		int v2 = sc.nextInt();// 乌龟的速度
		int t = sc.nextInt();// 兔子领先t米
		int s = sc.nextInt();// 兔子停留s秒
		int l = sc.nextInt();// 赛道长度

		int time = 0;
		// 起跑
		int s1 = 0;
		int s2 = 0;
		while (s1 < l && s2 < l) {
			if (s1 - s2 >= t) {// 兔子休息
				for (int i = 0; i < s; i++) {
					s2 += v2;
					time++;
					if (s2 == l) {// 乌龟到达终点
						break;
					}
				}
			} else if (s1 - s2 < t) {// 两个都在跑
				s1 += v1;
				s2 += v2;
				time++;
			}
		}
		if (s1 == l && s2 < l) {
			System.out.println("R");
			System.out.println(time);
		} else if (s2 == l && s1 < l) {
			System.out.println("T");
			System.out.println(time);
		} else {
			System.out.println("D");
			System.out.println(time);
		}
	}
}

7.芯片测试

题目描述

问题描述
  有n(2≤n≤20)块芯片,有好有坏,已知好芯片比坏芯片多。
  每个芯片都能用来测试其他芯片。用好芯片测试其他芯片时,能正确给出被测试芯片是好还是坏。而用坏芯片测试其他芯片时,会随机给出好或是坏的测试结果(即此结果与被测试芯片实际的好坏无关)。
  给出所有芯片的测试结果,问哪些芯片是好芯片。
输入格式
  输入数据第一行为一个整数n,表示芯片个数。
  第二行到第n+1行为n*n的一张表,每行n个数据。表中的每个数据为0或1,在这n行中的第i行第j列(1≤i, j≤n)的数据表示用第i块芯片测试第j块芯片时得到的测试结果,1表示好,0表示坏,i=j时一律为1(并不表示该芯片对本身的测试结果。芯片不能对本身进行测试)。
输出格式
  按从小到大的顺序输出所有好芯片的编号
样例输入
3
1 0 1
0 1 0
1 0 1
样例输出
1 3

样例输入
5
1 1 0 0 1
1 1 0 0 1
0 0 1 0 0
1 0 1 1 0
1 1 0 0 1

样例输出
1 2 5

代码实现

import java.util.Scanner;

public class XinPian {
	/*
	 * 思路:好芯片测试结果是正确的,坏芯片可能是正确可能是错误的; 另外好芯片比坏芯片要多。这里i==j时一律为1,举个例子3个好的,2个坏的,
	 * 那好的芯片得到的1一定是大于等于3的,而坏芯片得到的1一定是小于等于2的, 即如果是n个芯片,那么得到1的个数*2大于n的话就是好芯片。
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();// 芯片数量
		int[][] arr = new int[num][num];// 存放二维数组
		int res[] = new int[num];// 存放结果集
		int k = 0;
		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				arr[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < arr.length; i++) {
			int tmp = 0;// 存放每一列的临时结果
			for (int j = 0; j < res.length; j++) {
				tmp += arr[j][i];
			}
			if (tmp * 2 > num) {
				res[k++] = i + 1;
			}
		}
		for (int i : res) {
			if (i != 0) {
				System.out.print(i + " ");
			}
		}
	}
}

8.FJ的字符串

题目描述

问题描述
  FJ在沙盘上写了这样一些字符串:
  A1 = “A”
  A2 = “ABA”
  A3 = “ABACABA”
  A4 = “ABACABADABACABA”
  … …
  你能找出其中的规律并写所有的数列AN吗?
 
输入格式
  仅有一个数:N ≤ 26。  
输出格式
  请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。

样例输入
3
样例输出
ABACABA

代码实现

import java.util.Scanner;

public class FJ {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		String res[]=new String[N+1];
		res[0]="";
		for (int i = 1; i <= N; i++) {
			int a=64+i;
			char c=(char)a;
			String s=""+c;
			res[i]=res[i-1]+s+res[i-1];			
		}
		System.out.println(res[N]);
	}
}

9.矩形面积交

题目描述

问题描述
  平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。
  
输入格式
  输入仅包含两行,每行描述一个矩形。
  在每行中,给出矩形的一对相对顶点的坐标,每个点的坐标都用两个绝对值不超过10^7的实数表示。
输出格式
  输出仅包含一个实数,为交的面积,保留到小数后两位。
  
样例输入
1 1 3 3
2 2 4 4
样例输出
1.00

代码实现

import java.util.Scanner;

public class JuXingMianJiJiao {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double x1, x2, x3, x4;
		double y1, y2, y3, y4;
		x1 = sc.nextDouble();
		y1 = sc.nextDouble();
		x2 = sc.nextDouble();
		y2 = sc.nextDouble();

		x3 = sc.nextDouble();
		y3 = sc.nextDouble();
		x4 = sc.nextDouble();
		y4 = sc.nextDouble();

		double m1, n1;// 交集相对左顶点坐标
		double m2, n2;// 交集相对右顶点坐标

		// min:用来取每个矩形的左边,取出来之后用max求出这两个左边的最大值,就是相交左顶点的横坐标
		m1 = Math.max(Math.min(x1, x2), Math.min(x3, x4));
		n1 = Math.max(Math.min(y1, y2), Math.min(y3, y4));

		m2 = Math.min(Math.max(x1, x2), Math.max(x3, x4));
		n2 = Math.min(Math.max(y1, y2), Math.max(y3, y4));

		double width = m2 - m1;
		double length = n2 - n1;
		double s = width * length;
		if (m2 > m1 && n2 > n1) {
			System.out.printf("%.2f", s);
		} else {
			System.out.println("0.00");
		}
	}
}

10.矩阵乘法

题目描述

问题描述
  给定一个N阶矩阵A,输出A的M次幂(M是非负整数)
  例如:
  A =
  1 2
  3 4
  A的2次幂
  7 10
  15 22

输入格式
  第一行是一个正整数N、M(1<=N<=30, 0<=M<=5),表示矩阵A的阶数和要求的幂数
  接下来N行,每行N个绝对值不超过10的非负整数,描述矩阵A的值
输出格式
  输出共N行,每行N个整数,表示A的M次幂所对应的矩阵。相邻的数之间用一个空格隔开
样例输入
2 2
1 2
3 4
样例输出
7 10
15 22

代码实现

import java.util.Scanner;

public class JuZhengChengFa {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int arr[][] = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				arr[i][j] = sc.nextInt();
			}
		}
		int result[][] = new int[n][n];
		result = arr;

		if (m == 0) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == j) {
						System.out.print(1 + " ");
					} else {
						System.out.print(0 + " ");
					}
				}
				System.out.println();
			}
		}
		if (m != 0) {
			for (int i = 1; i < m; i++) {
				result = multMatrix(result, arr);
			}
			for (int i = 0; i < result.length; i++) {
				for (int j = 0; j < result[0].length; j++) {
					System.out.print(result[i][j] + " ");
				}
				System.out.println();
			}

		}
	}

	private static int[][] multMatrix(int[][] a, int[][] b) {
		int n = a.length;
		int result[][] = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {

				for (int k = 0; k < n; k++) {
					result[i][j] += a[i][k] * b[k][j];
				}
			}
		}
		return result;
	}
}

发布了88 篇原创文章 · 获赞 27 · 访问量 5908

猜你喜欢

转载自blog.csdn.net/weixin_43362002/article/details/104592123
今日推荐