作业1:作业进阶(棋盘进阶问题):
leetcode 第63题
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
1 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
package com.openlab.homework;
import java.util.Arrays;
public class Hw001 {
public static void main(String[] args) {
int[][] obstacleGrid = new int[][] {
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0}
};
printArr(obstacleGrid);
System.out.println(uniquePathsWithObstacles(obstacleGrid));
printArr(obstacleGrid);
}
private static int uniquePathsWithObstacles(int[][] obstacleGrid) {
// 数组赋初值
evaluation(obstacleGrid);
for (int i = 1; i < obstacleGrid.length; i++) {
for (int j = 1; j < obstacleGrid[0].length; j++) {
if (obstacleGrid[i][j] == 1) {
obstacleGrid[i][j] = 0;
} else {
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
}
return obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
}
// 给数组赋初值 第一行 和 第一列
private static void evaluation(int[][] obstacleGrid) {
// 对第一列里面数据进行遍历
for (int i = 0; i < obstacleGrid.length; i++) {
if (obstacleGrid[i][0] == 0) {
obstacleGrid[i][0] = 1;
} else {
for (int m = i; m < obstacleGrid.length; m++) {
obstacleGrid[m][0] = 0;
}
break;
}
}
// 第一行
for (int j = 1; j < obstacleGrid[0].length; j++) {
if (!(obstacleGrid[0][j] == 1)) {
obstacleGrid[0][j] = 1;
} else {
for (int m = j; m < obstacleGrid[0].length; m++) {
obstacleGrid[0][m] = 0;
}
break;
}
}
}
// 输出数组
private static void printArr(int[][] twoArr) {
// 对行进行遍历 Arrays.fil() 对每一行可以进行填充
for (int i = 0; i < twoArr.length; i++) {
String temp = Arrays.toString(twoArr[i]);
System.out.println(temp);
}
}
}
作业2:
定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登录案例
将注册的用户写在一个数组中。
package com.openlab.homework;
import java.util.Arrays;
import java.util.Iterator;
import java.util.*;
public class Hw002 {
private static final String[][] String = null;
// 定义两个属性
public String username;
public String passwd;
// 方法 注册账号
public String[] register(String[] save) {
save = Arrays.copyOf(save, save.length + 2);
// 用户名唯一
save[save.length - 2] = username;
save[save.length - 1] = passwd;
// System.out.println(Arrays.toString(save));
System.out.println("恭喜您注册成功!!");
return save;
}
// 方法 登录账号
public int enter(String[] save) {
for (int i = 0; i < save.length; i++) {
if (username.contains(save[i]) && save[i].contains(username)) {
if (passwd.contains(save[i+1]) && save[i+1].contains(passwd)) {
System.out.println("登录成功");
return 0;
} else {
System.out.println("!!!!密码错误!!!!");
return 0;
}
}
i++;
}
System.out.println("!!!!不存在该用户名!!!!");
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// save 数组存放用户名密码
String[] save = new String []{"用户名","密码"};
boolean flag = true;
while(flag) {
System.out.print("1.注册请输入【1】\n2.登录请输入【2】\n3.退出请输入【3】\n请输入:");
int n = sc.nextInt();
switch (n) {
case 1: {
System.out.println("=================");
System.out.println("开始注册了-----哔!");
Hw002 user = new Hw002();
System.out.print("注册用户名:");
user.username = sc.next();
System.out.print("对应的密码:");
user.passwd = sc.next();
save = user.register(save);
// System.out.println(Arrays.toString(save));
System.out.println("=================");
break;
}
case 2: {
System.out.println("=================");
System.out.println("开始登录了-----哔!");
Hw002 tempUser = new Hw002();
System.out.print("输入用户名:");
tempUser.username = sc.next();
System.out.print("对应的密码:");
tempUser.passwd = sc.next();
tempUser.enter(save);
System.out.println("=================");
break;
}
default:
System.out.println("退出成功,祝你生活愉快^v^");
flag = false;
break;
}
}
}
}
作业3:
定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛
package com.openlab.homework;
public class Hw003 {
public String name;
public boolean gender;
public int age;
public String master;
public String leather;
public void information(Hw003 cat) {
System.out.println(cat.name);
System.out.println(cat.gender);
System.out.println(cat.age);
System.out.println(cat.master);
System.out.println(cat.leather);
}
public static void main(String[] args) {
Hw003 cat = new Hw003();
cat.name = "咖啡猫";
cat.gender = true;
cat.age = 3;
cat.master = "qiaoZhi";
cat.leather = "红黑";
cat.information(cat);
}
}
作业4:
Stack的实现
package com.openlab.homework;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
public class Hw004 {
public int[] data = new int[0];
public int top;
public int capacity;
// 入栈
public void push(int e) {
data = Arrays.copyOf(data, data.length + 1);
data[data.length - 1] = e;
System.out.println(e + "\t入栈成功");
// System.out.println(Arrays.toString(data));
}
// 出栈
public int pop() {
if (data.length == 0) {
System.out.println("栈里无数据,不能出栈");
return 0;
}
int dataPop = data[data.length - 1];
data = Arrays.copyOf(data, data.length - 1);
System.out.println(dataPop + "\t出栈成功");
return dataPop;
}
// 查看栈的数据
public void stack() {
System.out.println("栈中的数据:");
System.out.println(Arrays.toString(data));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = true;
Hw004 stackHaha = new Hw004();
while(flag) {
System.out.print("1.入栈【1】\n2.出栈请输入【2】\n3.查看栈的数据请输入【3】\n4.退出请输入【4】\n请输入:");
int n = sc.nextInt();
switch (n) {
case 1: {
System.out.println("=================");
System.out.println("开始入栈了-----哔!");
System.out.print("输入入栈的数据:");
int num = sc.nextInt();
stackHaha.push(num);
System.out.println("=================");
break;
}
case 2: {
System.out.println("=================");
stackHaha.pop();
System.out.println("=================");
break;
}
case 3: {
System.out.println("=================");
stackHaha.stack();
System.out.println("=================");
break;
}
default:
System.out.println("退出成功,祝你生活愉快^v^");
flag = false;
break;
}
}
}
}
作业5:
word中题 class50、Home33、Home34……