冒牌嘚小冬瓜 2015-12-23 06:38 采纳率: 50%
浏览 1446
已采纳

杭电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;
} 

杭电原题链接

  • 写回答

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;
    } 
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料