poj1010 枚举

/*枚举出所有情况,在枚举的过程中,选出最优解。
大致题意:1、满足客户要求的即为可行解
	2、种类最多即为最优解
	3、在种类相同的情况下,张数少的更优
	4、在张数相等的情况下,单个价值更大的更优
	5、三者都相同,输出tie
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int stamp[1006];
int s[4];
int n,m,k,t,maxx;
int curK,curT,curM,curS[4];
bool curTie;
int temp;
bool ok(int temp)
{
    int i,j;
    for(i=0,j=0;i<n;i++)
        if(stamp[i]==temp)  j++;
    return (j<5);
}
 void Dodo(){
    for ( s[0]=0; s[0]<=n; s[0]++)
        for ( s[1]=s[0]; s[1]<=n; s[1]++)
            for ( s[2]=s[1]; s[2]<=n; s[2]++)
                for ( s[3] = s[2]; s[3]<=n; s[3]++) {
                    if ( stamp[s[0]]+stamp[s[1]]+stamp[s[2]]+stamp[s[3]] != m ) continue;
                    //cout<<stamp[s[0]]<<stamp[s[1]]<<stamp[s[2]]<<stamp[s[3]]<<endl;
                     k = 0;
                     t = 0;
                    maxx = 0;
                    if ( stamp[s[0]] ) {k++; t++; if ( stamp[s[0]]>maxx ) maxx = stamp[s[0]];}
                    if ( stamp[s[1]] ) {k++; if ( s[1]>s[0] ) t++; if ( stamp[s[1]]>maxx ) maxx = stamp[s[1]];}
                    if ( stamp[s[2]] ) {k++; if ( s[2]>s[1] ) t++; if ( stamp[s[2]]>maxx ) maxx = stamp[s[2]];}
                    if ( stamp[s[3]] ) {k++; if ( s[3]>s[2] ) t++; if ( stamp[s[3]]>maxx ) maxx = stamp[s[3]];}
                    if ( t<curT ) continue;
                    if ( t>curT ) {
                        curT = t;
                        curK = k;
                        curM = maxx;
                        curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
                        curTie = false;
                        continue;
                    }
                    if ( k>curK ) continue;
                    if ( k<curK ) {
                        curT = t;
                        curK = k;
                        curM = maxx;
                        curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
                        curTie = false;
                        continue;
                    }
                    if ( maxx<curM ) continue;
                    if ( maxx>curM ){
                        curT = t;
                        curK = k;
                        curM = maxx;
                        curS[0]=s[0]; curS[1]=s[1];curS[2]=s[2];curS[3]=s[3];
                        curTie = false;
                        continue;
                    }
                    curTie = true;
                }
    if ( curT == -1 ) {
        cout<<m<<" ---- none"<<endl;
        return;
    }
    cout<<m<<" ("<<curT<<"):";
    if ( curTie ) {
        cout<<" tie"<<endl;
        return;
    }
    for ( int i=0; i<4; i++)
        if ( stamp[curS[i]]>0 ) cout<<' '<<stamp[curS[i]];
    cout<<endl;
}
int main()
{
    int strs[255],k;
    int i;
    while(scanf("%d",&k)!=EOF)
    {
        i=0;
       while(k!=0)
       {
           strs[i++]=k;
           scanf("%d",&k);
       }
       strs[i]=0;
        n=0;
        memset(stamp,0,sizeof(stamp));
        for(i=0;strs[i]!=0;i++)
        {
            if(ok(strs[i]))
                stamp[n++]=strs[i];
        }
        i=0;
        while(1)
        {
            scanf("%d",&strs[i++]);
            if(strs[i-1]==0)
                break;
        }
        for(i=0;strs[i]!=0;i++)
        {
            m=strs[i];
            curK=100;
            curT=-1;
            curM=-1;
            curTie=false;
            Dodo();
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值