为什么会出现“ The winner is a: 327 + 1The winner is b: 129 + 2”的结果?
输入样例:
327 129
1 0 1
输出样例:
The winner is a: 327 + 1
```c
#include
int main()
{
int Pa=0,Pb=0,a=0,b=0,c=0,P1=0,P2=0;
scanf("%d %d",&Pa,&Pb);
scanf("%d %d %d",&a,&b,&c);
if(Pa>Pb&&a==0||b==0||c==0)
{P1=Pa;P2=3-(a+b+c);
printf("The winner is a: %d + %d",P1,P2);}
if(Paa==0&&b==0&&c==0)
{P1=Pa;P2=3;
printf("The winner is a: %d + %d",P1,P2);}
if(Paa==1||b==1||c==1)
{P1=Pb;P2=a+b+c;
printf("The winner is b: %d + %d",P1,P2);}
if(Pa>Pb&&a==1&&b==1&&c==1)
{P1=Pb;P2=3;
printf("The winner is b: %d + %d",P1,P2);}
return 0;
}
```
用了多个 if 条件语句,当输入的条件不满足第一个 if 语句的情况时,就不会执行后面的 if 语句,因此可能输出不符合预期。例如,输入 327 129 和 1 0 1 时,应该输出“The winner is b: 129 + 2”,但实际输出结果为“The winner is a: 327 + 1The winner is b: 129 + 2”。
改了一下,你看一下行不行
#include<stdio.h>
int main()
{
int Pa=0,Pb=0,a=0,b=0,c=0,P1=0,P2=0;
scanf("%d %d",&Pa,&Pb);
scanf("%d %d %d",&a,&b,&c);
if(Pa>Pb){
if(a==0||b==0||c==0){
P1=Pa;P2=3-(a+b+c);
printf("The winner is a: %d + %d",P1,P2);
}
else if(a==1&&b==1&&c==1){
P1=Pb;P2=3;
printf("The winner is b: %d + %d",P1,P2);
}
}
else{
if(a==0&&b==0&&c==0){
P1=Pa;P2=3;
printf("The winner is a: %d + %d",P1,P2);
}
else if(a==1||b==1||c==1){
P1=Pb;P2=a+b+c;
printf("The winner is b: %d + %d",P1,P2);
}
}
return 0;
}
那你觉得应该是什么结果?
谢谢各位解答,我忘了优先级,在或的判断那里加上括号即可。