UVa 565:Pizza Anyone?

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=109&page=show_problem&problem=506

类型: 暴力枚举,搜索

题目:

You are responsible for ordering a large pizza for you and your friends. Each of them has told you what he wants on a pizza and what he does not; of course they all understand that since there is only going to be one pizza, no one is likely to have all their requirements satisfied. Can you order a pizza that will satisfy at least one request from all your friends?

The pizza parlor you are calling offers the following pizza toppings; you can include or omit any of them in a pizza:

Your friends provide you with a line of text that describes their pizza preferences. For example, the line

+O-H+P;

reveals that someone will accept a pizza with onion, or without ham, or with pepperoni, and the line

-E-I-D+A+J;

indicates that someone else will accept a pizza that omits extra cheese, or Italian sausage, or diced garlic, or that includes anchovies or jalapenos.

题目大意:

你负责去订购一个大比萨, 但是你的朋友们对于要添加什么或者不添加什么都有不同的要求。 但是你的朋友们也知道不可能满足全部的要求。所以你要选择一个订购方案,让所有人至少满足其中一个要求。 注意,如果某人不想要哪个,那么不添加那个也算是满足了他的一个要求。

分析与总结:

题目只有16种东西选择, 对于每种东西之是选择或者不选泽两种状态。那么用暴力枚举法完全可以做出。

用一个数字,它的各个二进制位表示定或者不定。枚举0 ~ (1<<16)-1即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[100][100];
int nIndex;  

int main(){
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    while(gets(str[0])){
        nIndex = 1;
        while(gets(str[nIndex]), str[nIndex++][0]!='.') ;;  

        int status=0;
        bool flag=true;
        int maxNum = (1<<16)-1;
        while(status <= maxNum){  

            flag = true;
            for(int i=0; i<nIndex-1; ++i){
                bool ok = false;
                int pos=0;
                while(pos < strlen(str[i])){
                    if(str[i][pos]=='+'){
                        if((status >> (str[i][pos+1]-'A')) & 1 ){
                            ok = true; break;
                        }
                    }
                    else if(str[i][pos]=='-'){
                        if( !((status >> (str[i][pos+1]-'A')) & 1)){
                            ok = true;
                            break;
                        }
                    }
                    pos += 2;
                }
                if(!ok){flag=false ; break; }
            }
            if(flag) break;
            ++status;
        }
        if(!flag) printf("No pizza can satisfy these requests.\n") ;
        else{
            int pos=0;
            printf("Toppings: ");
            while(pos <16){
                if(status & 1) printf("%c", pos+'A');
                ++pos; status >>= 1;
            }
            printf("\n");
        }
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索题目
, 选择
, 一个
, You
, 或者
that
anyone of us、anyone、we don t talk anyone、anyone of us 下载、anyone else,以便于您获取更多的相关知识。

时间: 2025-01-01 15:56:29

UVa 565:Pizza Anyone?的相关文章

uva 565 - Pizza Anyone?

点击打开链接 题目意思:   有16种甜品,现在要做一个批萨,有很多人,每个人喜欢的甜品各不相同,要求我们找到一个方案使得至少让每个人都能够满足一个要求,最后输出这个方案. 解题思路:   我么知道对于每一个toppings只有两种情况取和不取,那么最多有16种,2^16是很小的,那么我们便可以枚举每一个情况,然后去判断是否每个人都能够满足,加入能够满足直接输出即可.注意这一题的输出是多种情况,不一定和样列相同.时间可能会很慢,还有注意数组的大小 代码:   #include <algorith

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the