鱼C论坛

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

[已解决]我修改了一下,还是不能正确输出

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

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

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

x
  1. #include <stdio.h>
  2. #include <string.h>
  3. void Longest(char a[]){
  4.     int count = 0,sign=0;
  5.     for(int i=0; i<strlen(a);){
  6.         if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z'){
  7.             int num =1;
  8.             for(int j=i; j>=0;){
  9.                 if(a[j+1]>='a'&&a[j+1]<='z'||a[j+1]>='A'&&a[j+1]<='Z'){
  10.                     j++;
  11.                     num++;
  12.                 }
  13.                 else{
  14.                     if(num>count){
  15.                         count=num;
  16.                         sign=i;
  17.                     }
  18.                     i=i+num-1;
  19.                     return;
  20.                 }
  21.             }
  22.         }
  23.         else{
  24.             i++;
  25.         }
  26.     }
  27.     for(int i=0;i<count;i++){
  28.         printf("%c",a[i+sign]);
  29.     }
  30. }

  31. int main(void) {
  32.     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."};
  33.     Longest(a);
  34. }
复制代码



问题:输出字符串中最长的单词
最佳答案
2023-8-12 11:20:40
感谢您提供了修改后的代码。我注意到在定义字符数组 a 时,您使用了大括号 {} 将字符串括起来,这是导致程序无法正确输出结果的原因之一。在 C 语言中,使用双引号 "" 来定义字符串,不需要额外的大括号。

另外,修改后的代码中还有几处逻辑错误和小问题。我已经帮您进行了修正和优化,以下是修改后的代码:

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

  3. void Longest(char a[]) {
  4.     int count = 0, maxCount = 0;
  5.     int sign = 0, maxStart = 0;

  6.     for (int i = 0; i < strlen(a);) {
  7.         if ((a[ i] >= 'a' && a[ i] <= 'z') || (a[ i] >= 'A' && a[ i] <= 'Z')) {
  8.             int num = 1;
  9.             for (int j = i; j < strlen(a);) {
  10.                 if ((a[j + 1] >= 'a' && a[j + 1] <= 'z') || (a[j + 1] >= 'A' && a[j + 1] <= 'Z')) {
  11.                     j++;
  12.                     num++;
  13.                 } else {
  14.                     if (num > count) {
  15.                         count = num;
  16.                         sign = i;
  17.                     }
  18.                     i = i + num - 1;
  19.                     break;
  20.                 }
  21.             }
  22.         } else {
  23.             i++;
  24.         }
  25.     }

  26.     printf("最长的单词有 %d 位,是:", count);
  27.     for (int i = sign; i < sign + count; i++) {
  28.         printf("%c", a[ i]);
  29.     }
  30.     printf("\n");
  31. }

  32. int main(void) {
  33.     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.";

  34.     Longest(a);

  35.     return 0;
  36. }
复制代码


在修正后的代码中,主要进行了以下改进:
1. 移除了字符数组 a 定义时的多余大括号。
2. 修正了变量的命名和作用域,以保证正确的结果输出。
3. 优化了循环和条件语句的结构,以提升代码的可读性和效率。

现在,修改后的代码应该能够正确输出字符串中最长的单词及其长度。希望对您有所帮助!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-12 11:19:47 | 显示全部楼层
问题: 输出字符串中最长的单词。

你好!我看到你的代码并进行了一些修改,以便正确输出最长的单词。以下是修改后的完整代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h> // 添加ctype.h头文件,用于判断字符是否为字母
  4. void LongestWord(char a[]) {
  5.     int maxLength = 0;  // 最长单词的长度
  6.     int startPos = 0;   // 最长单词的起始位置
  7.     int currentLength = 0;  // 当前正在统计的单词的长度
  8.     int currentStart = 0;   // 当前正在统计的单词的起始位置
  9.     int i = 0;
  10.     while (a[i] != '\\0') {
  11.         if (isalpha(a[i])) { // 判断字符是否为字母
  12.             if (currentLength == 0) {
  13.                 currentStart = i;
  14.             }
  15.             currentLength++;
  16.         } else {
  17.             if (currentLength > maxLength) {
  18.                 maxLength = currentLength;
  19.                 startPos = currentStart;
  20.             }
  21.             currentLength = 0;
  22.         }
  23.         i++;
  24.     }
  25.     // 处理最后一个单词的情况
  26.     if (currentLength > maxLength) {
  27.         maxLength = currentLength;
  28.         startPos = currentStart;
  29.     }
  30.     for (int j = startPos; j < startPos + maxLength; j++) {
  31.         printf(\%c\, a[j]);
  32.     }
  33.     printf(\\
  34. \);
  35. }
  36. int main(void) {
  37.     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.\};
  38.     LongestWord(a);
  39.     return 0;
  40. }
复制代码

这个修改后的代码会找出输入字符串中的最长单词并输出。我希望这次的修改能够满足你的需求!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 11:20:03 | 显示全部楼层
你的代码有一些问题,导致不能正确输出最长的单词。首先,你的字符串数组声明有误,应该使用双引号括起来,而不是大括号。其次,在你的循环中,你没有正确处理单词的结束条件。下面是修改后的代码:

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

  3. void Longest(char a[]){
  4.     int count = 0, sign = 0;
  5.     int max_count = 0, max_sign = 0;
  6.    
  7.     for(int i = 0; i < strlen(a); i++){
  8.         if((a[ i] >= 'a' && a[ i] <= 'z') || (a[ i] >= 'A' && a[ i] <= 'Z')){
  9.             int num = 1;
  10.             for(int j = i; j < strlen(a); j++){
  11.                 if((a[j+1] >= 'a' && a[j+1] <= 'z') || (a[j+1] >= 'A' && a[j+1] <= 'Z')){
  12.                     num++;
  13.                 }
  14.                 else{
  15.                     if(num > count){
  16.                         count = num;
  17.                         sign = i;
  18.                     }
  19.                     break;
  20.                 }
  21.             }
  22.         }
  23.     }
  24.    
  25.     for(int i = 0; i < count; i++){
  26.         printf("%c", a[i+sign]);
  27.     }
  28. }

  29. int main(void) {
  30.     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.";
  31.     Longest(a);
  32.     return 0;
  33. }
复制代码


这样,代码就会正确输出最长的单词。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 11:20:40 | 显示全部楼层    本楼为最佳答案   
感谢您提供了修改后的代码。我注意到在定义字符数组 a 时,您使用了大括号 {} 将字符串括起来,这是导致程序无法正确输出结果的原因之一。在 C 语言中,使用双引号 "" 来定义字符串,不需要额外的大括号。

另外,修改后的代码中还有几处逻辑错误和小问题。我已经帮您进行了修正和优化,以下是修改后的代码:

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

  3. void Longest(char a[]) {
  4.     int count = 0, maxCount = 0;
  5.     int sign = 0, maxStart = 0;

  6.     for (int i = 0; i < strlen(a);) {
  7.         if ((a[ i] >= 'a' && a[ i] <= 'z') || (a[ i] >= 'A' && a[ i] <= 'Z')) {
  8.             int num = 1;
  9.             for (int j = i; j < strlen(a);) {
  10.                 if ((a[j + 1] >= 'a' && a[j + 1] <= 'z') || (a[j + 1] >= 'A' && a[j + 1] <= 'Z')) {
  11.                     j++;
  12.                     num++;
  13.                 } else {
  14.                     if (num > count) {
  15.                         count = num;
  16.                         sign = i;
  17.                     }
  18.                     i = i + num - 1;
  19.                     break;
  20.                 }
  21.             }
  22.         } else {
  23.             i++;
  24.         }
  25.     }

  26.     printf("最长的单词有 %d 位,是:", count);
  27.     for (int i = sign; i < sign + count; i++) {
  28.         printf("%c", a[ i]);
  29.     }
  30.     printf("\n");
  31. }

  32. int main(void) {
  33.     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.";

  34.     Longest(a);

  35.     return 0;
  36. }
复制代码


在修正后的代码中,主要进行了以下改进:
1. 移除了字符数组 a 定义时的多余大括号。
2. 修正了变量的命名和作用域,以保证正确的结果输出。
3. 优化了循环和条件语句的结构,以提升代码的可读性和效率。

现在,修改后的代码应该能够正确输出字符串中最长的单词及其长度。希望对您有所帮助!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-11 04:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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