P118 月饼
贪心
主要是记录下结构体中元素的排序
#include <stdio.h>
#include <algorithm>
using namespace std;
struct mooncake{
double store;
double sell;
double price;
}cake[1010];
//结构体的排序
bool cmp(mooncake a,mooncake b){
return a.price >b.price;
}
int main(){
int n;
double D;
scanf("%d%lf",&n,&D);
for(int i = 0;i <n;i++){
scanf("%lf",&cake[i].store);
}
for(int i = 0;i <n;i++){
scanf("%lf",&cake[i].sell);
cake[i].price = cake[i].sell/cake[i].store;
}
sort(cake,cake+n,cmp);
double ans = 0;
for(int i = 0;i <n;i++){
if(cake[i].store <= D){
D = D - cake[i].store;
ans = ans + cake[i].sell;
}
else{
ans = ans + cake[i].price*D;
break;
}
}
printf("%.2f\n",ans);
return 0;
}
P121 组个最小数
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int count[10];
for(int i = 0;i <10;i++){
scanf("%d",&count[i]);
}
for(int i = 1;i <10;i++){
if(count[i] >0){
//count[i]--表明这一项的count[i]减少1
printf("%d",i);
count[i]--;
break;
}
}
for(int i = 0;i <10;i++){
for(int j = 0;j <count[i];j++){
printf("%d",i);
}
}
return 0;
}
来源:CSDN
作者:晴空_万里
链接:https://blog.csdn.net/weixin_42377217/article/details/104009605