思路
1. 给月饼存需求在moon[]里面, 单价存在price[]里面,
2. 根据price对进行排序,单价高的在前面
3. 从0开始所有月饼种类
for(i = 0; i < n && m > 0; i++){ // 若剩余0(m=0)则 退出
if(moon[i] <= m){
sum += price[i]*moon[i];
m -= moon[i];
}else if(moon[i] > m){
sum += price[i] * m;
m = 0;
}
4. sum及最大数
java堆实现 (java会超时)
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static class Moon{
int moon;
double price;
public Moon(int moon, double price) {
super();
this.moon = moon;
this.price = price;
}
public Moon() {
super();
}
@Override
public String toString() {
return "Moon [moon=" + moon + ", price=" + price + "]";
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Queue<Moon> que = new PriorityQueue<>(n, pricCompare);
sc.nextLine();
Moon[] moons = new Moon[n];
for(int i = 0; i < n; i++){
Moon moon = new Moon();
moon.moon = sc.nextInt();
moons[i] = moon;
}
for(int i = 0; i < n; i++){
moons[i].price =sc.nextInt()*1.0/moons[i].moon;
que.add(moons[i]);
}
sc.close();
double sum = 0;
for(int i =0; i < n && m > 0; i++){
Moon moon = que.poll();
if (moon.moon >= m){
sum += moon.price*m;
m -= moon.moon;
}else{
sum += moon.price * moon.moon;
m -= moon.moon;
}
}
System.out.printf("%.2f", sum);
}
public static Comparator<Moon> pricCompare = new Comparator<Moon>() {
@Override
public int compare(Moon arg0, Moon arg1) {
int res = arg0.price >= arg1.price?-1:1;
return res;
}
};
}
快速排序(递归,超时了)
#include <stdio.h>
#include <stdlib.h>
static int moon[1001];
static float price[1001];
void mysort(int start, int end){
int i = start, j = end;
if(start >= end){
return ;
}
while(i < j){
while(i < j && price[i] > price[j]){
i++;
}
if(i == j){
break;
}else if(i < j && price[i] < price[j]){
float tmp, tmp1;
tmp = price[j];
tmp1 = moon[j];
price[j] = price[i];
price[i] = tmp;
moon[j] = moon[i];
moon[i] = tmp1;
j--;
}
while(j > i && price[i] > price[j]){
j--;
}
if(i == j){
break;
}else if(i < j && price[i] < price[j]){
float tmp, tmp1;
tmp = price[j];
tmp1 = moon[j];
price[j] = price[i];
price[i] = tmp;
moon[j] = moon[i];
moon[i] = tmp1;
i++;
}
}
mysort(start, i-1);
mysort(i+1, end);
}
int main()
{
int n;
int m;
scanf("%d %d\n", &n, &m);
int i;
for(i = 0; i < n; i++){
scanf("%d", &moon[i]);
}
for(i = 0; i < n ; i++){
int tmp;
scanf("%d", &tmp);
price[i] = tmp*1.0/moon[i];
}
mysort(0,n-1);
float sum = 0;
for(i = 0; i < n && m > 0; i++){
if(moon[i] <= m){
sum += price[i]*moon[i];
m -= moon[i];
}else if(moon[i] > m){
sum += price[i] * m;
m = 0;
}
}
printf("%.2f", sum);
return 0;
}
冒泡排序
#include <stdio.h>
#include <stdlib.h>
static double moon[1001];
static double price[1001];
void swap(int i, int j){
double tmp, tmp1;
tmp = price[j];
tmp1 = moon[j];
price[j] = price[i];
price[i] = tmp;
moon[j] = moon[i];
moon[i] = tmp1;
}
void mysort(int n){
int i, j;
for(i = 0; i < n-1; i++){
for(j = 0; j < n-i-1; j++){
if(price[j] < price[j+1]){
swap(j, j+1);
}
}
}
}
int main()
{
double n;
double m;
scanf("%lf %lf\n", &n, &m);
int i;
for(i = 0; i < n; i++){
scanf("%lf", &moon[i]);
}
for(i = 0; i < n ; i++){
double tmp;
scanf("%lf", &tmp);
price[i] = tmp/moon[i];
}
mysort(n);
double sum = 0;
for(i = 0; i < n && m > 0; i++){
if(moon[i] <= m){
sum += price[i]*moon[i];
m -= moon[i];
}else if(moon[i] > m){
sum += price[i] * m;
m = 0;
}
}
printf("%.2f", sum);
return 0;
}
问题
- 所有的数据类型都要用 double统一,否则测试点3会出错
- 为什么 快速排序超时,而冒泡排序不超时。。递归的原因?