ZOJ 1558 Euro Efficiency

此题和背包问题,不过注意数组开大一点,因为针对1,2,5,30,50,70。在求90时,90=70+50-30只用了3枚,是最少的,但50+70=120超出了100.

 

 

#include<iostream> #include<memory.h> #include<queue> #include<stdlib.h> #include<stdio.h> #include<math.h> using namespace std; #define inf 0x7fffff int N, i, mfront; int coins[6], sum[205]; //硬币单位,及1-200内所需最少硬币数 queue<int> value_qu; //队列,先进先出,存储已计算出硬币数目的金钱值 int rlt_max; //最大值 float rlt_average; //平均值 void init(){ for(i=1;i<=200;i++){ sum[i]=inf; } while(!value_qu.empty()) value_qu.pop(); for(i = 0; i < 6; i++){ cin >> coins[i]; value_qu.push(coins[i]); sum[coins[i]] = 1; } rlt_max = 0; rlt_average = 0; }void work(){ int temp; while(!value_qu.empty()){ //当队列为空时,结束 mfront = value_qu.front(); //取出队列头,将其与硬币单位相加减,将符合条件的数存入队列尾 for(i = 0; i < 6; i++){ temp = mfront+coins[i]; if( temp<=200&∑[temp]> sum[mfront]+1){ sum[temp] = sum[mfront]+1; //所用硬币数是队列元素的硬币数+1 value_qu.push(temp); } temp =mfront-coins[i];//不明白为何要取绝对值,反正不取就WA if(temp>0&∑[temp] >sum[mfront]+1){ sum[temp] = sum[mfront]+1; value_qu.push(temp); } } value_qu.pop(); } } int main(){ cin >> N; while(N--){ init(); //初始化,接收输入 work(); //进行计算 for(i = 1; i <= 100; i++){ //找出最大数和平均数 if(rlt_max < sum[i]) rlt_max = sum[i]; rlt_average += sum[i]; } rlt_average = rlt_average/100; printf("%.2f %d/n", rlt_average, rlt_max); } return 0; }

你可能感兴趣的:(存储,float)