矩陣的“之”形列印
阿新 • • 發佈:2018-12-13
#include <iostream>
using namespace std;
void round_print(int **a, int x_temp1, int y_temp1, int x_temp2, int y_temp2, bool reverse)
{
if(reverse)
while(x_temp1 <= x_temp2)
{
cout << a[x_temp1][y_temp1] << ' ';
x_temp1 = x_temp1 + 1;
y_temp1 = y_temp1 - 1;
}
else
while(x_temp2 >= x_temp1)
{
cout << a[x_temp2][y_temp2] << ' ';
x_temp2 = x_temp2 - 1;
y_temp2 = y_temp2 + 1;
}
}
int main()
{
cout << "please input rows and columns: ";
int rows, columns; cin >> rows >> columns;
int **nums = new int *[rows];
for(int i = 0; i < rows; i++)
nums[i] = new int [columns];
cout << "please input the data: ";
for(int i = 0; i < rows; i++)
{
for(int ii = 0; ii < columns; ii++)
{
cin >> nums[i][ii];
}
}
cout << "the row data is: " << endl;
for(int i = 0; i < rows; i++)
{
for(int ii = 0; ii < columns; ii++)
{
cout << nums[i][ii] << ' ';
}
cout << endl;
}
cout << "the output data is: " << endl;
int a_temp1 = 0, a_temp2 = 0, b_temp1 = 0, b_temp2 = 0;bool reverse = false;
while(a_temp1 < rows)
{
round_print(nums, a_temp1, a_temp2, b_temp1, b_temp2, reverse);
a_temp1 = (a_temp2 == columns - 1? a_temp1 + 1 : 0);
a_temp2 = (a_temp2 == columns - 1? a_temp2 : a_temp2 + 1);
b_temp2 = (b_temp1 == rows - 1? b_temp2 + 1 : 0);
b_temp1 = (b_temp1 == rows - 1? b_temp1 : b_temp1 + 1);
reverse = !reverse;
}
delete [] nums;
system("pause");
return 0;
}