代码求助
#include <stdio.h>#include <string.h>
void Longest(char a[]){
int count = 0,sign=0;
for(int i=0; i<strlen(a);){
if(a>=65&&a<=90||a>=97&&a<=122){
int num =1;
for(int j=i; j>=0;){
if(a>=65&&a<=90||a>=97&&a<=122){
j++;
num++;
}
else{
if(num>count){
count=num;
sign=i;
}
i=i+num-1;
return;
}
}
}
else{
i++;
}
}
printf("最长的单词有%d位,是:%c\n",count,a);
}
int main(void) {
char a = {"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);
}
题目:输出字符串中最长的单词
问题:无输出,我怀疑是循环的问题。{:10_269:} 问题分析: 首先,您的代码存在一些语法和逻辑错误。在代码中,您试图通过将一个字符数组传递给Longest函数来查找最长的单词,但是您在循环中的许多地方使用了错误的方式来访问和操作字符数组。
代码修改建议:
1. 在C语言中,单个字符的ASCII值应该使用单引号(')括起来,而不是双引号(\)。例如,应该使用'A'而不是\A\。
2. 在字符的ASCII范围判断部分,您应该使用单引号括起字符,例如a >= 'A' && a <= 'Z'。
3. 在循环部分,您应该使用for循环而不是while循环,并且应该正确地增加循环变量。
4. 在内部循环中,您应该使用正确的循环条件来遍历一个单词,例如使用while(a >= 'A' && a <= 'Z' || a >= 'a' && a <= '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 >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z')) {
int num = 1;
for (int j = i; j < strlen(a);) {
if ((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z')) {
j++;
num++;
} else {
if (num > count) {
count = num;
sign = i;
}
i = j + 1;
break;
}
}
} else {
i++;
}
}
printf(\最长的单词有 %d 位,是:%.*s\
\, count, count, &a);
}
int main(void) {
char a = \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;
}
注意: 请确保对于输入字符串中的标点符号和空格,您的代码也需要进行适当的处理,以避免将它们包含在单词长度的计算中。以上代码只是修复了原始代码的问题,仍然需要进一步优化以处理更复杂的文本。 问题出在循环的判断条件上。在判断字母大小写的时候,需要使用数组索引来获取字符,而不是直接使用字符数组本身。
将代码中的 if(a>=65&&a<=90||a>=97&&a<=122) 修改为 if(a[ i]>=65&&a[ i]<=90||a[ i]>=97&&a[ i]<=122) ,以及 if(a>=65&&a<=90||a>=97&&a<=122) 修改为 if(a>=65&&a<=90||a>=97&&a<=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 >= 65 && a <= 90 || a >= 97 && a <= 122) {
j++;
num++;
} else {
if (num > count) {
count = num;
sign = i;
}
i = i + num - 1;
return;
}
}
} else {
i++;
}
}
printf("最长的单词有%d位,是:%c\n", count, a);
}
int main(void) {
char a = {"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);
}
修改后的代码应该能够正确输出最长的单词。 你提供的代码中存在一些语法错误和逻辑问题导致程序无法正常输出结果。我对代码进行了修正和优化,请参考以下修改后的代码:
#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 = "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. 添加了变量maxCount和maxStart,分别用于保存最长单词的长度和起始位置。
3. 在循环结束后,通过打印最长的单词子字符串来输出结果。
注意,修正后的代码会输出最长的单词及其长度,但不会输出整个最长单词所在的句子或文本块。如果你需要输出整个单词所在的句子,可以对代码进行进一步修改。
希望能帮助到你!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
页:
[1]