62,623
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception {
int[] arr = { 9, 8, 7, 10, 23, 45, 6, 2, 1, 0, 7, 8, 5, 2 };
//將三個臨時變量設置為數組的前三個數
int a = arr[0];
int b = arr[1];
int c = arr[2];
//使這三個變量有序
if (c > b) {
b = b ^ c;
c = b ^ c;
b = b ^ c;
}
if (b > a) {
a = b ^ a;
b = b ^ a;
a = b ^ a;
}
//遍歷數組,與臨時變量進行比較,並始終保持三個臨時變量有序
for (int i : arr) {
if (i > a) {
c = b;
b = a;
a = i;
}
if (i < a && i > b) {
c = b;
b = i;
}
if (i < b && i > c) {
c = i;
}
}
System.out.println(a + " " + b + " " + c);
}
LinkedList<Stock> stk = new LinkedList<Stock>();
...
List<Integer> idxList = new ArrayList<Integer>(); //用于记录stk的相关下标
double maxPrice = 0; //临时最大price
for (int i=0; i<stk.size(); i++) { //遍历寻找最大
if (maxPrice < stk.get(i).getPrice()) { //当找到某个大price的时候
idxList.add(0, i); //把该大price的下标保存
maxPrice = stk.get(i).getPrice(); //并让最大price=当前price
}
}
int idx = 0;
for (idx=0; idx<Math.min(3, idxList.size()); idx++) { //遍历下标集合打印
System.out.println(stk.get(idxList.get(idx)));
}
for (int i=0; idx<3; i++, idx++) { //如果找到的最大小于3个,说明其他的都是相同的price
if (! idxList.contains(i)) { //继续打印其他price,直到达到3个
System.out.println(stk.get(i)));
}
}
import java.util.*;
import com.sun.org.apache.bcel.internal.generic.SWAP;
public class FindTopThree {
public static void main(String[] args) {
LinkedList<Stock> stk = new LinkedList<Stock>();
for (int i = 0; i < 10; i++)
// 随机产生10个1到100之间的price建立Stock对象,并添加进List中
stk.add(new Stock((int) (Math.random() * 100) + 1));
System.out.println("========原始数据========");
System.out.println(stk);
Stock[] stocks = new Stock[] { stk.get(0), stk.get(1), stk.get(2) };// 假设前三个stock最大
for (int i = 3; i < stk.size(); i++) {
Stock s = stk.get(i);
if (s.price() > stocks[0].price) {
Stock temp = s;
s = stocks[0];
stocks[0] = temp;
}
;// 如果大于数组第0个Stock
if (s.price() > stocks[1].price) {
Stock temp = s;
s = stocks[1];
stocks[1] = temp;
}
;// 如果大于数组第1个Stock
if (s.price() > stocks[2].price) {
Stock temp = s;
s = stocks[2];
stocks[2] = temp;
}
;// 如果大于数组第2个Stock
}
System.out.println("========最大三个========");
System.out.println(Arrays.toString(stocks));
}
}
class Stock {
int price = 0;
Stock(int price) {
this.price = price;
}
int price() {
return price;
}
public String toString() {
return "" + price;
}
}
/*
========原始数据========
[1, 53, 95, 18, 55, 61, 9, 73, 71, 35]
========最大三个========
[73, 71, 95]
*/
import java.util.*;
public class FindTopThree {
public static void main(String[] args) {
LinkedList<Stock> stk = new LinkedList<Stock>();
for(int i=0; i<10; i++)//随机产生10个1到100之间的price建立Stock对象,并添加进List中
stk.add(new Stock((int) (Math.random()*100)+1));
System.out.println("========原始数据========");
System.out.println(stk);
Stock [] stocks = new Stock[]{stk.get(0),stk.get(1),stk.get(2)};//假设前三个stock最大
for(int i = 3; i< stk.size(); i++) {
Stock s = stk.get(i);
if(s.price()>stocks[0].price) stocks[0] = s;//如果大于数组第0个Stock
else if(s.price()>stocks[1].price) stocks[1] = s;//如果大于数组第1个Stock
else if(s.price()>stocks[2].price) stocks[2] = s;//如果大于数组第2个Stock
else ;//什么都不做
}
System.out.println("========最大三个========");
System.out.println(Arrays.toString(stocks));
}
}
class Stock{
int price = 0;
Stock(int price) {
this.price = price;
}
int price() {
return price;
}
public String toString() {
return "" + price;
}
}
/*output:
========原始数据========
[78, 39, 72, 95, 38, 16, 99, 40, 88, 60]
========最大三个========
[99, 88, 72]
*/
LinkedList<Stock> stk = new LinkedList<Stock>()
Collections.sort(stk, new Comparator<Stock>() {
public int compare(Stock s1, Stock s2) {
return (int) (s2.getPrice() - s1.getPrice())
}
});
stk.get(0), stk.get(1), stk.get(2)
如果不对改成s1.getPrice() - s2.getPrice()