本篇主要把基础练习中用到的一些知识点以及可写题目的代码列出来,具体的代码请看我的github
基础练习
做题后的思考
- 一题多解
- 修改题目
- 构造输入,正确性测试,
- debug积累
十进制转换为十六进制
package lanqiao.base;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author: Zekun Fu
* @date: 2022/10/3 17:01
* @Description: 16进制转换十进制
*
*/
public class JinZhi {
public static void main(String[] args) {
char[] num = {'0','1','2','3','4','5','6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Character> ans = new ArrayList<>();
while (n != 0) {
ans.add(num[n % 16]);
n /= 16;
}
for (int i = ans.size() - 1; i >= 0; i--) {
System.out.print(ans.get(i));
}
if (ans.size() == 0) System.out.println(0);
else System.out.println();
}
}
杨辉三角
package lanqiao.base;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author: Zekun Fu
* @date: 2022/10/5 13:33
* @Description: 杨辉三角
*/
public class Triangle {
public static void main(String[] args) {
final int maxn = 100;
int[][] dp = new int[maxn][maxn];
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i], 0);
}
dp[1][1] = 1;
dp[2][1] = dp[2][2] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
}
}
// 如果从1开始,就不是小于i了,应该是等于i了
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j != i) System.out.print(dp[i][j] + " ");
else System.out.println(dp[i][j]);
}
}
}
}
闰年判断
package lanqiao.base;
import java.util.Scanner;
/**
* @author: Zekun Fu
* @date: 2022/10/5 14:29
* @Description: 检测是否是闰年
*/
public class CheckRun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) System.out.println("yes");
else System.out.println("no");
}
}
字母图形
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ejKXJZ3w-1665568018573)(D:\blgs\source\imgs\image-20221012174010324.png)]
这里采用了对拍的方式测试子集的代码,其中的m2是别人的代码,m1是自己的方法
package lanqiao.base;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author: Zekun Fu
* @date: 2022/10/5 14:13
* @Description: 字母图形,基础练习
*
* 规律:
* 第一行从A-(A + m - 1)
* 第二行BA-
* 第三行CBA
* 第四行
*/
public class ZimuTuxing {
public static char[][] m1 (int n, int m) {
char[][] ans = new char[n][m];
for (int i = 0; i < n; i++) {
// char bg = (char)('A' + i);
// for (int j = 0; j < i; j++)
// System.out.print((char)(bg - j));
// for (int j = 0; j < m - i; j++) {
// System.out.print((char)('A'+j));
// }
// System.out.println();
char bg = (char) ('A' + i);
// i 可能会出现大于m的情况。
for (int j = 0; j < Math.min(i, m); j++) { // m1(3, 1)会出错
ans[i][j] = (char) (bg - j);
}
for (int j = 0; j < m - i; j++) {
ans[i][j + i] = (char) ('A' + j);
}
}
return ans;
}
public static char[][] m2 (int n, int m) {
char[][] ans = new char[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j ++) {
ans[i][j] = (char)('A' + Math.abs(i - j));
}
}
return ans;
}
public static boolean check(char[][] a1, char[][] a2) {
int n = a1.length;
for (int i = 0; i < n; i++)
if (!Arrays.equals(a1[i], a2[i]))
return false;
return true;
}
public static void arrPrint(char[][] chars) {
for (char[] vec : chars) {
for (char ch : vec) {
System.out.print(ch);
}
System.out.println();
}
}
public static void test(String[] args) {
Scanner sc = new Scanner(System.in);
// m1(2, 1);
for (int i = 1; i <= 26; i++) {
for (int j = 1; j <= 26; j++) {
// arrPrint(m2(i, j));
// System.out.println();
if (!check(m1(i, j), m2(i, j))) {
System.out.println("i = " + i + " j = " + j);
arrPrint(m1(i, j));
System.out.println();
arrPrint(m2(i, j));
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 0; i < n; i++) {
char bg = (char)('A' + i);
// 注意这里不应该小于i,因为i可能大于m,但是不应该输出这么多!!!
for (int j = 0; j < Math.min(i, m); j++)
System.out.print((char)(bg - j));
for (int j = 0; j < m - i; j++) {
System.out.print((char)('A'+j));
}
System.out.println();
}
}
}