例:設空白為X
1 XXXX1
10 XXX10
100 XX100
2.每筆測資間,即輸出完需再換行一次,但最後一筆不用。
#include<iostream>
#include<string>
using namespace std;
int main()
{
int M, n, num, a, b;
int arr[10][10], ans[10][10];
string s;
cin >> M;
for (int i = 0; i < M; i++)
{
num = 1;
cin >> n;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++)
{
arr[x][y] = num;
num++;
}
}
getline(cin, s);
getline(cin, s);
for (int j = 0; j < s.length(); j++)
{
if (s[j] == 'R')
{
a = 0;
for (int y = n - 1; y >= 0; y--)
{
b = 0;
for (int x = 0; x < n; x++)
{
ans[x][y] = arr[a][b];
b++;
}
a++;
}
}
else if (s[j] == 'L')
{
b = n - 1;
for (int x = 0; x < n; x++)
{
a = 0;
for (int y = 0; y < n; y++)
{
ans[x][y] = arr[a][b];
a++;
}
b--;
}
}
else
{
a = 0;
for (int x = n - 1; x >= 0; x--)
{
b = 0;
for (int y = 0; y < n; y++)
{
ans[x][y] = arr[a][b];
b++;
}
a++;
}
}
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
arr[x][y] = ans[x][y];
}
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++)
{
if (arr[x][y] < 10)
cout << " " << ans[x][y];
else if (arr[x][y] < 100)
cout << " " << ans[x][y];
else
cout << " " << ans[x][y];
}
cout << endl;
}
if (i < M - 1)
cout << endl;
}
return 0;
}
沒有留言:
張貼留言