最长单词(希望在vs平台解决)

  • 题目

    img

  • 输入样例
Keywords insert, two way insertion sort,
Abstract This paper discusses three method for two way insertion
words. insert, two way sorted.
You're a boy.

题目是多组数据,找到单组数据最长单词的函数已经写出来了,但不知道怎么结束程序,早些时候有个老哥用别的平台写了一段代码,但是看不太懂,希望能帮我重写主函数部分。

  • 我写的代码
void find(char b[81])
{
    int i;
    int o;
    int p;
    int count = 0;
    int length = 0;
    int start = 0;
    int m;
    int maxlength = 0;
    int dot = 0;

    for (o = 0; b[o] != '\0'; o++)
    {
        if (b[o] == ' ')
            start = o + 1;
        if (o == start)
        {
            m = o;
            while ((b[o] >= 'A' && b[o] <= 'Z') || (b[o] >= 'a' && b[o] <= 'z'))
            {
                o++;
                length++;
            }
            if (maxlength < length)
            {
                maxlength = length;
                dot = start;
            }
            length = 0;
            o = m;
        }
        if (b[o + 1] == '\0')
        {
            for (i = dot; ((b[i] >= 'A' && b[i] <= 'Z') || (b[i] >= 'a' && b[i] <= 'z')); i++)
            {
                printf("%c", b[i]);
            }
            printf("\n");
        }
    }
}
int main()
{
    char a[100];
    while(gets(a))
    {
        find(a);
    }
    return 0;
}

  • 运行效果

img

python

def find_max_word(s):
    s = s.split()
    max_word = s[0]
    for word in s:
        if len(word) > len(max_word):
            max_word = word
    return max_word

while True:
    try:
        s = input()
        print(find_max_word(s))
    except:
        break

C++

#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;
string find_max_word(string s)
{
    stringstream ss(s);
    string word, max_word;
    while(ss >> word)
    {
        if(word.size() > max_word.size())
            max_word = word;
    }
    return max_word;
}
int main()
{
    string s;
    while(getline(cin, s))
    {
        cout << find_max_word(s) << endl;
    }
    return 0;
}

c

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

#define MAX_LEN 80

void get_longest_word(char str[]){
    int i=0,j=0,k=0;
    int max_len=0,len=0;
    int start=0,end=0,max_start=0;
    while(str[i]){
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
            start=i;
            while((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
                i++;
            }
            end=i-1;
            len=end-start+1;
            if(len>max_len){
                max_len=len;
                max_start=start;
            }
        }
        i++;
    }
    for(j=max_start;j<=max_start+max_len-1;j++){
        printf("%c",str[j]);
    }
    printf("\n");
}

int main(){
    char str[MAX_LEN];
    while(fgets(str,MAX_LEN,stdin)!=NULL){
        str[strlen(str)-1]='\0';
        get_longest_word(str);
    }
    return 0;
}

你可以使用fgets函数代替gets函数。fgets函数可以读取固定长度的字符串,避免缓冲区溢出的情况。

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

void find(char b[81])
{
    int i;
    int o;
    int p;
    int count = 0;
    int length = 0;
    int start = 0;
    int m;
    int maxlength = 0;
    int dot = 0;
    for (o = 0; b[o] != '\0'; o++)
    {
        if (b[o] == ' ')
            start = o + 1;
        if (o == start)
        {
            m = o;
            while ((b[o] >= 'A' && b[o] <= 'Z') || (b[o] >= 'a' && b[o] <= 'z'))
            {
                o++;
                length++;
            }
            if (maxlength < length)
            {
                maxlength = length;
                dot = start;
            }
            length = 0;
            o = m;
        }
        if (b[o + 1] == '\0')
        {
            for (i = dot; ((b[i] >= 'A' && b[i] <= 'Z') || (b[i] >= 'a' && b[i] <= 'z')); i++)
            {
                printf("%c", b[i]);
            }
            printf("\n");
        }
    }
}

int main()
{
    char a[100];
    while(fgets(a, 100, stdin))
    {
        // 去掉换行符
        int len = strlen(a);
        if (a[len - 1] == '\n')
            a[len - 1] = '\0';

        find(a);
    }
    return 0;
}

注意:

在调用find函数前,把换行符删掉。
使用fgets(a, 100, stdin),直到读入失败。