誰家孩子跑得最慢

1.問題描述

假設張王李三家,每家都有3個孩子。某一天,這三家的9個孩子一起比賽短跑,規定不考慮年齡大小,第1名得9分,第2名得8分,第3名得7分,依次類推。比賽結束後統計分數發現三家孩子得總分是相同的,同時限定這9個孩子的名次不存在並列的情況,且同一家的孩子不會獲得相連的分數。現已知獲得第1名的是李家的孩子,獲得第2名的是王家的孩子,要求編程求出獲得最後一名的是哪家的孩子。

2.源碼實現

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

/*是否滿足題目要求*/
short isMatch(int **tab, int a, int b, int c)
{
    if(tab[b][0] != 8 || tab[c][0] != 9)
    {
        return 0;
    }

    if(tab[a][0] == tab[b][0] || tab[a][0] == tab[b][1] || tab[a][0] == tab[b][2])
    {
        return 0;
    }

    if(tab[a][1] == tab[b][0] || tab[a][1] == tab[b][1] || tab[a][1] == tab[b][2])
    {
        return 0;
    }

    if(tab[a][2] == tab[b][0] || tab[a][2] == tab[b][1] || tab[a][2] == tab[b][2])
    {
        return 0;
    }

    if(tab[a][0] == tab[c][0] || tab[a][0] == tab[c][1] || tab[a][0] == tab[c][2])
    {
        return 0;
    }

    if(tab[a][1] == tab[c][0] || tab[a][1] == tab[c][1] || tab[a][1] == tab[c][2])
    {
        return 0;
    }

    if(tab[a][2] == tab[c][0] || tab[a][2] == tab[c][1] || tab[a][2] == tab[c][2])
    {
        return 0;
    }

    if(tab[b][0] == tab[c][0] || tab[b][0] == tab[c][1] || tab[b][0] == tab[c][2])
    {
        return 0;
    }

    if(tab[b][1] == tab[c][0] || tab[b][1] == tab[c][1] || tab[b][1] == tab[c][2])
    {
        return 0;
    }

    if(tab[b][2] == tab[c][0] || tab[b][2] == tab[c][1] || tab[b][2] == tab[c][2])
    {
        return 0;
    }

    if(tab[a][0] + tab[a][1] + tab[a][2] != tab[b][0] + tab[b][1] + tab[b][2])
    {
        return 0;
    }

    if(tab[a][0] + tab[a][1] + tab[a][2] != tab[c][0] + tab[c][1] + tab[c][2])
    {
        return 0;
    }

    if(tab[b][0] + tab[b][1] + tab[b][2] != tab[c][0] + tab[c][1] + tab[c][2])
    {
        return 0;
    }

    return 1;
}

int main()
{
    int a[3] = {0, 0, 0};   /*張家孩子*/
    int b[3] = {0, 0, 0};   /*王家孩子*/
    int c[3] = {0, 0, 0};   /*李家孩子*/
    int d[3] = {0, 0, 0};
    int e[100][3];
    int *u[100];
    int n = 0;
    int i;
    int j = 0;

    memset(e, 0x00, sizeof(e));

    /*只考慮其中一家孩子的可能性*/
    for(i=0; i<1000; i++)
    {
        d[0] = i % 10;
        d[1] = (i / 10) % 10;
        d[2] = i / 100;

        if(d[0] != 0 && d[1] != 0 && d[2] != 0 && d[0] - d[1] > 1 && d[1] - d[2] > 1)
        {
            e[j][0] = d[0];
            e[j][1] = d[1];
            e[j][2] = d[2];
            j++;
        }
    }

    n = j;

    for(i=0; i<n; i++)
    {
        u[i] = e[i];
    }

    j = 0;

    /*考慮其中三家孩子的可能性*/
    for(i=0; i<n*n*n; i++)
    {
        e[n][0] = i % n;
        e[n][1] = (i / n) % n;
        e[n][2] = i / (n*n);

        if(isMatch(u, e[n][0], e[n][1], e[n][2]))
        {
            a[0] = u[e[n][0]][0];
            a[1] = u[e[n][0]][1];
            a[2] = u[e[n][0]][2];
            b[0] = u[e[n][1]][0];
            b[1] = u[e[n][1]][1];
            b[2] = u[e[n][1]][2];
            c[0] = u[e[n][2]][0];
            c[1] = u[e[n][2]][1];
            c[2] = u[e[n][2]][2];

            j++;
        }
    }

    printf("probability = %d\n", j);

    printf("a = {%d, %d, %d}\n", a[0], a[1], a[2]);
    printf("b = {%d, %d, %d}\n", b[0], b[1], b[2]);
    printf("c = {%d, %d, %d}\n", c[0], c[1], c[2]);

    return 0;
}

3.編譯源碼

$ gcc -o test test.c -std=c89

4.運行及其結果

$ ./test
probability = 1
a = {7, 5, 3}
b = {8, 6, 1}
c = {9, 4, 2}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章