算法训练 最长字符串
时间限制:1.0s 内存限制:512.0MB
求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。
样例输入
one two three four five
样例输出
three
#include<stdio.h>
#include<string.h>
int main(){
char str[5][100];
int max=0;//记录最长串的下标
int a[5];//记录字符串长度的数组
for(int i=0;i<5;i++){
scanf("%s",str[i]);
a[i]=strlen(str[i]);//只有将str定义为二维数组才行
}
for(int i=0;i<5;i++){
if(a[i]>a[max]){
max = i;
}
}
printf("%s",str[max]);
return 0;
}
本文介绍了一个简单的算法挑战,即从五个字符串中找出最长的一个。通过使用C语言的二维字符数组和长度函数,我们实现了这一目标。示例代码展示了如何读取字符串,计算其长度,并比较它们以找到最长的字符串。
2752





