hust-A Whole Lot of Walnuts解题报告
大致题意:
第一行输入一个整数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;
}
下面附上大神代码:
#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;
}
总结:多做题,多思考,像大神们学习。