文章目錄
- Character Wheels
- 題目描述
- 輸入描述
- 輸出描述
- 題目大意
- 示例1
- 輸入
- 輸出
- 思路
- C++ 代碼
Character Wheels
鏈接:https://ac.nowcoder.com/acm/contest/8688/I
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 524288K,其他語言1048576K
64bit IO Format: %lld
題目描述
You are given one n \times nn×n character wheels, and it is guaranteed that n is even. Please implement these following operations:

Clockwise/Counter-clockwise rotate the x-th wheel y(1≤y≤109 ) times. Rotating one time means rotatting 90 degrees. The instruction is L/R x y, R indicates rotating clockwise and L indicates rotating counter-clockwise. For example, R 1 3 means rotate the 1-st wheel 3 times clockwise.
Print all the wheels, the instruction is P.
輸入描述
First line contains one integer 𝑛 ( 4 ≤ 𝑛 ≤ 50), indicates the size of the wheels.
Then followed the wheels.
After that, one line contains one integer 𝑚 ( 1 ≤ 𝑚 ≤ 100), indicates the number ofoperations.
Then followed 𝑚 lines, one line one instruction.
it is guaranteed that at least there is one P instruction and the wheels only contain lowercase letters.
輸出描述
If the instruction is P, then print all the wheels.
題目大意
給一個n*n的矩陣,從外到內每一圈編號1~n/2,輸入R就是向右轉,L就是向左轉,將第x圈旋轉y次,每次旋轉90°,注意題中要求y的次數小于10的九次方,因此需要y % 4 簡化次數,避免超時
輸入P列印當前矩陣
示例1
輸入
4
abcd
cabe
fgha
edbe
5
L 1 1
P
R 1 3
L 2 1
P
輸出
deae
cabb
bghd
acfe
ebde
abhf
eagc
dcba
思路
先把矩陣記錄下來,
定義一個二位陣列存入每個環的字符順序,(均從一號位開始存入)a[環層數][0] 初始為0
之后根據具體旋轉情況,改變a[環層數][0] 的 值,R1向右轉加一,L1向左轉減一,
輸入P就將二維陣列從新回傳矩陣,將矩陣輸出即可,

C++ 代碼
#include<iostream>
#include<cstring>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N = 10005;
char ma[55][55];
char a[55][N];
int n,T;
void print(){
for(int i = 1; i <= n/2; i ++)
{
int m = 1;
if(a[i][0] == '1'){
int hang = i,lie = i;
do{ma[hang][lie] = a[i][m++]; lie ++;}while(m <= n - 2 * i + 1);
do{ma[hang][lie] = a[i][m++]; hang++;}while(m <= 2*n - 4*i + 2);
do{ma[hang][lie] = a[i][m++]; lie--;}while(m <= 3*n - 6*i + 3);
do{ma[hang][lie] = a[i][m++]; hang--; }while(m <= 4*n - 8*i + 4);
}
else if(a[i][0] == '2'){
int hang = i,lie = n - i + 1;
do{ma[hang][lie] = a[i][m++]; hang ++;}while(m <= n - 2 * i + 1);
do{ma[hang][lie] = a[i][m++]; lie --;}while(m <= 2*n - 4*i + 2);
do{ma[hang][lie] = a[i][m++]; hang --;}while(m <= 3*n - 6*i + 3);
do{ma[hang][lie] = a[i][m++]; lie ++; }while(m <= 4*n - 8*i + 4);
}
else if(a[i][0] == '3'){
int hang = n - i + 1,lie = n - i + 1;
do{ma[hang][lie] = a[i][m++]; lie --;}while(m <= n - 2 * i + 1);
do{ma[hang][lie] = a[i][m++]; hang --;}while(m <= 2*n - 4*i + 2);
do{ma[hang][lie] = a[i][m++]; lie ++;}while(m <= 3*n - 6*i + 3);
do{ma[hang][lie] = a[i][m++]; hang ++; }while(m <= 4*n - 8*i + 4);
}
else if(a[i][0] == '4'){
int hang = n - i + 1,lie = i;
do{ma[hang][lie] = a[i][m++]; hang --;}while(m <= n - 2 * i + 1);
do{ma[hang][lie] = a[i][m++]; lie ++;}while(m <= 2*n - 4*i + 2);
do{ma[hang][lie] = a[i][m++]; hang ++;}while(m <= 3*n - 6*i + 3);
do{ma[hang][lie] = a[i][m++]; lie --; }while(m <= 4*n - 8*i + 4);
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j ++)
{
cout << ma[i][j];
}
cout << endl;
}
}
int main()
{
IO;
memset(a,0,sizeof(a));
cin >> n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
cin >> ma[i][j];
for(int i = 1; i <= n/2; i ++)
{
a[i][0] = '1';
int m = 1,hang = i,lie = i;
do{a[i][m++] = ma[hang][lie]; lie++;}while(m <= n - 2 * i + 1);//m <= (n - 2*(i - 1) - 1) //向右
do{a[i][m++] = ma[hang][lie]; hang++;}while(m <= 2*n - 4*i + 2);//m <= 2*(n - 2*(i - 1) - 1) //向下
do{a[i][m++] = ma[hang][lie]; lie--;}while(m <= 3*n - 6*i + 3);//m <= 3*(n - 2*(i - 1) - 1) //向左
do{a[i][m++] = ma[hang][lie]; hang--; }while(m <= 4*n - 8*i + 4);//m <= 4*(n - 2*(i - 1) - 1) //向上
}
cin >> T;
while(T --)
{
char ch;
int x,y;
cin >> ch;
getchar();
if(ch == 'L'){
cin >> x >> y;
y = y % 4;
while(y--){
a[x][0] --;
if(a[x][0] < '1')a[x][0] += 4;
}
}
else if(ch == 'R'){
cin >> x >> y;
y = y % 4;
while(y--){
a[x][0] ++;
if(a[x][0] > '4')a[x][0] -= 4;
}
}
else if(ch == 'P'){
print();
}
}
// for(int j = 1; j <= n/2; j ++)
// {
// for(int i = 1; i <= 4*(n-1); i ++)
// cout << a[j][i] << " ";
// cout << endl;
// }
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202223.html
標籤:其他
上一篇:從頻域計算信號回圈譜
