PAT 1097 矩阵行平移 (20 分)
个人感觉题目描述的不太清楚,这里的意思是,奇数行向右平移,每次平移1,2,3 … k, 1, 2, 3, … ,k。并不是1, k, 1, k。而且示例给的实在太狡猾了。容易被误导(别问我是怎么知道容易被误导的)
题目本身不难,可以采用数学的方式直接计算,也可以采取笨方法再重新开一个数组进行模拟。
C++代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, k, x;
cin >> n >> k >> x;
vector<vector<int>> a(n,vector<int>(n));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> a[i][j];
}
}
vector<vector<int>> b(n,vector<int>(n));
int now = 1;
for (int i = 0; i < n; i++){
if (i % 2 == 0) {
for (int j = 0; j < now; j++){
b[i][j] = x;
}
for (int j = now; j < n; j++){
b[i][j] = a[i][j - now];
}
now++;
if (now == k + 1){
now = 1;
}
} else {
for (int j = 0; j < n; j++){
b[i][j] = a[i][j];
}
}
}
for (int i = 0; i < n; i++){
int sum = 0;
for (int j = 0; j < n; j++){
sum += b[j][i];
}
if (i == 0){
cout << sum;
}else {
cout << " " << sum;
}
}
cout << endl;
return 0;
}
- 喜欢的话可以点个赞哦