这一题死的很悲剧,就n值问题,起初我还一直以为是排序的cmp出问题了,检查它,却忽略了后面的检查……呜呜~~~~(>_<)~~~~ 竟然wa了那么多次,而且用了那么长时间,如此基础的题目,看来我检查错误的能力还有待改善啊~~~
View Code
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
struct node
{
char a1[100];
char b1[100];
char c1[100];
}in[1000];
int cmp(const void *a,const void *b)
{
node *c=(node *)a;
node *d=(node *)b;
if(!strcmp(c->b1,d->b1))
{
if(!strcmp(c->c1,d->c1))
{
return strcmp(c->a1,d->a1);
}
else if(!strcmp(c->c1,"wonderful")&&(!strcmp(d->c1,"good")||!strcmp(d->c1,"so-so")))
return -1;
else if((!strcmp(c->c1,"good")||!strcmp(c->c1,"so-so"))&&!strcmp(d->c1,"wonderful"))
return 1;
else if(!strcmp(c->c1,"so-so")&&strcmp(d->c1,"so-so"))
return 1;
else if(!strcmp(c->c1,"good")&&!strcmp(d->c1,"so-so"))
return -1;
else
return 0;
}
return strcmp(c->b1,d->b1);
}
int main()
{
int n;
int count=1;
while(scanf("%d",&n)==1)
{
int i;
for(i=0;i<n;i++)
{
scanf("%s%s%s",in[i].a1,in[i].b1,in[i].c1);
}
qsort(in,n,sizeof(node),cmp);
/*for(i=0;i<n;i++)
{
printf("%s %s %s\n",in[i].a1,in[i].b1,in[i].c1);
}*/
printf("Case %d\n",count++);
int j;
for(i=0;i<n;i++)
{
printf("%s:\n",in[i].b1);
printf(" %s %s\n",in[i].a1,in[i].c1);
while(!strcmp(in[i].b1,in[i+1].b1)&&i<n-1)
{
printf(" %s %s\n",in[i+1].a1,in[i+1].c1);
i++;
}
}
}
return 0;
}
就输出那里的i<n-1这个地方啊,漏了这个就错了,因为有可能全部的b1都是相同的……