鱼C论坛

 找回密码
 立即注册
查看: 633|回复: 3

[已解决]代码求助

[复制链接]
发表于 2023-8-12 11:06:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <string.h>
void Longest(char a[]){
    int count = 0,sign=0;
    for(int i=0; i<strlen(a);){
        if(a[i]>=65&&a[i]<=90||a[i]>=97&&a[i]<=122){
            int num =1;
            for(int j=i; j>=0;){
                if(a[j+1]>=65&&a[j+1]<=90||a[j+1]>=97&&a[j+1]<=122){
                    j++;
                    num++;
                }
                else{
                    if(num>count){
                        count=num;
                        sign=i;
                    }
                    i=i+num-1;
                    return;
                }
            }
        }
        else{
            i++;
        }
    }
    printf("最长的单词有%d位,是:%c\n",count,a[sign]);
}

int main(void) {
    char a[1000] = {"Graph convolutional networks (GCNs) have been widely used and achieved remarkable results in skeleton-based action recognition. In GCNs, graph topology dominates feature aggregation and therefore is the key to extracting representative features. ln this work,we propose a novel Channel-wise Topology Refinement Graph Convolution (CTR-GC) to dynamically learn different topologies and effectively aggregate joint features in different channels for skeleton-based action recognition. The proposed CTR-GC models channel-wise topologies through learning a shared topology as a generic prior for all channels and re-fining it with channel-specific correlations for each channel. Our refinement method introduces few extra parameters and significantly reduces the difficulty of modeling channel-wise topologies."};
    Longest(a);
}

题目:输出字符串中最长的单词

问题:无输出,我怀疑是循环的问题。
最佳答案
2023-8-12 11:07:08
问题分析: 首先,您的代码存在一些语法和逻辑错误。在代码中,您试图通过将一个字符数组传递给Longest函数来查找最长的单词,但是您在循环中的许多地方使用了错误的方式来访问和操作字符数组。

代码修改建议:

1. 在C语言中,单个字符的ASCII值应该使用单引号(')括起来,而不是双引号(\)。例如,应该使用'A'而不是\A\

2. 在字符的ASCII范围判断部分,您应该使用单引号括起字符,例如a[i] >= 'A' && a[i] <= 'Z'

3. 在循环部分,您应该使用for循环而不是while循环,并且应该正确地增加循环变量。

4. 在内部循环中,您应该使用正确的循环条件来遍历一个单词,例如使用while(a[j+1] >= 'A' && a[j+1] <= 'Z' || a[j+1] >= 'a' && a[j+1] <= 'z')

5. 最后,在返回之前,应该将i增加以便继续扫描字符串。

以下是经过修正的代码:
#include <stdio.h>#include <string.h>void Longest(char a[]) {    int count = 0, sign = 0;    for (int i = 0; i < strlen(a);) {        if ((a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z')) {            int num = 1;            for (int j = i; j < strlen(a);) {                if ((a[j + 1] >= 'A' && a[j + 1] <= 'Z') || (a[j + 1] >= 'a' && a[j + 1] <= 'z')) {                    j++;                    num++;                } else {                    if (num > count) {                        count = num;                        sign = i;                    }                    i = j + 1;                    break;                }            }        } else {            i++;        }    }    printf(\最长的单词有 %d 位,是:%.*s\\, count, count, &a[sign]);}int main(void) {    char a[1000] = \Graph convolutional networks (GCNs) have been widely used and achieved remarkable results in skeleton-based action recognition. In GCNs, graph topology dominates feature aggregation and therefore is the key to extracting representative features. ln this work, we propose a novel Channel-wise Topology Refinement Graph Convolution (CTR-GC) to dynamically learn different topologies and effectively aggregate joint features in different channels for skeleton-based action recognition. The proposed CTR-GC models channel-wise topologies through learning a shared topology as a generic prior for all channels and re-fining it with channel-specific correlations for each channel. Our refinement method introduces few extra parameters and significantly reduces the difficulty of modeling channel-wise topologies.\;    Longest(a);    return 0;}
注意: 请确保对于输入字符串中的标点符号和空格,您的代码也需要进行适当的处理,以避免将它们包含在单词长度的计算中。以上代码只是修复了原始代码的问题,仍然需要进一步优化以处理更复杂的文本。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-12 11:07:08 | 显示全部楼层    本楼为最佳答案   
问题分析: 首先,您的代码存在一些语法和逻辑错误。在代码中,您试图通过将一个字符数组传递给Longest函数来查找最长的单词,但是您在循环中的许多地方使用了错误的方式来访问和操作字符数组。

代码修改建议:

1. 在C语言中,单个字符的ASCII值应该使用单引号(')括起来,而不是双引号(\)。例如,应该使用'A'而不是\A\

2. 在字符的ASCII范围判断部分,您应该使用单引号括起字符,例如a[i] >= 'A' && a[i] <= 'Z'

3. 在循环部分,您应该使用for循环而不是while循环,并且应该正确地增加循环变量。

4. 在内部循环中,您应该使用正确的循环条件来遍历一个单词,例如使用while(a[j+1] >= 'A' && a[j+1] <= 'Z' || a[j+1] >= 'a' && a[j+1] <= 'z')

5. 最后,在返回之前,应该将i增加以便继续扫描字符串。

以下是经过修正的代码:
#include <stdio.h>#include <string.h>void Longest(char a[]) {    int count = 0, sign = 0;    for (int i = 0; i < strlen(a);) {        if ((a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z')) {            int num = 1;            for (int j = i; j < strlen(a);) {                if ((a[j + 1] >= 'A' && a[j + 1] <= 'Z') || (a[j + 1] >= 'a' && a[j + 1] <= 'z')) {                    j++;                    num++;                } else {                    if (num > count) {                        count = num;                        sign = i;                    }                    i = j + 1;                    break;                }            }        } else {            i++;        }    }    printf(\最长的单词有 %d 位,是:%.*s\\, count, count, &a[sign]);}int main(void) {    char a[1000] = \Graph convolutional networks (GCNs) have been widely used and achieved remarkable results in skeleton-based action recognition. In GCNs, graph topology dominates feature aggregation and therefore is the key to extracting representative features. ln this work, we propose a novel Channel-wise Topology Refinement Graph Convolution (CTR-GC) to dynamically learn different topologies and effectively aggregate joint features in different channels for skeleton-based action recognition. The proposed CTR-GC models channel-wise topologies through learning a shared topology as a generic prior for all channels and re-fining it with channel-specific correlations for each channel. Our refinement method introduces few extra parameters and significantly reduces the difficulty of modeling channel-wise topologies.\;    Longest(a);    return 0;}
注意: 请确保对于输入字符串中的标点符号和空格,您的代码也需要进行适当的处理,以避免将它们包含在单词长度的计算中。以上代码只是修复了原始代码的问题,仍然需要进一步优化以处理更复杂的文本。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 11:07:27 | 显示全部楼层
问题出在循环的判断条件上。在判断字母大小写的时候,需要使用数组索引来获取字符,而不是直接使用字符数组本身。



将代码中的 if(a>=65&&a<=90||a>=97&&a<=122) 修改为 if(a[ i]>=65&&a[ i]<=90||a[ i]>=97&&a[ i]<=122) ,以及 if(a[j+1]>=65&&a[j+1]<=90||a[j+1]>=97&&a[j+1]<=122) 修改为 if(a[j]>=65&&a[j]<=90||a[j]>=97&&a[j]<=122) 。



修改后的代码如下:


#include <stdio.h>

#include <string.h>



void Longest(char a[]) {

    int count = 0, sign = 0;

    for (int i = 0; i < strlen(a);) {

        if (a[ i] >= 65 && a[ i] <= 90 || a[ i] >= 97 && a[ i] <= 122) {

            int num = 1;

            for (int j = i; j >= 0;) {

                if (a[j] >= 65 && a[j] <= 90 || a[j] >= 97 && a[j] <= 122) {

                    j++;

                    num++;

                } else {

                    if (num > count) {

                        count = num;

                        sign = i;

                    }

                    i = i + num - 1;

                    return;

                }

            }

        } else {

            i++;

        }

    }

    printf("最长的单词有%d位,是:%c\n", count, a[sign]);

}



int main(void) {

    char a[1000] = {"Graph convolutional networks (GCNs) have been widely used and achieved remarkable results in skeleton-based action recognition. In GCNs, graph topology dominates feature aggregation and therefore is the key to extracting representative features. ln this work,we propose a novel Channel-wise Topology Refinement Graph Convolution (CTR-GC) to dynamically learn different topologies and effectively aggregate joint features in different channels for skeleton-based action recognition. The proposed CTR-GC models channel-wise topologies through learning a shared topology as a generic prior for all channels and re-fining it with channel-specific correlations for each channel. Our refinement method introduces few extra parameters and significantly reduces the difficulty of modeling channel-wise topologies."};

    Longest(a);

}


修改后的代码应该能够正确输出最长的单词。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 11:07:45 | 显示全部楼层
你提供的代码中存在一些语法错误和逻辑问题导致程序无法正常输出结果。我对代码进行了修正和优化,请参考以下修改后的代码:
#include <stdio.h>
#include <string.h>

void Longest(char a[]) {
    int count = 0, maxCount = 0;
    int start = 0, maxStart = 0;

    int len = strlen(a);
    for (int i = 0; i <= len; i++) {
        if ((a[ i] >= 'A' && a[ i] <= 'Z') || (a[ i] >= 'a' && a[ i] <= 'z')) {
            count++;
            if (count == 1) {
                start = i;
            }
        } else {
            if (count > maxCount) {
                maxCount = count;
                maxStart = start;
            }
            count = 0;
        }
    }

    printf("最长的单词有%d位,是:", maxCount);
    for (int i = maxStart; i < maxStart + maxCount; i++) {
        printf("%c", a[ i]);
    }
    printf("\n");
}

int main(void) {
    char a[1000] = "Graph convolutional networks (GCNs) have been widely used and achieved remarkable results in skeleton-based action recognition. In GCNs, graph topology dominates feature aggregation and therefore is the key to extracting representative features. ln this work,we propose a novel Channel-wise Topology Refinement Graph Convolution (CTR-GC) to dynamically learn different topologies and effectively aggregate joint features in different channels for skeleton-based action recognition. The proposed CTR-GC models channel-wise topologies through learning a shared topology as a generic prior for all channels and re-fining it with channel-specific correlations for each channel. Our refinement method introduces few extra parameters and significantly reduces the difficulty of modeling channel-wise topologies.";

    Longest(a);

    return 0;
}

在修正后的代码中,主要进行了以下改进:
1. 修正了条件判断语句中字母大小写的错误,使用字符常量'A''Z''a''z'代替原来的数字。
2. 添加了变量maxCountmaxStart,分别用于保存最长单词的长度和起始位置。
3. 在循环结束后,通过打印最长的单词子字符串来输出结果。

注意,修正后的代码会输出最长的单词及其长度,但不会输出整个最长单词所在的句子或文本块。如果你需要输出整个单词所在的句子,可以对代码进行进一步修改。

希望能帮助到你!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 08:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表