java编程题高分求解,要详细,别打酱油,100分够不够,不够再加
发布网友
发布时间:2022-05-27 12:55
我来回答
共3个回答
热心网友
时间:2023-10-23 20:14
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
System.out.println("----------第一题---------");
fun1();
System.out.println("----------第二题---------");
fun2();
System.out.println("----------第三题---------");
fun3();
}
// 第一题
public static void fun1() {
int x = 5;
for (int i = 1; i <= x; i++) {
System.out.print("a" + i + "=" + loops(i) + ",\t");
}
System.out.println();
}
public static int loops(int x) {
if (x == 1) {
return 3;
}
if (x == 2) {
return 8;
}
int result = 2 * loops(x - 1) + 2 * loops(x - 2);
return result;
}
// 第二题
public static void fun2() {
Random r = new Random();
TreeMap<Student, Student> map = new TreeMap<Student, Student>(
new MyComparator());
for (int i = 0; i < 10; i++) {
Student s = new Student();
s.name = "name" + (i + 1);
s.source = r.nextInt(100);
map.put(s, s);
}
Iterator<Student> iter = map.keySet().iterator();
while (iter.hasNext()) {
Student s = iter.next();
System.out.println(s.name + "," + s.source);
}
}
static class Student {
String name = null;
int source = 0;
}
static class MyComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
return o2.source - o1.source;
}
}
// 第三题
public static void fun3() {
Random r = new Random();
String[] names = { "联想", "三星", "苹果", "HP" };
int[] prices = { 60, 80, 140, 220 };
String[] spaces = { "4G", "8G", "16G", "32G" };
TreeMap<UDriver, UDriver> map = new TreeMap<UDriver, UDriver>(
new UDriverComparator());
while (true) {
UDriver u = new UDriver();
u.name = names[r.nextInt(4)];
int index = r.nextInt(100) % 4;
u.space = spaces[index];
u.price = prices[index];
if (!map.containsValue(u)) {
map.put(u, u);
}
if (map.size() == 4) {
break;
}
}
Iterator<UDriver> iter = map.keySet().iterator();
while (iter.hasNext()) {
UDriver s = iter.next();
System.out.println(s.name + "," + s.space + "," + s.price);
}
}
static class UDriver {
String name = null;
int price = 0;
String space = null;
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return equals((UDriver) obj);
}
public boolean equals(UDriver obj) {
return this.price == obj.price && this.space.equals(obj.space)
&& this.name.equals(obj.name);
}
}
static class UDriverComparator implements Comparator<UDriver> {
@Override
public int compare(UDriver o1, UDriver o2) {
// TODO Auto-generated method stub
return o2.price - o1.price;
}
}
}
热心网友
时间:2023-10-23 20:15
这是要纯手动写代码还是拿思路?
先告诉你思路:
第一个题目肯定是用递归去做的,。
第二个是关于集合排序的问题。
第三个。有键值对还不好?将属性分别保存,再根据键来排。然后再根据值来排。。。
热心网友
时间:2023-10-23 20:15
已发送到邮箱... ,请检查...追问哪里呢?好像没收到啊