[蓝桥杯]基础练习 回形取数
程序员文章站
2022-05-11 22:28:15
...
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int m, n;
cin>>m>>n;
vector<vector<int> >nums(m, vector<int>(n, 0));
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin>>nums[i][j];
}
}
int count = 0;
int t = 0;
while(count < m*n)
{
for(int i = 0; i < m; i++)
{
if(nums[i][t] != -1)
{
cout<<nums[i][t];
nums[i][t] = -1;
count++;
if(count != m*n)
{
cout<<" ";
}
}
}
for(int i = 0; i < n; i++)
{
if(nums[m-t-1][i] != -1)
{
cout<<nums[m-t-1][i];
nums[m-t-1][i] = -1;
count++;
if(count != m*n)
{
cout<<" ";
}
}
}
for(int i = m -1; i >= 0; i--)
{
if(nums[i][n-t-1] != -1)
{
cout<<nums[i][n-t-1];
nums[i][n-t-1] = -1;
count++;
if(count != m*n)
{
cout<<" ";
}
}
}
for(int i = n-1; i >= 0; i--)
{
if(nums[t][i] != -1)
{
cout<<nums[t][i];
nums[t][i] = -1;
count++;
if(count != m*n)
{
cout<<" ";
}
}
}
t++;
}
return 0;
}