hust-A Whole Lot of Walnuts解题报告

hust-A Whole Lot of Walnuts解题报告

Description

Ten big walnuts is a whole lot of walnuts, that's for sure! Two walnuts is too little for anybody. How about six walnuts? Is it a small number of walnuts or a large one? The Parrot has already found an answer to this question: after eating a small amount of walnuts he wants to eat some more, while after eating a large amount of walnuts he feels satisfied. The Parrot enjoys eating and hates overeating; that's why he wants to know a minimal number of walnuts that is enough to get satisfied.
In order to calculate this number, he conducted a series of experiments. Each experiment went like that: the Parrot, being quite hungry, ate a number of walnuts and checked if this was enough to get himself satisfied. Of course, if some number of walnuts is enough, any larger number will do either; vice versa, if after eating a number of walnuts he is still hungry, no smaller number can get the parrot satisfied. You should help the Parrot to process the results of the experiments.

Input

The first line of the input contains an integer  n — the number of experiments conducted by the parrot  (0 ≤  n ≤ 100) . The following  n lines contain descriptions of these experiments. A description of an experiment consists of a number of walnuts eaten by the parrot (an integer from 3 to 9) and an outcome: “satisfied” in case this number was enough to get the parrot satisfied or “hungry” otherwise. It is known as a fact, that ten walnuts is always enough and two walnuts are always not enough.

Output

Output the minimal number of walnuts that is enough to get the parrot satisfied. If the results of the experiments are inconsistent, output “Inconsistent”.

Sample Input

input output
4
4 hungry
7 satisfied
5 hungry
8 satisfied
7
2
6 hungry
5 satisfied
Inconsistent

这一题我wrong了很多次,而最终比赛结果也证明我们的确是栽在了这一题。哎,真是无颜面对我的队友啊~~

大致题意

第一行输入一个整数n,代表n只鹦鹉。

接下来每一行都有一组实验数据,一个整数m(m>=3 && m<=9),一个字符串,代表这只鹦鹉吃完m个核桃之后的状态。satisfied表示鹦鹉吃饱了。

现在就是要根据实验数据输出能让鹦鹉吃饱的最少的核桃数目。实验数据可能会不合实际,如果实验数据不合理,则输出Inconsistent。

其实这个题目很水的,看来还是自己的做题能力不行,脑子短路了。竟然wrong了这么多遍...........

最后附上AC的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>//使用memset,必须要记得加这个啊,今天长记性了
#include<math.h>
int main()
{
    int n;
    int a[7];//用于记录鹦鹉吃完核桃后的状态,1代表饿了,2代表饱了
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        int flag1=0;//flag1用于判定是否在输入实验数据时,吃同样数量的核桃,鹦鹉的状态是不是一样的
        while(n--)
        {
            int num;
            char b[12];
            scanf("%d %s",&num,b);
            if(b[0]=='h')
            {
                if(a[num-3]!=2)
                    a[num-3]=1;
                if(a[num-3]==2)
                    flag1=1;//flag1==1时代表吃同样多的核桃,鹦鹉状态不一样,等于0,则说明是一样的
            }
            if(b[0]=='s')
            {
                if(a[num-3]!=1)
                    a[num-3]=2;
                if(a[num-3]==1)
                    flag1=1;
            }
        }
        if(flag1==0)
        {
            int i,j,temp,flag2=0,flag3=0;//flag2用于判断是否有能让鹦鹉吃饱的实验数据。flag3用于判断实验数据有没有不合理的地方
            for(i=0;i<7;i++)
            {
                if(a[i]==2)
                {
                    temp=i;
                    flag2=1;
                    break;
                }
            }
            if(flag2==0) printf("10\n");//这也是该题的一个坑,当从实验数据无法得知鹦鹉什么时候能吃饱时,就输出10
            else
            {
                for(j=temp;j<7;j++)
                {
                    if(a[j]==1)
                    {
                        printf("Inconsistent\n");
                        flag3=1;
                        break;
                    }
                }
                if(flag3==0)
                    printf("%d\n",temp+3);
            }
        }
        else printf("Inconsistent\n");
    }
    return 0;
}


比赛之后我看了一下大神的代码,果然让我和我的小朋友们都惊呆了,用四个字形容:短小精悍。ORZ啊

下面附上大神代码:

#include <cstdio>

int n, x;
char s[20];

int main() {
    while (scanf("%d", &n) == 1) {
        int st = 0, en = 10;
        while (n--) {
            scanf("%d %s", &x, s);
            if (s[0] == 'h' && st < x) st = x;//大神用了一个非常巧妙的方法,类似于在坐标轴上标记的方法,逐渐来逼近~~
            if (s[0] == 's' && en > x) en = x;//st代表hungry位置,en代表satisfied位置
        }
        if (st < en) printf("%d\n", en);
        else puts("Inconsistent");
    }
    return 0;
}
总结:多做题,多思考,像大神们学习。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值