HDU 2600-War

War

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3386    Accepted Submission(s): 1212


题目链接:点击打开链接


Problem Description
      War is the reciprical and violent application of force between hostile political entities aimed at bringing about a desired political end-state via armed conflict. War is an interaction in which two or more militaries have a “struggle of wills”. When qualified as a civil war, it is a dispute inherent to a given society, and its nature is in the conflict over modes of governance rather than sovereignty. War is not considered to be the same as mere occupation,murder or genocide because of the reciprical nature of the violent struggle, and the organized nature of the units involved. 
      War is also a cultural entity, and its practice is not linked to any single type of political organisation or society. Rather, as discussed by John Keegan in his “History Of Warfare”, war is a universal phenomenon whose form and scope is defined by the society that wages it.

      Today we don't want to talk about war, rather than , I want you to tell me which year there was no war.
 

Input
For each test case, first input one integer N(1<= N <= 100), then two integers p and q (-6000000 <= p <= q <= 6000000) represent the starting year and the ending year in this case. Followed by N wars.
Each war described as follows:
A i B i N i (A,B is integer, p <= A, B <= q, N i is a String which is the ith war's name )
Represent that the ith War took place between year A and year B.
 

Output
Output one number represent which year there was no war in the range p and q, if there are not only one year, output the maximum year which there was no war. If all the year had war, just output "Badly!". 
 

Sample Input
 
 
3 100 200 100 120 RtWar 110 140 WeWar 141 199 QqWar 1 -600 600 -600 600 Cool War
 

Sample Output
 
 
200 Badly!


题意:

本题是给你一个 n ,表示有 n 场战争,然后给出一个开始年份 p 和结束年份 q ,然后再给出 n 场战争开始时间和结束时间,让你判断在给出的年份中,是否有没有战争的年份,如果都有战争发生,则输出 Badly!,如果有多个符合条件的年份,则输出最大的年份。


分析:

本题就是一个结构加排序,但是我很迷惑,因为我自己的代码提交Accepted ,但是看网上很多人的代码就不是很理解了,感觉哪里不对,但是呢,提交上去也能过,接下来我给出两个代码,第一个是我的,第二个是网上别人的代码,然后我给出一组测试数据:

3

1 6

1 3 sdh

2 4 dszh

5 6 szhn

对于这组测试数据,我的代码是Badly!,而别人的代码是 3 ,好奇怪,有知道的宝宝可以和我说说

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;

struct War{
    int a,b;///a表示该战争开始的年份,b表示该战争结束的年份
}war[105];

bool cmp(War x,War y)///按照战争开始时间早的排序,如果相等,则按结束时间早的排序
{
    if(x.a==y.a)
        return x.b<y.b;
    else
        return x.a<y.a;
}

int main()
{
    char s[105];
    int p,q,n;
    while(~scanf("%d",&n))
    {
        scanf("%d %d",&p,&q);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&war[i].a,&war[i].b);
            gets(s);///注意这里,对于字符输入一向很迷糊的我,用scanf一直错,所以改用gets
        }
        sort(war,war+n,cmp);
        if(war[n-1].b<q)
        {
            printf("%d\n",q);///如果最大的年份没有战争,则直接输出最大的年份
            continue;
        }
        int bja,bjb,bj;
        bja=war[0].a;
        bjb=war[0].b;
        bj=bja-1;///记录没有战争的最大年份
        for(int i=1;i<n;i++)
        {
            if(bjb+1>=war[i].a)///说明两场战争是衔接的或是交叉的
                bjb=war[i].b;
            else///否则得话,则要改变bj的值
            {
                bjb=war[i].b;
                bj=war[i].a-1;
            }
        }
        if(bj<p)
            printf("Badly!\n");
        else
            printf("%d\n",bj);
    }
    return 0;
}





#include<stdio.h>
#include<algorithm>
using namespace std;
char ss[107];
struct war
{
    int s,e;
}w[107];
int cmp(war a,war b)
{
    if(a.e==b.e)return a.s>b.s;
    return a.e>b.e;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int s,e;
        scanf("%d%d",&s,&e);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&w[i].s,&w[i].e);
            gets(ss);
        }
        sort(w,w+n,cmp);
        if(e>w[0].e){printf("%d\n",e);continue;}
        e=w[0].s-1;
        int flag=0;
        for(int i=1;i<n;i++)
        {
            if(e>w[i].s&&e<w[i].e)
                e=w[i].s-1;
            else {flag=1;printf("%d\n",e-1);}
        }
        if(!flag)printf("Badly!\n");
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值