提交时间:2019-12-14 19:10:56

运行 ID: 3238

#include <iostream> using namespace std; int main() { int num; cout << "请输入对角矩阵的大小" << endl; cin >> num; int **p = new int*[num];//数组大小动态,二维 for (int i = 0; i <num; i++) p[i] = new int[num]; int sum = 0; int number; for (int i = 0; i < num; i++) { cout << "请输入矩阵元素"; cout << "第" << (i + 1) << "行" << endl; for (int j = 0; j < num; j++){ cout << "第" << (j + 1) << "列" << endl; cin >> number; p[i][j] = number; } } for (int i = 0; i < num; i++) { sum += p[i][i] + p[i][num-i-1];//两个对角线元素相加 } cout << "sum=" << sum << endl; return 0; }