蓝桥杯vip之基础练习
程序员文章站
2022-06-12 19:38:25
...
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;
}
}
}
}
上一篇: 字符串乱码解决思路
下一篇: phpMyAdmin4.4.10装配