uva 565 - Pizza Anyone?

点击打开链接

题目意思:   有16种甜品,现在要做一个批萨,有很多人,每个人喜欢的甜品各不相同,要求我们找到一个方案使得至少让每个人都能够满足一个要求,最后输出这个方案。

解题思路:   我么知道对于每一个toppings只有两种情况取和不取,那么最多有16种,2^16是很小的,那么我们便可以枚举每一个情况,然后去判断是否每个人都能够满足,加入能够满足直接输出即可。注意这一题的输出是多种情况,不一定和样列相同。时间可能会很慢,还有注意数组的大小

代码:

 

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <list>
using namespace std;

const int MAXN = 100;
char G[MAXN][40]; //存储输入的字符串(注意数组的大小)
int P[16]; //存储每一个状态
int m, ans, len, flag;

//处理问题的函数
void solve(int cnt) {
    int i, j;
    int pos = 15;
    memset(P, -1, sizeof (P)); //初始化为-1
    while (pos >= 0) {
        P[pos] = cnt & 1;
        pos--;
        cnt >>= 1;
    }
    //搜索只要每个人都满足即可判断这种情况就是满足的解
    for (i = 0; i < len; i++) {
        flag = 0;
        for (j = 0; G[i][j] != ';'; j++) {
            if (G[i][j] == '+') {
                j++;
                int t = G[i][j] - 65;
                if (P[t])
                    flag = 1;
            }
            if (G[i][j] == '-') {
                j++;
                int t = G[i][j] - 65;
                if (P[t] == 0)
                    flag = 1;
            }
        }
        if (flag == 0)//如果有一个不满足直接退出
            return;
    }
    if (i == len)
        ans = 1;
}

//输出函数
void output() {
    if (ans) {
        printf("Toppings: ");
        for (int i = 0; i < 16; i++) {
            if (P[i])
                printf("%c", i + 65);
        }
        printf("\n");
    } else
        printf("No pizza can satisfy these requests.\n");
}

//主函数
int main() {
    int i;
    while (gets(G[0]) && strcmp(G[0], ".")) {
        i = 1;
        ans = 0;
        while (gets(G[i])) {
            if (strcmp(G[i], ".") == 0)
                break;
            i++;
        }
        len = i;
        m = 65535;//2的16次方减1
        while (m) {//枚举每一种情况
            solve(m);
            if (ans)//找到以后退出不用继续搜索
                break;
            m--;
        }
        output();
    }
    return 0;
}
时间: 2024-10-26 16:10:30

uva 565 - Pizza Anyone?的相关文章

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 h

UVa 10079 Pizza Cutting (water ver.)

10079 - Pizza Cutting Time limit: 8.333 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1020 When someone calls Ivan lazy, he claims that it is his intelligence that helps

UVa 624 CD (0-1背包)

624 - CD Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=565 You have a long drive by car ahead. You have a tape recorder, but unfortunately your best mus

UVa 755 / POJ 1002 487--3279 (排序)

755 - 487--3279 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=696 http://poj.org/problem?id=1002 Businesses like to have memorable telephone numbers. On

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.