#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;
}
杭电53A题,实在不知道哪里错了,想让大神们帮忙看一下,代码错到11组
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- ysuwood 2015-12-27 10:02关注
对,我搞错了,试试这个:
#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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报