#include <iostream> #include <iomanip> #include <cstring> #include <cmath> using namespace std; #define ROW 3 #define COL 4 int main() { int a[ROW][COL], i, j, *p[3]; //指针数组;p[0]为a[0][0]的地址 int sum; for (i = 0; i < ROW; i++) a[i][0] = *p[i]; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) cin >> *(p[i] + j); //a[i][j] } sum = 0; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (j % 2 == 0) sum = sum + *(p[i] + j); } cout << endl; } cout << "sum=" << sum << endl; return 0; }优化
时间: 2024-03-23 10:43:24 浏览: 23
这段代码可以优化的地方有很多,以下是一些可能的改进:
1. 指针数组的初始化可以放在一起,简化代码:
```
int a[ROW][COL], i, j, *p[3] = {a[0], a[1], a[2]};
```
2. 在第一次循环中,可以直接用指针数组来给每一行的第一个元素赋值,不需要用指针解引用:
```
for (i = 0; i < ROW; i++) {
p[i] = a[i];
}
```
3. 在第二次循环中,可以直接用指针数组来访问二维数组的元素,不需要用指针解引用:
```
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
cin >> p[i][j];
}
}
```
4. 在计算奇数列元素和的时候,可以使用步长为2的循环,不需要用if语句判断:
```
sum = 0;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j += 2) {
sum += p[i][j];
}
cout << endl;
}
```
5. 输出变量sum的值时,可以使用控制符setw()来控制输出宽度,让输出更美观:
```
cout << "sum = " << setw(3) << sum << endl;
```
综上所述,优化后的代码如下:
相关问题
#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<stack> #include<queue>
这是C++的头文件引用,分别包含了输入输出流、栈和队列的类。其中iostream头文件定义了输入输出流的类,stack头文件定义了栈的类,queue头文件定义了队列的类。以下是它们的简单使用示例:
1. iostream头文件的使用:
```c++
#include<iostream>
using namespace std;
int main() {
int num;
cin >> num; // 从控制台读取一个整数
cout << "You entered " << num << endl; // 输出读取到的整数
return 0;
}
```
2. stack头文件的使用:
```c++
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int> s; // 定义一个int类型的栈
s.push(1); // 压入元素1
s.push(2); // 压入元素2
s.push(3); // 压入元素3
while (!s.empty()) { // 遍历栈中元素
cout << s.top() << " "; // 输出栈顶元素
s.pop(); // 弹出栈顶元素
}
return 0;
}
```
3. queue头文件的使用:
```c++
#include<iostream>
#include<queue>
using namespace std;
int main() {
queue<int> q; // 定义一个int类型的队列
q.push(1); // 入队1
q.push(2); // 入队2
q.push(3); // 入队3
while (!q.empty()) { // 遍历队列中元素
cout << q.front() << " "; // 输出队首元素
q.pop(); // 弹出队首元素
}
return 0;
}
```
相关推荐
![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)