5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
#include <stdio.h>
#include <windows.h>
//写一个判断名次有无重复的情况
int Is_Repeat(int player[5]) {
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 5; ++j) {
if (player[i] == player[j]) {
return 1;
}
}
}
return 0;
}
//根据逻辑判断5为选手的名次,可采用for循环的嵌套
void Sort() {
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[0] == 3) + (player[1] == 2) == 1
&& (player[1] == 2) + (player[4] == 4) == 1
&& (player[2] == 1) + (player[3] == 2) == 1
&& (player[3] == 3) + (player[2] == 5) == 1
&& (player[4] == 4) + (player[0] == 1) == 1) {
if (Is_Repeat(player) == 0) {
printf("A:%d\nB:%d\nC:%d\nD:%d\nE:%d\n", player[0]
, player[1], player[2], player[3], player[4]);
}
}
}
}
}
}
}
}
int main() {
Sort();
system("pause");
return 0;
}
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个
嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手。
#include <stdio.h>
#include <windows.h>
/*
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个
嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手。
*/
void Murderer() {
for (char muder = 'A'; muder <= 'D'; ++muder) {
if ((muder != 'A') + (muder == 'C') + (muder == 'D') + (muder != 'C') == 3) {
printf("凶手是%c\n", muder);
}
}
}
int main() {
Murderer();
system("pause");
return 0;
}
在屏幕上打印杨辉三角。
1
1 1
1 2 1
1 3 3 1
#include <stdio.h>
#include <windows.h>
/*
在屏幕上打印杨辉三角。
1
1 1
1 2 1
1 3 3 1
*/
//可以随时改变杨辉三角的规格
#define SIZE 4
//写一个输出杨辉三角的函数
void Print_Yang() {
int triangle[SIZE][SIZE] = { 0 };
//将杨辉三角的两边置1
for (int i = 0; i < SIZE; ++i) {
triangle[i][0] = 1;
triangle[i][i] = 1;
}
//根据杨辉三角内部数字与上一行数字之间的关系赋值
for (int i = 2; i < SIZE; ++i) {
for (int j = 1; j < i; ++j) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
//打印杨辉三角
for (int row = 0; row < SIZE; ++row) {
for (int col = 0; col <= row; ++col) {
printf("%d ", triangle[row][col]);
}
printf("\n");
}
}
int main() {
Print_Yang();
system("pause");
return 0;
}