poj 1010

复制代码
/*
Sample Input

1 2 3 0    ; three different stamp types
7 4 0        ; two customers
1 1 0        ; a new set of stamps (two of the same type)
6 2 3 0    ; three customers
Sample Output

7 (3): 1 1 2 3 
4 (2): 1 3 
6 ---- none
2 (2): 1 1
3 (2): tie
*/
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int data[100],cnt,customer;
bool none,tie;
int now[5],now_type,now_num,now_max,end[5],end_type,end_num,end_max;
int get_type(int a[],int n){
    sort(a,a+n);
    int i,type = 0;
    if(n>0)type = 1; 
    for(i=1;i<n;i++){
        if(a[i]!=a[i-1]){
            type++;
        }
    }
    return type;
}
int get_max(int a[],int n){
    int i,m_a = -9999;
    for(i=0;i<n;i++){
        if(m_a<data[a[i]]){
            m_a = data[a[i]];
        }
    }
    return m_a;
}
void print(){
    int i;
    printf("%d ",customer);
    if(none){
        printf("---- none\n");
    }
    else{
        printf("(%d): ",get_type(end,end_num));
        if(tie){
            printf("tie\n");
        }
        else{
            sort(end,end+end_num);
            for(i=0;i<end_num-1;i++){
                printf("%d ",data[end[i]]);
            }
            printf("%d\n",data[end[i]]);
        }
    }
}
void compare(){
    int i;
    now_type = get_type(now,now_num);
    now_max = get_max(now,now_num);
    end_type = get_type(end,end_num);
    end_max = get_max(end,end_num);
    if((end_num==0)||(end_type<now_type)||(end_type==now_type&&end_num>now_num)||(end_type==now_type&&end_num==now_num&&end_max<now_max)){
        tie = false;
        end_num = now_num;
        for(i=0;i<now_num;i++){
            end[i] = now[i];
        }
    }
    else if(end_type==now_type&&end_num==now_num&&end_max==now_max){
         tie = true;
    }
}
int dfs(int n,int value){
    int i;
    if(value>customer)return 0;
    if(value==customer){
        none = false;
        compare();
    }
    if(now_num==4)return 0;
    for(i=n;i<cnt;i++){
        now[now_num] = i;
        now_num++;
        dfs(i,value+data[i]);
        now_num--;
    }
}
char suffix[100];
int main(){
    int i;
    while(true){
        cnt = 0;
        memset(now,0,sizeof(now));
        memset(end,0,sizeof(end));
        while(true){
            if(scanf("%d",&data[cnt])==EOF)return 1;
            if(data[cnt]==0)break;
            cnt++;
        }
        sort(data,data+cnt);
        gets(suffix);
        while(true){
            if(scanf("%d",&customer)==1&&customer==0){
                break;
            }
            now_num = 0;
            end_num = 0;
            none = true;
            tie = false;
            dfs(0,0);
            print();
        }
        gets(suffix);
    }
    return 0;
} 
复制代码

 

posted @   智人心  阅读(22)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示