有关数组 & 动态数组,输出数组中最长字符以及最短字符,若最长最短不止一个,都输出

1、初始化时设置他的数组大小

string[] myarr = new string[2];
myarr[0] = "1";
myarr[1]="第二个值";
2、初始化时设置他的数组大小并赋值

string[] myarr = new string[]{"1","2"};
3、可以动态改变数组大小的数组

List<string> listArr = new List<string>();
listArr.add("添加的值");
listArr.add("再添加一个值");
listArr.add("OK");
listArr.RemoveAt(1);//删除索引1的项
....//该方法操作数组更加灵活。如果不需要动态操作数组的话,可以不使用该方法。
string[] myarr = listArr.ToArray();//将List类型转成string[]类型的数组

======================================================================================================

动态数组,输出数组中最长字符以及最短字符,若最长最短不止一个,都输出

#region 控制台输入字符,最后将最长和最短的字符输出
//输出数组中最长和最短的字符
string[] strlib = new string[5];
for (int i = 0; i < 5; i++)
{
string str = Console.ReadLine();
strlib[i] = str;
}
int strLenMax = strlib[0].Length;//记录最长字符长度
int strLenMin = strlib[0].Length;//记录最短字符长度
for (int i = 1; i < strlib.Length; i++)
{
//先计算数组中最大长度字符长度和最短长度字符长度
if (strlib[i].Length < strLenMin)
{
strLenMin = strlib[i].Length;
}
if (strlib[i].Length > strLenMax)
{
strLenMax = strlib[i].Length;
}
}
Console.WriteLine("数组元素中最大长度字符串与最短长度字符串分别为:");
for (int j = 0; j < strlib.Length; j++)
{
if (strlib[j].Length == strLenMax || strlib[j].Length == strLenMin)
{
Console.WriteLine(strlib[j]);
}
}
#endregion







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下步骤实现: 1. 定义一个字符数组str,长度为5。 2. 使用循环语句,读入5个字符串,将它们存入数组str中。 3. 定义两个变量,分别记录最长字符串和最短字符串,初始值为数组中的第一个字符串。 4. 使用循环语句,遍历数组str,比较每个字符串的长度,更新最长字符串和最短字符串。 5. 输出最长字符串和最短字符串。 示例代码如下: ```python # 定义字符数组 str = ["", "", "", "", ""] # 读入5个字符串 for i in range(5): str[i] = input("请输入第%d个字符串:" % (i+1)) # 初始化最长字符串和最短字符串 max_str = str[] min_str = str[] # 遍历数组,更新最长字符串和最短字符串 for i in range(1, 5): if len(str[i]) > len(max_str): max_str = str[i] if len(str[i]) < len(min_str): min_str = str[i] # 输出结果 print("最长字符串为:%s" % max_str) print("最短字符串为:%s" % min_str) ``` ### 回答2: 题目要求我们从键盘上读入5个字符串,然后找出其中最长的和最短字符串,再将这两个字符输出。下面是一个简单的实现方法。 首先,我们需要用一个数组来存储这5个字符串。代码如下: ``` #include <iostream> #include <string> using namespace std; int main() { string str[5]; // 声明长度为5的字符数组 for (int i = 0; i < 5; i++) { cin >> str[i]; // 从键盘上读入5个字符串,存入数组str中 } // 输出数组中的所有字符串,用于检查是否读入正确 for (int i = 0; i < 5; i++) { cout << str[i] << endl; } return 0; } ``` 接着,我们需要找出数组中最长的和最短字符串。我们可以用一个for循环遍历数组,同时记录当前最长最短字符串。代码如下: ``` #include <iostream> #include <string> using namespace std; int main() { string str[5]; // 声明长度为5的字符数组 for (int i = 0; i < 5; i++) { cin >> str[i]; // 从键盘上读入5个字符串,存入数组str中 } // 找到最长最短字符串 int maxIndex = 0, minIndex = 0; for (int i = 1; i < 5; i++) { if (str[i].length() > str[maxIndex].length()) { maxIndex = i; } if (str[i].length() < str[minIndex].length()) { minIndex = i; } } // 输出最长最短字符串 cout << "最长字符串是:" << str[maxIndex] << endl; cout << "最短字符串是:" << str[minIndex] << endl; return 0; } ``` 最后,我们将上述两段代码合并起来,就可以完整地实现要求了。完整代码如下: ``` #include <iostream> #include <string> using namespace std; int main() { string str[5]; // 声明长度为5的字符数组 for (int i = 0; i < 5; i++) { cin >> str[i]; // 从键盘上读入5个字符串,存入数组str中 } // 找到最长最短字符串 int maxIndex = 0, minIndex = 0; for (int i = 1; i < 5; i++) { if (str[i].length() > str[maxIndex].length()) { maxIndex = i; } if (str[i].length() < str[minIndex].length()) { minIndex = i; } } // 输出最长最短字符串 cout << "最长字符串是:" << str[maxIndex] << endl; cout << "最短字符串是:" << str[minIndex] << endl; return 0; } ``` ### 回答3: 题目要求从键盘上读入5个字符串存入数组str中,然后输出这5个字符串中最长字符串和最短字符串。 我们可以通过使用C++语言的标准库函数来实现这个题目。具体的做法有以下两个步骤: 1. 读入5个字符串存入数组str中。 要读入5个字符串并存入数组中,可以使用C++标准库函数getline()和循环来实现。getline()函数可以读入一行字符串,可以通过循环使用多次来分别读入5个字符串,将它们存储在一个字符数组中。 以下是用C++代码实现5个字符串的读入和存储的方法: ```c++ #include <iostream> #include <string> using namespace std; int main(){ string str[5]; for(int i = 0; i < 5; i++){ getline(cin, str[i]); } return 0; } ``` 在上面的代码中,我们定义了一个字符数组str,并用循环读入了5个字符串并存储到该数组中。 2. 输出最长最短字符串。 要输出最长最短字符串,可以通过循环遍历整个字符数组,查找出最长最短字符串,然后将它们输出。 以下是用C++代码实现输出最长最短字符串的方法: ```c++ #include <iostream> #include <string> using namespace std; int main(){ string str[5]; for(int i = 0; i < 5; i++){ getline(cin, str[i]); } string maxstr = str[0]; string minstr = str[0]; for(int i = 1; i < 5; i++){ if(str[i].length() > maxstr.length()){ maxstr = str[i]; } if(str[i].length() < minstr.length()){ minstr = str[i]; } } cout << "最长字符串是:" << maxstr << endl; cout << "最短字符串是:" << minstr << endl; return 0; } ``` 在上面的代码中,我们定义了两个字符串变量maxstr和minstr,将其初始化为数组中的第一个字符串。然后使用循环遍历整个字符数组,查找出最长最短字符串,并将其分别保存到maxstr和minstr变量中。最后输出maxstr和minstr变量的值即可。 综上所述,这个题目可以使用C++的标准库函数和循环来实现,只需两个步骤即可完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值