算法复习之贪心算法之poj 1323

题意:一次card比赛,有m个参赛者(包括你),每个参赛者有n张卡片。每张卡片的编号都不一样。每一轮,所有参赛者都打出一张卡片,编号最大的赢。问你至少能赢几局。
 
思路:简单贪心

 

代码如下:

#include<iostream>
using namespace std;
const int mMax = 22;
const int nMax = 52;

struct data
{
    int num;
    bool my;
    bool small;
}card[mMax * nMax];

int main()
{
    int m , n, i, j, t = 1;
    while(cin >> m >> n && m != 0)
	{
        for(i = 1; i <= m * n; i ++)
		{
            card[i].num = i;
            card[i].my = false;
            card[i].small = false;
        }//初始化,给每个card
        for(i = 1; i <= n; i ++)
		{
            int number;
            cin >> number;
            card[number].my = true;
        }//初始化自己的卡片
        getchar();
        getchar();//按照题目要求
		
        int ans = n;
        for(i = m * n; i >= 2 && ans > 0; i --)
            if(!card[i].my)
                for(j = i - 1; j >= 1; j --)
                    if(card[j].my && !card[j].small)
					{
                        card[j].small = true;
                        ans--;
                        break;
                    }
					cout << "Case " << t++ << ": " << ans << endl;
    }
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值