杭电oj--1070(贪心算法)

1. Ignatius will never drink the milk which is produced 6 days ago or earlier. That means if the milk is produced 2005-1-1, Ignatius will never drink this bottle after 2005-1-6(inclusive).
2. Ignatius drinks 200mL milk everyday.
3. If the milk left in the bottle is less than 200mL, Ignatius will throw it away.
4. All the milk in the supermarket is just produced today.

要点分析:1.牛奶是一次性买的

                  2.低于200ml的牛奶将被舍弃,一天都不够喝(程序中可设置其性价比为999999999)

                  3.每200ml喝一天,最多喝5天,也就是做多喝1000ml,超过5天的牛奶将会被舍弃

                  4.如果性价比相同,选容量大的品牌

#include<iostream>
#include<string>
using namespace std;

typedef struct milk{
	char brand[120];
	int price;
	int v;
	double cost;
}MILK;

void compare(MILK *m,int len){//比较函数
	MILK tmp,max;
	int i,j;
	for(i=0;i<len;i++){//计算性价比
		if(m[i].v<200){//低于200ml,设置性价比为99999999999
			m[i].cost=99999999999;
			continue;
		}
		if(m[i].v>1000)//超过1000ml,就用价格 除以5天
			m[i].cost=m[i].price/double(5);
		else 反之,就用价格 除以可以喝多少天的天数
			m[i].cost=(double)m[i].price/(m[i].v/200);
	}
	for(i=1;i<=len;i++)//按照性价比升序排列
		for(j=0;j<len-i;j++)
			if(m[j].cost>m[j+1].cost){
				tmp=m[j];
				m[j]=m[j+1];
				m[j+1]=tmp;
	}
	max=m[0];
	for(i=1;i<len;i++)
		if(max.cost==m[i].cost)
			if(max.v<=m[i].v)
				max=m[i];//若性价比相同找容量最高的
	cout<<max.brand<<endl;

}

int main(){
int n,kinds,i,max,t;
MILK m[120];
while(cin>>n){
	while(n--){
		cin>>kinds; 
			for(i=0;i<kinds;i++)
				cin>>m[i].brand>>m[i].price>>m[i].v;
			compare(m,kinds);//比较	
	}
	break;
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41048982/article/details/84328023
今日推荐