#include <iostream> #include <iomanip> using namespace std; #define N 10 void Prt(int s[][N], int a, int b); int main() { int str[N][N], i, j, a, b; cin >> a >> b; if (a < 1 || b < 1 || a > 10 || b > 10) return 1; for (i = 0; i < a; i++) for (j = 0; j < b; j++) cin>>str[i][j]; Prt(str, a, b); return 0; } void Prt(int s[][N],int a,int b){ int c[a]; for (int i = 0; i < a; i++) { c[i] = 0; } for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { c[i] = c[i] + s[i][j]; } } int d[a]; for (int i = 0; i < a; i++) { d[i] = c[i]; } for (int i = 0; i < a; i++) { for (int j = i + 1; j < a; j++) { if (d[i] < d[j]) { int t = d[i]; d[i] = d[j]; d[j] = t; } } } for (int i = 0; i < a; i++) { if (c[i] == d[0]) { for (int j = 0; j < b; j++) { cout << setw(5) << s[i][j]; } break; } } }优化代码
时间: 2024-04-02 11:33:59 浏览: 15
可以将计算每行总和的代码优化为一行,使用memset函数将c数组初始化为0。同时可以将找到最大值的代码优化为一次遍历,不需要使用额外的数组d。
优化后的代码如下:
void Prt(int s[][N], int a, int b) {
int c[a];
memset(c, 0, sizeof(c));
int max_sum = 0, max_index = 0;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
c[i] += s[i][j];
}
if (c[i] > max_sum) {
max_sum = c[i];
max_index = i;
}
}
for (int j = 0; j < b; j++) {
cout << setw(5) << s[max_index][j];
}
}
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include <iostream> #include <string> #include <stack> #include <vector> #include <iomanip> using namespace std; 逐行注释以上代码
这是一段C++的程序代码,使用了几个标准库的头文件。以下是逐行的注释:
```
#include <iostream> // 输入输出流库头文件
#include <string> // 字符串库头文件
#include <stack> // 栈库头文件
#include <vector> // 向量库头文件
#include <iomanip> // 格式控制库头文件
using namespace std; // 命名空间
```
这段程序代码导入了五个标准库的头文件,分别是iostream、string、stack、vector、iomanip,它们分别用于处理输入输出、字符串、栈、向量和格式控制。其中iostream是C++中最常用的输入输出流库,string是用于字符串操作的库,stack是用于栈数据结构的库,vector是用于动态数组的库,iomanip是用于格式化输出的库。
最后一行代码使用了命名空间std,这意味着所有的标准库函数和变量都在std命名空间中。如果不使用using namespace std,则需要在使用标准库函数时加上std::前缀。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)