| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1169 人关注过本帖
标题:关于杭电1052田忌赛马的问题
只看楼主 加入收藏
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:10 
程序代码:
#include"stdio.h"
int main()
{
    int n;
    int a[2][1024]={0,0},t,w,i,j;
    int tmax,tmin ,kmax, kmin;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;    
        for(i=1;i<=n;i++)scanf("%d",&a[0][i]);
        for(i=1;i<=n;i++)scanf("%d",&a[1][i]);
        for(i=1;i<=n;i++)                            //排序田忌与国王的马的速度
        {
            for(j=1;j<=n-i;j++)
            {
                if(a[0][j]>a[0][j+1])
                {
                    t=a[0][j];
                    a[0][j]=a[0][j+1];
                    a[0][j+1]=t;
                }
                if(a[1][j]>a[1][j+1])
                {
                    t=a[1][j];
                    a[1][j]=a[1][j+1];
                    a[1][j+1]=t;
                }
            }
        }
        w=0;
        p=0;
        tmax = n;
           tmin = 1;
        kmax = n;
        kmin = 1;
        for(i=1;i<=n;i++)                       
        {
            if(a[0][tmax]>a[1][kmax])
            {
                w++;
                tmax--;
                kmax--;
            }else if(a[0][tmax]<a[1][kmax]){
                w--;
                tmin++;
                kmax--;
            }else{
                if(a[0][tmin]>a[1][kmin]){
                    w++;
                    tmin++;
                    kmin++;
                }else if(a[0][tmin]<a[1][kmin]){
                    w--;
                    tmin++;
                    kmax--;
                }else{
                    if(a[0][tmin]<a[1][kmax]){
                        w--;
                        tmin++;
                        kmax--;
                    }
                }
            }
        }
        printf("%d\n",w*200);
    }
}


1 田忌的最快与齐王最快比,田忌快则PK掉,田忌慢则用田忌最慢的PK齐王最快;
2 快马相等的话,用田忌的最慢与齐王最慢比,田忌快则PK掉,田忌慢则PK齐王最快;
3 最快与最慢都相等,则用田忌最慢PK掉齐王最快;

2010-08-04 19:00
不言葬月
Rank: 1
等 级:新手上路
帖 子:12
专家分:3
注 册:2010-7-22
收藏
得分:0 
终于A了啊,不容易啊~~谢谢各位的帮助哈~~
就是代码有点长~~
#include"stdio.h"
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        int a[2][1024]={0,0},t,w,i,j,t1=1,g1=1,t2=1,g2=1;
        for(i=1;i<=n;i++)scanf("%d",&a[0][i]);
        for(i=1;i<=n;i++)scanf("%d",&a[1][i]);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n-i;j++)
            {
                if(a[0][j]<a[0][j+1])
                {
                    t=a[0][j];
                    a[0][j]=a[0][j+1];
                    a[0][j+1]=t;
                }
                if(a[1][j]<a[1][j+1])
                {
                    t=a[1][j];
                    a[1][j]=a[1][j+1];
                    a[1][j+1]=t;
                }
            }
        }
        t2=g2=n;
        w=0;
        for(i=1;i<=n;i++)
        {
            if(a[0][t1]>a[1][g1])
            {
                w++;
                t1++;
                g1++;
                continue;
            }
            if(a[0][t1]==a[1][g1])
            {
                if(a[0][t2]>a[1][g2])
                {
                    w++;
                    t2--;
                    g2--;
                    continue;
                }
                else
                {
                    if(a[0][t2]>a[1][g1])
                    {
                        w++;
                        t2--;
                        g1++;
                        continue;
                    }
                    if(a[0][t2]<a[1][g1])
                    {
                        w--;
                        t2--;
                        g1++;
                        continue;
                    }
                    if(a[0][t2]==a[1][g1])
                    {
                        t2--;
                        g1++;
                        continue;
                    }
                }
            }
            if(a[0][t1]<a[1][g1])
            {
                    if(a[0][t2]>a[1][g1])
                    {
                        w++;
                        t2--;
                        g1++;
                        continue;
                    }
                    if(a[0][t2]<a[1][g1])
                    {
                        w--;
                        t2--;
                        g1++;
                        continue;
                    }
                    if(a[0][t2]==a[1][g1])
                    {
                        t2--;
                        g1++;
                        continue;
                    }
            }
        }
   
        printf("%d\n",w*200);
    }
}
2010-08-04 20:08
zclaile
Rank: 1
等 级:新手上路
帖 子:15
专家分:8
注 册:2010-8-20
收藏
得分:0 
好猛的说。。。。
2010-08-20 13:03
快速回复:关于杭电1052田忌赛马的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025652 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved