用Java完成下列题目

用Java完成下列题目,第1张

反正最多也才比100次 不如直接穷举完吧

代码如下:

import java.util.*

public class test {

public static void main(String args[]) {

Scanner in = new Scanner(System.in)

int n = in.nextInt()

int na = in.nextInt()

int nb = in.nextInt()

int[] a = new int[n]

int[] b = new int[n]

int[] aS = new int[na]//a序列

int[] bS = new int[nb]//b序列

for (int i=0i<nai++) {

aS[i] = in.nextInt()

}

for (int i=0i<nbi++) {

bS[i] = in.nextInt()

}

for (int i=0i<ni++) {

a[i] = aS[i%na]

}

for (int i=0i<ni++) {

b[i] = bS[i%nb]

}

//比较a 和 b

int aScore = 0, bScore = 0

for (int i=0i<ni++) {

if (compare(a[i], b[i]) > 0) {

aScore++

} else if (compare(a[i], b[i]) < 0) {

bScore++

}

}

if (aScore == bScore) {

System.out.println("draw")

} else if (aScore > bScore) {

System.out.println("A")

} else if (aScore < bScore) {

System.out.println("B")

}

}

//返回1代表胜利 -1代表失败 0平局

private static int compare(int i, int j) {

if (i == 0) {

if (j == 0) {

return 0

} else if (j == 2) {

return 1

} else if (j == 5){

return -1

}

} else if (i == 2) {

if (j == 0) {

return -1

} else if (j == 2) {

return 0

} else if (j == 5){

return 1

}

} else if (i == 5) {

if (j == 0) {

return 1

} else if (j == 2) {

return -1

} else if (j == 5){

return 0

}

}

return 0

}

}

public class Person {

private String name//姓名

private String sex//性别

public void sayHello() {

System.out.println("姓名:" + name)

System.out.println("性别:" + sex)

}

public Person() {

}

public Person(String name, String sex) {

this.name = name

this.sex = sex

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getSex() {

return sex

}

public void setSex(String sex) {

this.sex = sex

}

}

public class Student extends Person {

private String num//学号

private String school//学校

public void sayHello() {

super.sayHello()

System.out.println("学号:" + num)

System.out.println("学校:" + school)

}

public Student(String num, String school) {

this.num = num

this.school = school

}

public Student(String name, String sex, String num, String school) {

super(name, sex)

this.num = num

this.school = school

}

public Student() {

}

public String getNum() {

return num

}

public void setNum(String num) {

this.num = num

}

public String getSchool() {

return school

}

public void setSchool(String school) {

this.school = school

}

}

public class Test {

public static void main(String[] args) {

Student stu1 = new Student()

stu1.setName("张三")

stu1.setSex("男")

stu1.setNum("20211225001")

stu1.setSchool("北京大学")

Student stu2 = new Student("20211225002", "北京大学")

stu2.setName("李四")

stu2.setSex("男")

Student stu3 = new Student("王五", "女", "20211225003", "清华大学")

Person person1 = new Person()

person1.setName("赵六")

person1.setSex("女")

Person person2 = new Person("孙七", "女")

stu1.sayHello()

stu2.sayHello()

stu3.sayHello()

person1.sayHello()

person2.sayHello()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12167145.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存