List<int> aryList1 = new List<int>(); aryList1.AddRange(new int[] { 85, 92, 67, 94, 94 }); List<int> aryList2 = new List<int>(); aryList2.AddRange(new int[] { 50, 60, 57, 95 }); List<int> aryList3 = new List<int>(); aryList3.AddRange(new int[] { 95 }); List<List<int>> classGrade = new List<List<int>>(); classGrade.Add(aryList1); classGrade.Add(aryList2); classGrade.Add(aryList3); List<int> highestGrade(List<List<int>> p2) { List<int> result = new List<int>(); for (int i = 0; i < p2.Count; i++) { int max; List<int> temp = p2[i]; for (int j = 1; j < temp.Count; j++) { if (temp[0] < temp[j]) { max = temp[j]; temp[j] = temp[0]; temp[0] = max; } } result.Add(temp[0]); } return result; }; Console.WriteLine($"The highest grade is {highestGrade(classGrade).Max()}"); 改进代码
时间: 2024-03-20 21:41:37 浏览: 23
可以将内部循环改为使用 LINQ 的 Max() 方法来查找列表中的最大值,而不必手动进行比较和交换。同时,可以使用 var 关键字简化代码。改进后的代码如下:
```
List<int> highestGrade(List<List<int>> p2)
{
List<int> result = new List<int>();
foreach (var temp in p2)
{
result.Add(temp.Max());
}
return result;
}
List<List<int>> classGrade = new List<List<int>>
{
new List<int> { 85, 92, 67, 94, 94 },
new List<int> { 50, 60, 57, 95 },
new List<int> { 95 }
};
Console.WriteLine($"The highest grade is {highestGrade(classGrade).Max()}");
```
相关问题
List<int> aryList1 = new List<int>(); aryList1.AddRange(new int[] { 1, 5, 3 }); List<int> aryList2 = new List<int>(); aryList2.AddRange(new int[] { 9, 7, 3, -2 }); List<int> aryList3 = new List<int>(); aryList3.AddRange(new int[] { 2, 1, 2 }); List<List<int>> aryList4 = new List<List<int>>(); aryList4.Add(aryList1); aryList4.Add(aryList2); aryList4.Add(aryList3); List<int> maxNumbersInLists(List<List<int>> p1) { List<int> result = new List<int>(); for (int i = 0; i < p1.Count; i++) { int max; List<int> temp = p1[i]; for (int j = 1; j < temp.Count; j++) { if (temp[0] < temp[j]) { max = temp[j]; temp[j] = temp[0]; temp[0] = max; } } result.Add(temp[0]); } return result; } for (int i = 0; i < maxNumbersInLists(aryList4).Count; i++) { Console.WriteLine($"List {i + 1} has a maximum of {maxNumbersInLists(aryList4)[i]}."); } 改进
可以对代码进行如下改进:
1. 可以使用 var 关键字来进行变量类型推断,使代码更加简洁。
2. 在找到每个列表的最大值时,可以使用 LINQ 中的 Max() 方法进行查找,避免手动进行循环。
3. 在 for 循环中,maxNumbersInLists(aryList4) 方法被多次调用,可以将其调用结果存储在变量中。
4. 可以使用 foreach 循环来遍历 maxNumbersInLists(aryList4) 方法的结果,使代码更加简洁。
改进后的代码如下:
```
var aryList1 = new List<int> { 1, 5, 3 };
var aryList2 = new List<int> { 9, 7, 3, -2 };
var aryList3 = new List<int> { 2, 1, 2 };
var aryList4 = new List<List<int>> { aryList1, aryList2, aryList3 };
List<int> maxNumbersInLists(List<List<int>> p1)
{
var result = new List<int>();
foreach (var list in p1)
{
result.Add(list.Max());
}
return result;
}
var maxNumbers = maxNumbersInLists(aryList4);
foreach (var maxNumber in maxNumbers)
{
Console.WriteLine($"List {maxNumbers.IndexOf(maxNumber) + 1} has a maximum of {maxNumber}.");
}
```
这样可以使代码更加简洁易读,同时提高了运行效率。
改进代码 list<int> arylist1 = new list<int>(); arylist1.addrange(new int[] { 85, 92, 67, 94, 94 }); list<int> arylist2 = new list<int>(); arylist2.addrange(new int[] { 50, 60, 57, 95 }); list<int> arylist3 = new list<int>(); arylist3.addrange(new int[] { 95 }); list<list<int>> classgrade = new list<list<int>>(); classgrade.add(arylist1); classgrade.add(arylist2); classgrade.add(arylist3); list<int> highestgrade(list<list<int>> p2) { list<int> result = new list<int>(); for (int i = 0; i < p2.count; i++) { int max; list<int> temp = p2[i]; for (int j = 1; j < temp.count; j++) { if (temp[0] < temp[j]) { max = temp[j]; temp[j] = temp[0]; temp[0] = max; } } result.add(temp[0]); } return result; }; console.writeline($"the highest grade is {highestgrade(classgrade).max()}");
这段代码实现的功能是找到班级中每个学生的最高成绩,并输出班级中的最高分数。但是在代码实现上存在一些问题:
1. 在对每个学生的成绩进行比较时,没有对变量max进行初始化,可能会导致结果不准确。
2. 每次比较只将最大值和第一个值进行比较,可能会导致结果不准确。
3. 在输出班级中的最高分数时,使用了max()方法,但是这个方法是找到列表中的最大值,而不是找到列表中的最后一个值,可能会导致结果不准确。
下面是改进后的代码:
```
list<int> highestgrade(list<list<int>> p2) {
list<int> result = new list<int>();
for (int i = 0; i < p2.count; i++)
{
int max = p2[i][0];
list<int> temp = p2[i];
for (int j = 1; j < temp.count; j++)
{
if (max < temp[j])
{
max = temp[j];
}
}
result.add(max);
}
return result;
}
console.writeline($"the highest grade is {highestgrade(classgrade)[highestgrade(classgrade).count-1]}");
```
改进后的代码将变量max进行了初始化,并且在比较时将每个值都和max进行比较,确保找到最大值。在输出班级中的最高分数时,使用了列表中的最后一个值,确保找到班级中的最高分数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)