java中的Sort函数,你值得看
2021-07-03 05:06
94.50代码实现如下:
package package1111;import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;class MoonCake
{
int quantity;
int money;
double unitprice;
}
class cmp implements Comparator
{
public int compare(MoonCake A , MoonCake B)
{
if(A.unitprice {
return 1;
}
else if(A.unitprice == B.unitprice)
{
return 0;
}
else
{
return -1;
}
}
}
public class Main
{
public static MoonCake num[] = new MoonCake[1005];
public static void main(String []args)
{
Scanner cin = new Scanner(System.in);
int N,D;
N = cin.nextInt();
D = cin.nextInt();
for(int i = 0 ; i {
num[i] = new MoonCake();
num[i].quantity = cin.nextInt();
}
for(int i = 0 ; i {
num[i].money = cin.nextInt();
num[i].unitprice = num[i].money*1.0/num[i].quantity;
}
Arrays.sort(num,0,N,new cmp());
double sum = 0;
for(int i = 0 ; i {
if(num[i].quantity {
sum += num[i].money;
D = D - num[i].quantity;
continue;
}
else if(num[i].quantity == D)
{
sum += num[i].money;
}
else
{
sum += D*num[i].unitprice;
}
break;
}
String output = String.format("%.2f", sum);
System.out.print(output);
}
}
文章标题:java中的Sort函数,你值得看
文章链接:http://soscw.com/index.php/essay/101118.html