杭电53A题,实在不知道哪里错了,想让大神们帮忙看一下,代码错到11组
问题描述:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct xx
{
char qq[101];
}x[101];
int cmp(const void *x, const void *y)
{
struct xx x1 = *(struct xx *)x;
struct xx y1 = *(struct xx *)y;
if(strcmp(x1.qq, y1.qq) > 0) return 1;
return 0;
}
int main()
{
char s[101];
int n;
while(~scanf("%s", s))
{
int ant = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%s", x[i].qq);
qsort(x, n, sizeof(x[0]), cmp);
for(int i = 0; i < n; i++)
{
if(strstr(x[i].qq, s) - x[i].qq == 0)
{
printf("%s\n", x[i].qq);
break;
}
else ant++;
}
if(ant == n) puts(s);
}
return 0;
}
答
对,我搞错了,试试这个:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct xx
{
char qq[101];
}x[101];
int cmp(const void *x, const void *y)
{
struct xx x1 = *(struct xx *)x;
struct xx y1 = *(struct xx *)y;
return strcmp(x1.qq, y1.qq);//修改
}
int main()
{
char s[101];
int i,n;//修改
while(~scanf("%s", s))
{
scanf("%d", &n);
for( i = 0; i < n; i++)
scanf("%s", x[i].qq);
qsort(x, n, sizeof(x[0]), cmp);
for( i = 0; i < n; i++)
{
if(strstr(x[i].qq, s) - x[i].qq == 0)
{
printf("%s\n", x[i].qq);
break;
}
}
if(i >= n) puts(s);
}
return 0;
}
答
题意理解错误,排序按字符串长度,不是按字母顺序。修改:
int cmp(const void *x, const void *y)
{
struct xx x1 = *(struct xx *)x;
struct xx y1 = *(struct xx *)y;
return strlen(x1.qq)-strlen(y1.qq);
}
答
不好意思啊,那个图片没有传上,不是排序的问题