第一题:
比较两个元素的大小,在程序编写时会经常遇到,特别是在进行元素排序时,“比大小”必不可少。编程高手们总喜欢想着“一劳永逸”,编写一个程序可以适用不同情况,而且容易拓展。小王同学以编程高手为目标,想编写一个类,其中包含了各种各样的“比大小”方法,而且名称全部相同,调用形式也基本一样,小王同学已经将程序的主类设计完成,并添加了注释,请你帮他完善各个功能类的代码。
//比较两个对象的大小,每个类的对象大小比较规则不尽相同
import java.util.Scanner;
public class Main {
public static Scanner reader=new Scanner(System.in);
public static void main(String[] args) {
Main ct=new Main();
Point p1=new Point(),p2=new Point();//定义两个Point型对象
ct.input(p1,p2);//输入p1和p2对象的成员变量值
System.out.println(ct.compare(p1,p2));//输入对象p1和p2的比较结果,如果p1大,输出true,否则输出false
Fraction f1=new Fraction(),f2=new Fraction();
f1.input(); f2.input();
System.out.println(ct.compare(f1,f2));
MyArray a1=new MyArray();
MyArray a2=new MyArray();
ct.input(a1, a2);
System.out.println(ct.compare(a1,a2));
}
void input(Point a,Point b){
a.input(); b.input();
}
void input(Fraction a,Fraction b){
a.input(); b.input();
}
void input(MyArray a,MyArray b){
a.input(); b.input();
}
public boolean compare(Point a,Point b){
if(a.bigTo(b))return true;
else return false;
}
public boolean compare(Fraction f1,Fraction f2){
if(f1.compare(f2)==true)return true;
else return false;
}
public boolean compare(MyArray m1,MyArray m2){
if(MyArray.compare(m1,m2)>=0)return true;
else return false;
}
}
class Point{
int x,y;
public void input(){
Scanner reader=Main.reader;
x=reader.nextInt(); y=reader.nextInt();
}
public boolean bigTo(Point p){
if(this.xthis.y>=p.xp.y)return true;
else return false;
}
}
说明1:Fraction类的对象大小比较规则:根据分数值的大小进行比较;MyArray类的对象大小比较规则:认为对应位置大于对方数组元素的个数多于小于对方数组元素个数的那个数组更大,若元素缺失,则补0。
说明2:所有数据都是合法的
说明3:在Fraction类与MyArray类中读入数据时必须使用Main.reader对象作为数据输入对象。
输入
两个点,两个分数,两个数组
输出
三个判断结果
样例输入 Copy
1 2
3 4
1 2
3 4
2
1 2
3
2 1 -1
样例输出 Copy
false
false
true
import java.util.Scanner;
public class Main {
public static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
Main ct = new Main();
Point p1 = new Point(), p2 = new Point();// 定义两个Point型对象
ct.input(p1, p2);// 输入p1和p2对象的成员变量值
System.out.println(ct.compare(p1, p2));// 输入对象p1和p2的比较结果,如果p1大,输出true,否则输出false
Fraction f1 = new Fraction(), f2 = new Fraction();
f1.input();
f2.input();
System.out.println(ct.compare(f1, f2));
MyArray a1 = new MyArray();
MyArray a2 = new MyArray();
ct.input(a1, a2);
System.out.println(ct.compare(a1, a2));
}
void input(Point a, Point b) {
a.input();
b.input();
}
void input(Fraction a, Fraction b) {
a.input();
b.input();
}
void input(MyArray a, MyArray b) {
a.input();
b.input();
}
public boolean compare(Point a, Point b) {
if (a.bigTo(b))
return true;
else
return false;
}
public boolean compare(Fraction f1, Fraction f2) {
if (f1.compare(f2) == true)
return true;
else
return false;
}
public boolean compare(MyArray m1, MyArray m2) {
if (MyArray.compare(m1, m2) >= 0)
return true;
else
return false;
}
}
class Point {
int x, y;
public void input() {
Scanner reader = Main.reader;
x = reader.nextInt();
y = reader.nextInt();
}
public boolean bigTo(Point p) {
if (this.x * this.y >= p.x * p.y)
return true;
else
return false;
}
}
class Fraction{
double x, y;
public void input() {
Scanner reader = Main.reader;
x = reader.nextDouble();
y = reader.nextDouble();
}
public boolean compare(Fraction f) {
if (this.x / this.y >= f.x / f.y)
return true;
else
return false;
}
}
class MyArray{
int[] x;
int n;
public void input() {
Scanner reader = Main.reader;
int n;
n = reader.nextInt();
x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = reader.nextInt();
}
}
public static int compare(MyArray m1, MyArray m2) {
int max = Math.max(m1.x.length, m2.x.length);
int mark1 = 0;
int mark2 = 0;
for (int i = 0; i < max; i++) {
int n = m1.x.length > i ? m1.x[i] : 0;
int m = m2.x.length > i ? m2.x[i] : 0;
if(n > m) {
mark1 += 1;
}else if(n<m) {
mark2 += 1;
}
}
return mark1-mark2;
}
}
第二题:
冬奥会自由式滑雪大跳台决赛的计分规则是:六名裁判给分(0-100分,整数分数),去掉一个最高分,去掉一个最低分,取平均值作为(除以4.0)参数选手得分(double类型);每位选手可以出场三次,取最高的两次得分和作为该选手的最终总分。
假设有12名选手参加自由式滑雪大跳台决赛,请你编写Java程序,根据每位选手每次出场的得分,计算选手总分,并按照总分降序排列输出选手的基本信息。如果总分相同,则按姓名升序排序。
说明1:字符串s与t比较大小使用s.compareTo(t),若返回值<0,则s串<t串,若返回值>0,则s串>t串,若返回值为0,则s、t串相等。
说明2:计算每次的平均分时记得除以4.0而不是4,否则会视为整除,无法得到正确的结果
输入
每个选手姓名、国家、三次六位裁判的评分
输出
按选手总分降序输出每个选手的姓名、国家与总分
样例输入 Copy
a5
SUI
92 35 100 7 65 6
65 90 4 78 43 45
61 5 35 51 52 62
a1
DEN
73 54 94 64 39 38
63 35 48 61 91 52
40 68 47 59 82 30
a9
CHN
49 62 78 84 79 37
52 5 38 14 33 39
13 44 13 25 87 42
a12
JPN
61 13 75 51 89 35
89 85 33 6 62 8
60 50 55 54 24 9
a6
USA
99 52 8 48 47 9
85 37 64 22 26 7
36 29 41 67 89 81
a4
GER
5 68 66 34 32 17
48 50 71 51 37 65
59 28 51 28 64 86
a11
USA
64 41 9 14 96 94
29 89 33 20 60 13
22 88 28 36 74 28
a3
AUS
86 48 70 19 1 38
17 14 89 74 81 40
56 39 42 71 47 47
a8
CHN
18 71 46 78 51 23
94 77 85 76 90 7
59 46 66 59 63 29
a10
GBR
6 80 41 64 47 56
0 5 68 77 95 6
85 33 96 97 72 31
a2
ISL
95 96 11 44 12 41
36 6 68 71 89 7
31 41 16 3 82 35
a7
SWE
9 77 79 93 21 93
88 24 92 55 56 56
97 27 27 13 98 15
样例输出 Copy
a8,CHN,138.75
a7,SWE,131.25
a10,GBR,123.5
a1,DEN,113.5
a5,SUI,107.5
a4,GER,104.0
a12,JPN,102.5
a3,AUS,101.0
a9,CHN,98.0
a6,USA,95.25
a11,USA,94.75
a2,ISL,93.5
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Player[] players = new Player[12];
PlayerOperator opertator = new PlayerOperator();
opertator.input(players);
opertator.print(players);
}
}
class Player {
private String name;
private String country;
private double finalScore;
public Player() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getFinalScore() {
return finalScore;
}
public void setFinalScore(int[] scores1, int[] scores2, int[] scores3) {
int max1 = scores1[0];
int min1 = scores1[0];
int allScore1 = scores1[0];
for (int i = 1; i < scores1.length; i++) {
if(max1 < scores1[i]) {
max1 = scores1[i];
}
if(min1 > scores1[i]) {
min1 = scores1[i];
}
allScore1 += scores1[i];
}
double a1 = 1.0*(allScore1-max1-min1)/(scores1.length-2);
int max2 = scores2[0];
int min2 = scores2[0];
int allScore2 = scores2[0];
for (int i = 1; i < scores2.length; i++) {
if(max2 < scores2[i]) {
max2 = scores2[i];
}
if(min2 > scores2[i]) {
min2 = scores2[i];
}
allScore2 += scores2[i];
}
double a2 = 1.0*(allScore2-max2-min2)/(scores2.length-2);
int max3 = scores3[0];
int min3 = scores3[0];
int allScore3 = scores3[0];
for (int i = 1; i < scores3.length; i++) {
if(max3 < scores3[i]) {
max3 = scores3[i];
}
if(min3 > scores3[i]) {
min3 = scores3[i];
}
allScore3 += scores3[i];
}
double a3 = 1.0*(allScore3-max3-min3)/(scores3.length-2);
double b = Math.min(a1, a2);
double c = Math.min(a3, b);
finalScore = a1+a2+a3-c;
}
}
class PlayerOperator {
public void input(Player[] players) {
Scanner reader = new Scanner(System.in);
int[] scores1 = new int[6];
int[] scores2 = new int[6];
int[] scores3 = new int[6];
for (int i = 0; i < 12; i++) {
Player p = new Player();
p.setName(reader.next());
p.setCountry(reader.next());
for (int j = 0; j < scores1.length; j++) {
scores1[j] = reader.nextInt();
}
for (int j = 0; j < scores2.length; j++) {
scores2[j] = reader.nextInt();
}
for (int j = 0; j < scores3.length; j++) {
scores3[j] = reader.nextInt();
}
p.setFinalScore(scores1, scores2,scores3);
players[i] = p;
}
}
public void print(Player[] players) {
for (int i = 0; i < players.length-1; i++) {
Player p = new Player();
for (int j = 0; j < players.length-1-i; j++) {
if(players[j].getFinalScore()>players[j+1].getFinalScore()) {
p = players[j];
players[j] = players[j+1];
players[j+1] = p;
}
if(players[j].getFinalScore()==players[j+1].getFinalScore()&&players[j].getName().compareTo(players[j+1].getName())<0) {
p = players[j];
players[j] = players[j+1];
players[j+1] = p;
}
}
}
for (int i = players.length-1; i >= 0; i--) {
System.out.println(players[i].getName()+","+ players[i].getCountry()+"," +players[i].getFinalScore());
}
}
}
第三题:
某村庄有一口公共水井,为了保护该水井及饮用水的质量,当地规定当该水井内的水量不高于100时,禁止取水6小时,在此期间,水井水量自动恢复到最大容量1000;如果预计村民当次取水后水量低于100,则拒绝该村民本次取水。
其实每个村民自家也有口水井,自家的水井容量各不相同,范围在(100,200]内。村民优先去公共水井取水,若发现公共水井禁止取水或因容量不够拒绝取水时,再从自家水井取水。
自家水井容量不高于100时,禁止取水1小时,在此期间,水井水量自动恢复到初始水量;如果预计村民当次取水后水量低于100,则允许该村民本次从自家水井取水至水井容量到100。
请模拟两个村民用水情况:先设置公共水井水量及两位村民自家水井容量,再模拟村民1取水,村民2取水,村民1再次取水,村民2再次取水的情形。村民每次取水时需输入年、月、日、时、分、秒和取水量,若成功从公共水井取水则输出“public:m”形式,m表示公共水井剩余水量;若成功从自家水井取水则输出“private:n”形式,n表示自家水井剩余水量; 若没有取到水,则输出“wait”。
输入
公共水井初始水量(可以比1000小),两位村民自家水井水量(最大容量,也是初始水量),两位村民两次交替取水情况
输出
每次取水后的情况
样例输入 Copy
1000 150 200
2022 3 1 18 25 30 800
2022 3 1 20 25 20 150
2022 3 1 23 25 19 100
2022 3 2 6 25 25 150
样例输出 Copy
public:200
private:100
public:100
public:850
import java.time.LocalDateTime;
import java.util.Scanner;
public class Main {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Well.setPubWater(input.nextInt());
Well people1 = new Well();
Well people2 = new Well();
people1.setPriWater(input.nextInt());
people2.setPriWater(input.nextInt());
int[] time = new int[6];
for (int i = 0; i < 2; i++) {
LocalDateTime n1 = LocalDateTime.of(input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt());
int wantUse = input.nextInt();
if(!people1.usePub(n1, wantUse)) {
people1.usePri(n1, wantUse);
}
LocalDateTime n2 = LocalDateTime.of(input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt());
int wantUse2 = input.nextInt();
if(!people2.usePub(n2, wantUse2)) {
people2.usePri(n2, wantUse2);
}
}
}
}
class Well {
private static int pubWater;
private static int pubMark = 1000;
private static String pubCondition = "open";
private static LocalDateTime pubTime;
private int priWater;
private int priMark;
private String priCondition = "open";
private LocalDateTime priTime;
public static int getPubWater() {
return pubWater;
}
public static void setPubWater(int pubWater) {
Well.pubWater = pubWater;
}
public int getPriWater() {
return priWater;
}
public void setPriWater(int priWater) {
priMark = priWater;
this.priWater = priWater;
}
public boolean usePub(LocalDateTime time, int wantUse) {
boolean mark;
if (pubCondition.equals("open") || pubCondition.equals("close") && time.isAfter(pubTime)) {
pubCondition = "open";
if (pubWater - wantUse >= 100) {
pubWater -= wantUse;
System.out.println("public:" + pubWater);
mark = true;
} else {
mark = false;
}
} else {
mark = false;
}
if (pubWater == 100) {
pubTime = time.plusHours(6);
pubWater = pubMark;
pubCondition = "close";
}
return mark;
}
public void usePri(LocalDateTime time, int wantUse) {
if (priCondition.equals("open") || priCondition.equals("close") && time.isAfter(priTime)) {
priCondition = "open";
if (priWater > 100) {
if (priWater - wantUse >= 100) {
priWater -= wantUse;
System.out.println("private:" + priWater);
} else {
priWater = 100;
System.out.println("private:" + priWater);
}
}
}else {
System.out.println("wait");
}
if (priWater == 100) {
priTime = time.plusHours(1);
priWater = priMark;
priCondition = "close";
}
}
}
第四题:
一元二次方程的根,在整数范围内无解时,可以扩展到在实数范围内求解,如果在实数范围内无解,还可以扩展到复数范围内求解。请你根据一个一元二次方程的系数,求其在各类数范围内的解。
输入
n和n组一元二次方程的系数(整数)
输出
整数、实数(输出四舍五入保留2位小数)、复数(实部、虚部为小数时输出四舍五入保留2位小数,实部虚部的小数位是0时输出其整数部分)范围内的解,有整数解时不必输出实数、复数解,有实数解时不必输出复数解;若两个解相同,只输出1个,若两个解不同,按降序输出,本题定义复数的大小关系时不考虑虚数单位i。
样例输入 Copy
4
1 1 1
1 2 1
1 3 1
1 0 1
样例输出 Copy
Complex:-0.50+0.87i,-0.50-0.87i
Integer:-1
Real:-0.38,-2.62
Complex:i,-i
值得一提的是不用考虑a是否等于0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a, b, c;
for (int i = 0; i < n; i++) {
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
Tools.solution(a, b, c);
}
input.close();
}
}
class Tools{
public static void solution(int a, int b, int c) {
double delta = 1.0 * b * b - 4.0 * a * c;
double r1;
double r2;
//if (a != 0) {
if (delta > 0) {
r1 = (-1.0 * b + Math.sqrt(delta)) / (2.0 * a);
r2 = (-1.0 * b - Math.sqrt(delta)) / (2.0 * a);
if (r1 - (int) r1 == 0 && r2 - (int) r2 == 0) {
System.out.printf("Integer:%d,%d\n", (int) r1, (int) r2);
} else if (r1 - (int) r1 != 0 && r2 - (int) r2 == 0) {
System.out.printf("Integer:%d\n", (int) r2);
} else if (r1 - (int) r1 == 0 && r2 - (int) r2 != 0) {
System.out.printf("Integer:%d\n", (int) r1);
} else {
System.out.printf("Real:%.2f,%.2f\n", r1, r2);
}
} else if (delta == 0) {
r1 = -1.0 * b / (2.0 * a);
if (r1 - (int) r1 == 0) {
System.out.printf("Integer:%d\n", (int) r1);
} else {
System.out.printf("Real:%.2f\n", r1);
}
} else if (delta < 0) {
delta *= -1;
double real = -1.0 * b / (2.0 * a);
double imaginary = Math.sqrt(delta) / (2.0 * a);
if (real == 0) {
if (imaginary - (int) imaginary == 0) {
if (imaginary == 1) {
System.out.printf("Complex:i,-i\n");
} else {
System.out.printf("Complex:%di,-%di\n", (int) imaginary, (int) imaginary);
}
} else {
System.out.printf("Complex:%.2fi,%.2fi\n", imaginary, -1 * imaginary);
}
} else {
if (real - (int) real == 0) {
if (imaginary - (int) imaginary == 0) {
if (imaginary == 1) {
System.out.printf("Complex:%d+i,%d-i\n", (int) real, (int) real);
} else {
System.out.printf("Complex:%d+%di,%d-%di\n", (int) real, (int)imaginary, (int) real,
(int)imaginary);
}
} else {
System.out.printf("Complex:%d+%.2fi,%d-%.2fi\n", (int) real, imaginary, (int) real,
imaginary);
}
} else {
if (imaginary - (int) imaginary == 0) {
System.out.printf("Complex:%.2f+%di,%.2f-%di\n", real, (int) imaginary, real,
(int) imaginary);
} else {
System.out.printf("Complex:%.2f+%.2fi,%.2f-%.2fi\n", real, imaginary, real, imaginary);
}
}
}
}
// } else {
// r1 = -1.0 * c / b;
// if (r1 - (int) r1 == 0&&b!=0) {
// System.out.printf("Integer:%d\n", (int) r1);
// } else if(r1 - (int) r1 != 0&&b!=0){
// System.out.printf("Real:%.2f\n", r1);
// }else {
// System.out.printf("Integer:0\n");
// }
// }
}
}
第五题:
定义一个小车类Car,其初始位置为(0,0),属性包括:当前位置,行驶速度,行驶朝向(东1、南2、西3、北4);方向变化:直行(1)、左转(2)、右转(3);速度变化(正数为加速,负数为减速)。
约束条件:(1)采用直角坐标系,上北下南左西右东;(2)速度为0时,车辆停止,减速无效;(3)速度达到20时,加速无效。
输入
小车的初始位置(x,y)、初始速度(v)和初始朝向(f),时刻数量(<=1000),每个时刻内的方向变化(c)、速度变化(s)
输出
当前小车的位置
样例输入 Copy
0 0 0 1
10
1 1 1 1 1 1 1 0 1 0 1 0 2 0 1 0 1 2 1 -1
样例输出 Copy
15,15
import java.util.Scanner;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
CarOperator operator = new CarOperator();
operator.input_();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
operator.changeDirection( sc.nextInt());
operator.changeSpeed( sc.nextInt());
operator.changePosition();
}
operator.print();
}
}
class CarOperator{
private Car car = new Car();
public void input_(){
car.setX( Main.sc.nextInt());
car.setY( Main.sc.nextInt());
car.setV( Main.sc.nextInt());
car.setF( Main.sc.nextInt());
}
public void changeDirection(int c) {
switch(car.getF()) {
case 1:if(c == 2) {
car.setF(4);
}else if(c == 3) {
car.setF(2);
}
break;
case 2:if(c == 2) {
car.setF(1);
}else if(c == 3) {
car.setF(3);
}
break;
case 3:if(c == 2) {
car.setF(2);
}else if(c == 3) {
car.setF(4);
}
break;
case 4:if(c == 2) {
car.setF(3);
}else if(c == 3) {
car.setF(1);
}
break;
}
}
public void changeSpeed(int s){
car.setV(car.getV()+s);
if(car.getV() > 20) {
car.setV(20);
}
if(car.getV() < 0) {
car.setV(0);
}
}
public void changePosition() {
switch(car.getF()) {
case 1:car.setX(car.getX()+car.getV());
break;
case 2:car.setY(car.getY()-car.getV());
break;
case 3:car.setX(car.getX()-car.getV());
break;
case 4:car.setY(car.getY()+car.getV());
break;
}
}
public void print() {
System.out.println(car.getX() + "," + car.getY());
}
}
class Car{
private int x;
private int y;
private int v;
private int f;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public int getF() {
return f;
}
public void setF(int f) {
this.f = f;
}
}