代碼
|
口味
|
A
|
鳳尾魚
|
B
|
黑橄欖
|
C
|
加拿大燻肉
|
D
|
方丁大蒜
|
E
|
濃奶酪
|
F
|
鮮花椰菜
|
G
|
青辣椒
|
H
|
火腿
|
I
|
意大利香腸
|
J
|
加拉佩諾胡椒
|
K
|
波蘭薰腸
|
L
|
瘦牛肉
|
M
|
蘑菇
|
N
|
脫脂羊乳酪
|
O
|
洋蔥
|
P
|
胡椒
|
Simple input 1
|
Output for the input
|
+A+B+C+D-E-F-G-H;
-A-B+C+D-E-F+G+H;
-A+B-C+D-E+F-G+H;
.
|
Toppings:
|
Simple input 2
|
Output for the input
|
+A+B+C+D;
+E+F+F+H;
+A+B-G;
+O+J-F;
+H+I+C;
+P;
+O+M+L;
+M-L+P;
.
|
Toppings: CELP
|
Simple input 3
|
Output for the input
|
+A;
-A;
.
|
No pizza can satisfy these requests.
|
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<io.h>
#define TYPELEN 16 //比薩口味的種類數
#define PERSONMAX 30 //最多的朋友人數
int pizza,want[PERSONMAX],hate[PERSONMAX],person;
char buf[TYPELEN*2];
void
setup(){
int count,i;
count=strlen(buf)-1; //最後一個是分號,因捨去。
memset(want+person,0,TYPELEN);
memset(hate+person,0,TYPELEN);
for(i=1;i<count;i+=2)
if(buf[i-1]=='+')
want[person]=want[person]|(1<<(buf[i]-65));
else
hate[person]=hate[person]|(1<<(buf[i]-65));
++person;
}
bool
checkPizza(){
bool flag;
int i;
for(pizza=0;pizza<65536;pizza++){ //搜索所有的可能pizza取值,直到成功
flag=true;
for(i=0;i<person;i++)
if(!((pizza&want[i])>0 || (!pizza&hate[i])>0)){
flag=false;
break;
}
if(flag) return true;
}
return false;
}
void
output(){
int i;
printf("Toppings: ");
for(i=0;i<TYPELEN;i++)
if(pizza&(1<<i)) putchar(i+65);
puts("/0");
}
void
main(){
person=0;
while(1){
scanf("%s",buf);
if(buf[0]=='.') break;
setup();
}
if(checkPizza()==true) output();
else puts("No pizza can satisfy these requests.");
}