算法训练 最长字符串  
时间限制:1.0s   内存限制:512.0MB
    
  求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。
样例输入
one two three four five
样例输出
three
 
作者注释:
  有一个疑问:代码第九行,为何 若str定义为一维数组就会报错?
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char str[5][100];
 5     int max=0;//记录最长串的下标 
 6     int a[5];//记录字符串长度的数组 
 7     for(int i=0;i<5;i++){
 8         scanf("%s",str[i]); 
 9         a[i]=strlen(str[i]);//只有将str定义为二维数组才行 
10     }
11     for(int i=0;i<5;i++){
12         if(a[i]>a[max]){
13             max = i;
14         }
15     }
16     printf("%s",str[max]);
17 }

 

 

相关文章:

  • 2021-11-04
  • 2021-09-18
  • 2021-07-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-05-18
  • 2021-11-14
  • 2021-10-17
相关资源
相似解决方案