1.

5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int a, b, c, d, e, i;
    int num = 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) == 1
                            && (b == 2) + (e == 4) == 1
                            && (c == 1) + (d == 2) == 1
                            && (c == 5) + (d == 3) == 1
                            && (e == 4) + (a == 1) == 1) {
                            num = 0;
                            num |= (1 << (a - 1));
                            num |= (1 << (b - 1));
                            num |= (1 << (c - 1));
                            num |= (1 << (d - 1));
                            num |= (1 << (e - 1));
                            for (i = 0; i < 5; i++) {
                                if (num == 0)
                                    break;
                                num &= (num - 1);
                            }
                            if (i == 5) {
                                printf("a = %d\nb = %d\nc = %d\nd = %d\ne = %d\n\n", a, b, c, d, e);
                            }
                        }
                    }
                }
            }
        }
    }

    system("pause");
    return 0;
}

2.

日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个
嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int a, b, c, d;

    for (a = 0; a <= 1; a++) {
        for (b = 0; b <= 1; b++) {
            for (c = 0; c <= 1; c++) {
                for (d = 0; d <= 1; d++) {
                    if ((a != 1) + (c == 1) + (d == 1) + (d != 1) == 3) {
                        if(a + b + c + d == 1)
                            printf("a = %d\nb = %d\nc = %d\nd = %d\n\n", a, b, c, d);
                    }
                }
            }
        }
    }

    system("pause");
    return 0;
}

3. 打印杨辉三角

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void yanghui_triangle(int n) {
    int** arr = (int **)malloc(sizeof(int *) * n);

    for (int i = 0; i < n; i++) {
        arr[i] = (int *)malloc(sizeof(int) * n);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = 0;
        }
    }

    arr[0][0] = 1;
    for (int row = 1; row < n; row++) {
        for (int col = 0; col <= row; col++) {
            arr[row][col] = ((col - 1 >= 0) ? (arr[row - 1][col - 1]) : 0)
                + ((col < row) ? (arr[row - 1][col]) : 0);
        }
    }

    for (int row = 0; row < n; row++) {
        for (int i = 0; i < n - row - 1; i++) {
            printf(" ");
        }
        for (int col = 0; col <= row; col++) {
            printf("%d ", arr[row][col]);
        }
        printf("\n");
    }
}

int main() {
    yanghui_triangle(5);

    system("pause");
    return 0;
}
Last modification:November 10th, 2019 at 10:22 am