实验二
(1)把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包 含数字 2 和 4,一共有多少种不同的分解方法? 注意交换 3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和 1001+1000+18 被视为同一种。
(2)请实现从键盘输入10个数字,然后使用选择排序、冒泡排序、插入排序实现数字排序,并输出。
(3)一个字符串的非空子串是指字符串中长度至少为 1 的连续的一段字符组成 的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共 7 个。 注意在计算时,只算本质不同的串的个数。 请问,字符串0100110001010001 有多少个不同的非空子串?
package java实验2;
public class First {
public static void main(String args[]) {
//计算三数之和为2019
int ans = 0;
for(int i = 1;i <= 2019/2;i++) {
if(juge(i)==0) continue;
for(int j = i+1;j<=2019;j++) {
if(juge(j)==0) continue;
for(int k = j+1;k<=2019;k++) {
if(juge(k)==0) continue;
if(i+j+k == 2019) {
ans++;
}
}
}
}
System.out.println(ans);
}
public static int juge(int x) {
//1表示此数不包含2或者4,0表示包含
int flag = 1;
while(x>0) {
if (x % 10 == 2 || x % 10 == 4) {
flag = 0;
break;
}
else {
x /= 10;
}
}
return flag;
}
}
package java实验2;
import java.util.Scanner;
public class Second {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
int [] numbers = new int[10];
for (int i = 0;i<numbers.length;i++) {
numbers[i] = reader.nextInt();
}
int [] numbers2 = numbers; //复制两个数组
int [] numbers3 = numbers;
//冒泡
for(int i = 0;i < numbers.length ;i++ ) {
for (int j=0;j<numbers.length - 1;j++) {
if(numbers[j]>numbers[j+1]) {
int temp;
temp = numbers[j+1];
numbers[j+1] = numbers[j];
numbers[j] = temp;
}
}
}
//插入
for (int i = 1; i < numbers2.length; i++) {
int key = numbers[i];
int j = i - 1;
// 将大于key的元素向后移动一位
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j = j - 1;
}
numbers[j + 1] = key; // 将key插入到正确的位置
}
// 选择
for (int i = 0; i < numbers3.length - 1; i++) {
// 寻找[i, end]区间内的最小值
int minIndex = i;
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[j] < numbers[minIndex]) {
minIndex = j; // 更新最小值的索引
}
}
// 将找到的最小值和第i位置所在的值交换
if (minIndex != i) {
int temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
}
}
System.out.println("冒泡排序结果为:");
for (int number : numbers) {
System.out.print(number+" ");
}
System.out.println();
System.out.println("插入排序结果为:");
for (int number : numbers2) {
System.out.print(number+" ");
}
System.out.println();
System.out.println("选择排序结果为:");
for (int number : numbers3) {
System.out.print(number+" ");
}
}
}
package java实验2;
import java.util.HashSet;
public class Third {
public static void main(String[] args) {
String str = "0100110001010001";
int count = countDistinctSubstrings(str);
System.out.println("字符串" + str + "有" + count + "个不同的非空子串");
}
public static int countDistinctSubstrings(String str) {
HashSet<String> set = new HashSet<>();
int length = str.length();
// 遍历所有可能的子串
for (int i = 0; i < length; i++) {
for (int j = i + 1; j <= length; j++) {
String substring = str.substring(i, j);
set.add(substring);
}
}
return set.size();
}
}
实验三
(1)已知复数类Complex:
float real;代表实部
float imag;代表虚部
Complex();空构造方法
Complex(float,float);带参构造方法
Complex Add(float);
Complex Add(Complex com);
Complex Sub(float);
Complex Sub(Complex com);
Complex Mul(float);
Complex Mul(Complex com);
String toString();把当前复数,按照a+bi的形式进行打印
要求:
写测试类test,测试自定义的Complex类各方法的功能正确性,并打印出各自运算结果
(2)在一村庄中有一口井well,每天产水100升,村庄中有n户人家family(n<20),每户人家用水同户序号同,比如第i户人家每天用水i升(i<n),以此类推,每户人家取水顺序为随机,请编写代码实现该村的取水情况。
已知有Well类, Family类及测试test类,将各类完善,并打印出取水顺序。
package java实验3;
public class First {
public static void main(String args[]){
Complex A = new Complex(3,4); //复数1
Complex B = new Complex(5,6); //复数2
float c = 7; //浮点数
test t1 = new test(A,c);
test t2 = new test(A,B);
}
}
class test{
private Complex A,B;
private float a;
test (Complex a,float b){ //实际上在构造函数里面做计算不建议
//给一个复数和一个浮点数
this.A = a;
this.a = b;
System.out.println("原始数据:");
System.out.println("复数:" + this.A + " " + "浮点数:" + this.a);
System.out.println("相加:" + this.A.Add(this.a));
System.out.println("相减:" + this.A.Sub(this.a));
System.out.println("相乘:" + this.A.Mul(this.a));
}
test (Complex a, Complex b){
this.A = a;
this.B = b;
System.out.println("原始数据:");
System.out.println("复数1:" + this.A + " " + "复数2:" + this.B);
System.out.println("相加:" + this.A.Add(this.B));
System.out.println("相减:" + this.A.Sub(this.B));
System.out.println("相乘:" + this.A.Mul(this.B));
}
}
class Complex {
private float real; //实部
private float imag; //虚部
Complex(){};
Complex(float a, float b){
this.real = a;
this.imag = b;
}
public Complex Add(float a){
float new_real = this.real + a;
return new Complex(new_real,this.imag);
}
public Complex Add(Complex com) {
float a = com.real;
float b = com.imag;
float new_real = this.real + a;
float new_imag = this.imag + b;
return new Complex(new_real,new_imag);
}
public Complex Sub(float a) {
float new_real = this.real - a;
return new Complex(new_real,this.imag);
}
public Complex Sub(Complex com) {
float a = com.real;
float b = com.imag;
float new_real = this.real - a;
float new_imag = this.imag - b;
return new Complex(new_real,new_imag);
}
public Complex Mul(float a) {
float new_real = this.real * a;
float new_imag = this.imag * a;
return new Complex(new_real,new_imag);
}
public Complex Mul(Complex com) {
float a = com.real;
float b = com.imag;
float new_real = this.real * a - this.imag * b;
float new_imag = this.real * b + this.imag * a;
return new Complex(new_real,new_imag);
}
@Override
public String toString() {
return this.real + " + " + this.imag + "i";
}
}
package java实验3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Well {
private int waterAmount;
public Well(int waterAmount) {
this.waterAmount = waterAmount;
}
public void takeWater(int amount) {
if (waterAmount >= amount) {
waterAmount -= amount;
System.out.println("取水 " + amount + " 升,剩余水量: " + waterAmount + " 升");
} else {
System.out.println("水量不足,取水失败");
}
}
}
class Family {
public int id;
private int waterUsage;
public Family(int id, int waterUsage) {
this.id = id;
this.waterUsage = waterUsage;
}
public void takeWater(Well well) {
System.out.println("家庭 " + id + " 开始取水 " + waterUsage + " 升");
well.takeWater(waterUsage);
}
}
public class Second {
public static void main(String[] args) {
int n = 18; // 村庄中的家庭数量
int wellWater = 100; // 井的水量
Well well = new Well(wellWater);
List<Family> families = new ArrayList<>();
// 创建家庭并添加到列表中
for (int i = 1; i <= n; i++) {
families.add(new Family(i, i));
}
// 随机打乱取水顺序
Collections.shuffle(families);
// 打印取水顺序并按顺序进行取水操作
System.out.println("取水顺序:");
for (Family family : families) {
System.out.println("家庭"+family.id);
}
for (Family family : families) {
family.takeWater(well);
}
}
}
实验四
(1):
交通工具有多种多样,比如:汽车、火车、飞机等,一般的交通工具类的属性有:厂商,发动机型号,制作型号,时间;方法有:运行(输出速度),停止。
1)请使用抽象方法实现交通工具类和方法:实现汽车、火车、飞机类,继承于交通工具类,实现他们不同的运行方法。
2)Tom在出门去,不知道会乘坐什么交通工具,利用用户的输入值进行随机派遣(mod 3 ,根据余数随机决定交通工具),要求程序利用上转型对象完成编程。
(2):
已知公司company有员工30人,分三类:WeekWorker每周580¥,MonthWorker月薪每月2500,YearWorker年薪每年12000。、
已知有Employee抽象类,有抽象方法earning()用于计算Employee一年的收入。有conmpany类,Employee[]存放公司员工,salariesPay方法计算并打印公司的所有一年薪水之处。
编写test类,company,employee, WeekWorker,MonthWorker,YearWorker,假定公司三类员工各自的数量用随机数来模拟,打印某公司一年的薪水支出。
package java实验4;
import java.util.Scanner;
// 抽象交通工具类
abstract class Vehicle {
protected String manufacturer; //生产商
protected String engineModel; //发动机型号
protected String modelNumber; //制作型号
protected String productionDate; //生产日期
public Vehicle(String manufacturer, String engineModel, String modelNumber, String productionDate) {
this.manufacturer = manufacturer;
this.engineModel = engineModel;
this.modelNumber = modelNumber;
this.productionDate = productionDate;
}
// 抽象运行方法
public abstract void run();
// 停止方法
public void stop() {
System.out.println("交通工具已停止。");
}
}
// 汽车类
class Car extends Vehicle {
public Car(String manufacturer, String engineModel, String modelNumber, String productionDate) {
super(manufacturer, engineModel, modelNumber, productionDate);
}
@Override
public void run() {
System.out.println("汽车正在以60km/h的速度行驶。");
}
}
// 火车类
class Train extends Vehicle {
public Train(String manufacturer, String engineModel, String modelNumber, String productionDate) {
super(manufacturer, engineModel, modelNumber, productionDate);
}
@Override
public void run() {
System.out.println("火车正在以120km/h的速度行驶。");
}
}
// 飞机类
class Airplane extends Vehicle {
public Airplane(String manufacturer, String engineModel, String modelNumber, String productionDate) {
super(manufacturer, engineModel, modelNumber, productionDate);
}
@Override
public void run() {
System.out.println("飞机正在以800km/h的速度飞行。");
}
}
public class First {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数(0汽车,1火车,2飞机): ");
int input = scanner.nextInt();
scanner.close();
Vehicle vehicle;
int randomNumber = input % 3;
switch (randomNumber) {
case 0:
vehicle = new Car("生产商:Honda", "发动机型号:V6", "生产商:Accord", "型号:2022-01-01");
break;
case 1:
vehicle = new Train("生产商:Siemens", "发动机型号:Electric", "生产商:ICE", "型号:2021-05-15");
break;
case 2:
vehicle = new Airplane("生产商:Boeing", "发动机型号:Jet", "生产商:747", "型号:2020-10-20");
break;
default:
System.out.println("无效的输入!");
return;
}
vehicle.run();
vehicle.stop();
}
}
package java实验4;
import java.util.Random;
abstract class Employee {
abstract double earning();
}
class WeekWorker extends Employee {
@Override
double earning() {
return 580 * 52; // 每周580元,一年52周
}
}
class MonthWorker extends Employee {
@Override
double earning() {
return 2500 * 12; // 每月2500元,一年12个月
}
}
class YearWorker extends Employee {
@Override
double earning() {
return 12000; // 年薪12000元
}
}
class Company {
Employee[] employees = new Employee[30];
Random random = new Random();
Company() {
long seed = 0; // 设置随机种子为0
random.setSeed(seed);
int weekWorkerCount = random.nextInt(30); // 周薪员工人数随机
int monthWorkerCount = random.nextInt(30 - weekWorkerCount); // 月薪员工人数随机
int yearWorkerCount = 30 - weekWorkerCount - monthWorkerCount; // 年薪员工人数
for (int i = 0; i < weekWorkerCount; i++) {
employees[i] = new WeekWorker();
}
for (int i = weekWorkerCount; i < weekWorkerCount + monthWorkerCount; i++) {
employees[i] = new MonthWorker();
}
for (int i = weekWorkerCount + monthWorkerCount; i < 30; i++) {
employees[i] = new YearWorker();
}
}
double salariesPay() {
double totalSalary = 0;
for (Employee employee : employees) {
totalSalary += employee.earning();
}
return totalSalary;
}
}
public class Second {
public static void main(String[] args) {
Company company = new Company();
double totalSalary = company.salariesPay();
System.out.println("公司一年的薪水支出为: " + totalSalary + " 元");
}
}
实验五
(1):
已知游戏中常见人物状态State,包括站立(Idle),行走(Run),攻击(Attack),死亡(Dead)。在游戏中有角色hero类,拥有人物状态同时还拥有血量,攻击力,防御力等属性,有怪物类Monster,怪物有血量,攻击力,防御力,怪物类型等属性。
已知角色可以变身为怪物,现在test类中,模拟某个角色在正常情况下以及在变身状态下的各种状态操作。
要求,使用接口及接口变量的引用实现。
(2)
利用java实现二维数组的查找
已知:一个二维数组m*n,每一行按照从左到右递增的顺序排序,每列按照从上到下递增排序,完成test类输入一个数组及一个数,Martix类构造出该二维数组,实现boolean Find(double)方法判定该二维数组是否含有该数,要求使用的时间复杂度最小。
package java实验5;
// 定义人物状态接口
interface State {
void action();
}
// 实现不同状态
class IdleState implements State {
@Override
public void action() {
System.out.println("站立状态");
}
}
class RunState implements State {
@Override
public void action() {
System.out.println("行走状态");
}
}
class AttackState implements State {
@Override
public void action() {
System.out.println("攻击状态");
}
}
class DeadState implements State {
@Override
public void action() {
System.out.println("死亡状态");
}
}
// 英雄类
class Hero {
private State state;
private int hp, attack, defense;
public Hero(int hp, int attack, int defense) {
this.hp = hp;
this.attack = attack;
this.defense = defense;
this.state = new IdleState(); // 初始状态为站立
}
public void changeState(State newState) {
this.state = newState;
}
public void performAction() {
state.action();
}
// 变身为怪物
public Monster transformToMonster() {
return new Monster(this.hp, this.attack, this.defense, "变身怪物");
}
}
// 怪物类
class Monster {
private State state;
private int hp, attack, defense;
private String type;
public Monster(int hp, int attack, int defense, String type) {
this.hp = hp;
this.attack = attack;
this.defense = defense;
this.type = type;
this.state = new IdleState(); // 初始状态为站立
}
public void changeState(State newState) {
this.state = newState;
}
public void performAction() {
state.action();
}
}
public class First {
public static void main(String[] args) {
Hero hero = new Hero(100, 20, 10); //100血,20攻击力,10防御力
System.out.println("英雄初始状态:");
hero.performAction();
// 切换英雄状态
hero.changeState(new RunState());
System.out.println("英雄行走状态:");
hero.performAction();
hero.changeState(new AttackState());
System.out.println("英雄攻击状态:");
hero.performAction();
hero.changeState(new DeadState());
System.out.println("英雄死亡状态:");
hero.performAction();
// 变身为怪物
Monster monster = hero.transformToMonster();
System.out.println("变身后的怪物状态:");
monster.performAction();
monster.changeState(new RunState());
System.out.println("怪物行走状态:");
monster.performAction();
monster.changeState(new AttackState());
System.out.println("怪物攻击状态:");
monster.performAction();
monster.changeState(new DeadState());
System.out.println("怪物死亡状态:");
monster.performAction();
}
}
package java实验5;
import java.util.Scanner;
public class Second {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入二维数组的行数: ");
int m = scanner.nextInt();
System.out.print("输入二维数组的列数: ");
int n = scanner.nextInt();
double[][] array = new double[m][n];
System.out.println("输入二维数组的元素:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array[i][j] = scanner.nextDouble();
}
}
System.out.print("输入要查找的数: ");
double target = scanner.nextDouble();
scanner.close();
Matrix matrix = new Matrix(array);
boolean found = matrix.Find(target);
System.out.println(found ? "找到该数" : "未找到该数");
}
}
class Matrix {
private double[][] array;
public Matrix(double[][] array) {
this.array = array;
}
public boolean Find(double target) {
int m = array.length;
int n = array[0].length;
int row = 0;
int col = n - 1;
while (row < m && col >= 0) {
if (array[row][col] == target) {
return true;
} else if (array[row][col] > target) {
col--;
} else {
row++;
}
}
return false;
}
}
点个赞,继续更新😉
319

被折叠的 条评论
为什么被折叠?



