C語言跳水比賽預測結果
阿新 • • 發佈:2018-10-26
轉換 turn 精簡 print system tdi ++ col pre
#include<stdlib.h>
int main(){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
for (a = 1; a <= 5; a++)
{
for (b = 1; b <= 5; b++)
{
for (c = 1; c <= 5; c++)
{
for (d = 1; d <= 5; d++)
{
for (e = 1; e <= 5; e++)
{
if ((b == 2 && a != 3) || (b != 2 && a == 3) == 1)
{
if ((b == 2 && e != 4) || (b != 2 && e == 4) == 1)
{
if ((c == 1 && d != 2) || (c != 1 && d == 2) == 1)
{
if ((c == 5 && d != 3) || (c != 5 && d == 3) == 1)
{
if ((e == 4 && a != 1) || (e != 4 && a == 1) == 1)
{
if (a*b*c*d*e==120)
{
printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", a, b, c, d, e);
}
}
}
}
}
}
}
}
}
}
}
system("pause");
return 0;
}
5位運動員參加了10米臺跳水比賽,有人讓他們預測比賽結果
A選手說:B第二,我第三;
B選手說:我第二,E第四;
C選手說:我第一,D第二;
D選手說:C最後,我第三;
E選手說:我第四,A第一;
比賽結束後,每位選手都說對了一半,請編程確定比賽的名次
1.首先將選手的話轉換成編程語言;
2.將編程語言用邏輯表達出來.
1
#include<stdio.h>#include<stdlib.h>
int main(){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
for (a = 1; a <= 5; a++)
{
for (b = 1; b <= 5; b++)
{
for (c = 1; c <= 5; c++)
{
for (d = 1; d <= 5; d++)
{
for (e = 1; e <= 5; e++)
{
if ((b == 2 && a != 3) || (b != 2 && a == 3) == 1)
{
if ((b == 2 && e != 4) || (b != 2 && e == 4) == 1)
{
if ((c == 1 && d != 2) || (c != 1 && d == 2) == 1)
{
if ((c == 5 && d != 3) || (c != 5 && d == 3) == 1)
{
if ((e == 4 && a != 1) || (e != 4 && a == 1) == 1)
{
if (a*b*c*d*e==120)
{
printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", a, b, c, d, e);
}
}
}
}
}
}
}
}
}
}
system("pause");
return 0;
}
這個表達式雖然能顯示出結果,但語句有點冗雜,所以,我再精簡了這個代碼.
C語言跳水比賽預測結果