#2558. 2019 CSP-J1 真题 · 完善程序(矩阵变换)
2019 CSP-J1 真题 · 完善程序(矩阵变换)
2019 CSP-J1 真题(120 分钟,总分 100 分)
完善程序(矩阵变换)(共 5 题,共 15.0 分)
1. (矩阵变换)有一个奇幻的矩阵,在不停的变幻,其变幻方式为:数字0变成矩阵\begin{bmatrix}
0 & 0 \
0 & 1
\end{bmatrix}数字1变成矩阵\begin{bmatrix}
1 & 1 \
1 & 0
\end{bmatrix}。最初该矩阵只有一个元素0,变幻n次后,矩阵会变成什么样?
例如,矩阵最初为:[0]:矩阵变幻1次后:\begin{bmatrix}
0 & 0 \
0 & 1
\end{bmatrix};矩阵变幻2次后:\begin{bmatrix}
0 & 0 & 0 & 0 \
0 & 1 & 0 & 1 \
0 & 0 & 1 & 1 \
0 & 1 & 1 & 0
\end{bmatrix}。
输入一行一个不超过10的正整数n。输出变幻n次后的矩阵。试补全程序。
提示:
“«” 表示二进制左移运算符,例如(11)<sub>2</sub> <<2 = (1100)<sub>2</sub>;
而“^”表示二进制异或运算符,它将两个参与运算的数中的每个对应的二进制 位一进行比较,若两个二进制位相同,则运算结果的对应二进制位为0 ,反之 为1。
#include <cstdio>
using namespace std;
int n;
const int max_size = 1 << 10;
int res[max_size][max_size];
void recursive(int x, int y, int n, int t)
{
if (n == 0)
{
res[x][y] = ① ;
return;
}
int step = 1 << (n - 1);
recursive( ②, n - 1, t);
recursive(x, y + step, n - 1, t);
recursive(x + step, y, n - 1, t);
recursive( ③ , n - 1, !t);
}
int main()
{
scanf("%d", &n);
recursive(0, 0, ④ );
int size = ⑤;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
printf("%d", res[i][j]);
puts(" ");
}
return 0;
}
①处应填()
{{ select(1) }}
cpp n%2cpp 0cpp tcpp 1
2. ②处应填( )
{{ select(2) }}
cpp x-step, y-stepcpp x, y-stepcpp x-step, ycpp x,y
3. ③处应填( )
{{ select(3) }}
cpp x-step, y-stepcpp x+step, y+stepcpp x-step, ycpp x, y-step
4. ④处应填( )
{{ select(4) }}
cpp n-1, n%2cpp n,0cpp n,n%2cpp n-1,0
5. ⑤处应填( )
{{ select(5) }}
cpp 1<<(n+1)cpp 1<<ncpp n+1cpp 1<<(n-1)
相关
在下列比赛中: