1.5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
#include <stdio.h>
int check(int* player,int n)
{
int i, res = 0;
for (i = 0; i < n; i++)
res |= 1 << player[i];
return res == 0x3e;
}
int main(void)
{
int player[5] = { 0 };
for (player[0] = 1; player[0] <= 5; player[0]++)
{
for (player[1] = 1; player[1] <= 5; player[1]++)
{
for (player[2] = 1; player[2] <= 5; player[2]++)
{
for (player[3] = 1; player[3] <= 5; player[3]++)
{
for (player[4] = 1; player[4] <= 5; player[4]++)
{
if ((player[1] == 2) + (player[0] == 3) == 1 &&
(player[1] == 2) + (player[4] == 4) == 1 &&
(player[2] == 1) + (player[3] == 2) == 1 &&
(player[2] == 5) + (player[3] == 3) == 1 &&
(player[4] == 4) + (player[0] == 1) == 1 &&
check(player, 5))
{
printf("a=%d,b=%d,c=%d,d=%d,e=%d\n", player[0], player[1], player[2], player[3], player[4]);
}
}
}
}
}
}
system("pause");
return 0;
}
2.日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。现在请根据这些信息,写一个程序来确定到底谁是凶手。
#include <stdio.h>
int main(void)
{
char n;
for (n = 'A'; n <= 'D'; n++)
{
if ((n != 'A') + (n == 'C') + (n == 'D') + (n != 'D') == 3)
printf("%c是凶手。\n", n);
}
system("pause");
return 0;
}
3.在屏幕上打印杨辉三角。
1
1 1
1 2 1
1 3 3 1
#include <stdio.h>
int main(void)
{
int num[20][20] = { 0 };
int n = 10;
int i, j;
printf("%d\n", num[0][0] = 1);
for (i = 1; i < n; i++)
{
printf("%d\t", num[i][0] = 1);
for (j = 1; j < i; j++)
{
num[i][j] = num[i - 1][j] + num[i - 1][j - 1];
printf("%d\t", num[i][j]);
}
printf("%d\n", num[i][j] = 1);
}
system("pause");
return 0;
}