1097 矩阵行平移
思路:双端队列deque来实现数据的右移(前端push插入,后端pop出)
#include<stdio.h>
#include<iostream>
#include<deque>
#include<vector>
using namespace std;
vector<deque<int> > ve;
int main(int argc, char* argv[]) {
int n, k, x; cin >> n >> k >> x;
int index = 1;
for (int i = 1; i <= n; ++i) {
deque<int> de;
for (int j = 0; j < n; ++j) {
int num; cin >> num;
de.push_back(num);
}
if (i % 2 == 1) {
// 假如是奇数行,则右移index个数字
for (int k = 0; k < index; ++k) {
de.push_front(x);
de.pop_back();
}
if (index == k) { index = 1; }
else { index++; }
}
ve.push_back(de);
}
for (int i = 0; i < n; ++i) {
int sum = 0;
for (int j = 0; j < n; ++j) {
sum += ve[j][i];
}
if (i == 0) { cout << sum; }
else { cout << " " << sum; }
}
return 0;
}