【贪心算法】田忌赛马问题代码和注释

来源:互联网 发布:软件无线电 军用 编辑:程序博客网 时间:2024/06/05 16:33

描述
田忌与齐王赛马,双方各有n匹马参赛(n<=100),每场比赛赌注为1两黄金,现已知齐王与田忌的每匹马的速度,并且齐王肯定是按马的速度从快到慢出场,现要你写一个程序帮助田忌计算他最好的结果是赢多少两黄金(输用负数表示)。
Tian Ji and the king play horse racing, both sides have n horse (n is no more the 100), every game a bet of 1 gold, now known king and Tian Ji each horse’s speed, and the king is definitely on the horse speed from fast to slow, we want you to write a program to help Tian Ji his best result is win the number gold (lost express with the negative number).
输入
多个测例。
每个测例三行:第一行一个整数n,表示双方各有n匹马;第二行n个整数分别表示田忌的n匹马的速度;第三行n个整数分别表示齐王的n匹马的速度。
n=0表示输入结束。
A plurality of test cases.
Each test case of three lines: the first line contains an integer n, said the two sides each have n horse; second lines of N integers n Tian Ji horse speed; third lines of N integers King n horse speed.
N = 0 indicates the end of input.
输出
每行一个整数,田忌最多能赢多少两黄金。
how many gold the tian ji win
输入样例
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
3
20 20 10
20 20 10
0
输出样例
1
0
0
0

思路:
1、先从田忌的最差的马和齐王的最差的马比,如果田马速度大于齐马,赢一场!
2、如果田忌的最差的马比齐王最差的马还差,让田的最差马和齐王最好的马比,输一场
3、如果田忌的最差的马比齐王最差的马一样速度,则比较田忌最好的马和齐王最好的马:
{
(1).如果田忌最好的马比齐王最好的马快,赢一场。
(2).如果田忌最好的马比齐王最好的马慢或两匹马一样快,则比较田忌最差的马和齐王最好的马
{
1).如果田忌的最差的马比齐王最好的马慢,则输一场。
2).如果田忌最差的马和齐王最好的马一样快,则赢一场。
}
}

#include<stdio.h>int n,tianji[101],qiwang[101]; void sort(int m[]);int race();int main(){    while(1)    {        int i;        scanf("%d",&n);        if(n==0) return 0;        for(i=1;i<=n;i++)        {            scanf("%d",&tianji[i]);        }         for(i=1;i<=n;i++)        {            scanf("%d",&qiwang[i]);        }        sort(tianji);        sort(qiwang);        //将两个人的马都由小到大排序,方便比较        printf("%d\n",race());    }}void sort(int m[]){    int i,j,t;      for(i=1;i<n;i++)    {        for(j=1;j<=n-i;j++)        {            if(m[j]>m[j+1])            {                t=m[j];                m[j]=m[j+1];                m[j+1]=t;            }        }     }}int race(){    int x0=1,y0=1,x1=n,y1=n,g=0,count=0;    //x0为田忌所剩的最差的马的编号,y0为齐王所剩的最差的马的编号;    //x1为田忌所剩的最好的马的编号,y1为齐王所剩的最好的马的编号;    //count为田忌当前赢的钱数    //g为两人已经进行过几场比赛    while(++g<=n)//有n匹马,要进行n场比赛    {        if(tianji[x0]>qiwang[y0])        //田忌最差的马比齐王最差的马快,赢一场。        //用掉了自己最差的马,同时胜利,这是最优情况。        {            count+=1;            x0++;            y0++;        }        else if(tianji[x0]<qiwang[y0])    //如果田忌最差的马比齐王最差的马还慢,无论策略如何都会输一局。    //这时用田忌最差的马和齐王最快的马比,虽然输了一局,但是消耗了齐王最好的马。        {            count-=1;            y1--;            x0++;        }        else if(tianji[x0]==qiwang[y0])        //当田忌最差的马和齐王最差的马一样快时        //比较田忌最好的马和齐王最好的马,分情况讨论        {            if(tianji[x1]>qiwang[y1])            //如果田忌最好的马比齐王最好的马快,赢一局            {                count+=1;                x1--;                y1--;            }            //如果田忌最好的马比齐王最好的马慢,用田忌最差的马和齐王最好的马比赛            //需要考虑到田忌最差的马和齐王最好的马一样快的特殊情况            else            {                if(tianji[x0]==qiwang[y1])                //田忌最差的马和齐王最好的马一样快,平局                {                    count+=0;                    x0++;                    y1--;                }                else                //田忌最差的马比齐王最好的马慢,输一局                {                    count-=1;                    x0++;                    y1--;                }            }        }    }    return count;}
原创粉丝点击