汶先生201 2023-02-01 00:33 采纳率: 66.7%
浏览 34
已结题

最长单词(希望在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

  • 写回答

1条回答 默认 最新

  • m0_54204465 2023-02-01 07:25
    关注

    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),直到读入失败。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月10日
  • 已采纳回答 2月2日
  • 修改了问题 2月1日
  • 修改了问题 2月1日
  • 展开全部

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答