谁家孩子跑得最慢

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}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章