哪位大神能帮我看一下,这段代码为什么在vscode中编译不了,直接跳出结束,无法输入输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORDS 100000
#define MAX_LEN 100001

typedef struct {
    char *word;
    int count;
}WordFreq;

int cmp_str(const void* a,const void* b){
    return strcmp(*(const char**)a,*(const char**)b);
}

int cmp_freq(const void* a,const void* b){
    const WordFreq* wa=(const WordFreq*)a;
    const WordFreq* wb=(const WordFreq*)b;
    if(wa->count!=wb->count){
         return wb->count-wa->count;
    }
    return strcmp(wa->word,wb->word);
}

int main(){
    char input[MAX_LEN];
    fgets(input,sizeof(input),stdin);
    input[strcspn(input,"\n")]='\0';
    
    char* words[MAX_WORDS];
    int wordCount=0;

    char* token=strtok(input," ");
    while(token!=NULL&&wordCount<MAX_WORDS){
        words[wordCount++]=token;
        token=strtok(NULL," ");
    }

    if(wordCount==0) return 0;

    qsort(words,wordCount,sizeof(char*),cmp_str);

    WordFreq freqs[MAX_WORDS];
    int freqCount=0;

    int i=0;
    while(i<wordCount){
        int j=i;
        while(j<wordCount&&strcmp(words[i],words[j])==0){
            j++;
        }
        int count=j-i;
        if(count>=3){
            freqs[freqCount].word=words[i];
            freqs[freqCount].count=count;
            freqCount++;
        }
        i=j;
    }

    qsort(freqs,freqCount,sizeof(WordFreq),cmp_freq);

    for(int i=0;i<freqCount;i++){
        printf("%s\n",freqs[i].word);
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务