PAT上分記(c++ + 完美過關)---1008---數組元素循環右移問題

數組元素循環右移的問題

在這裏插入圖片描述

#include<stdio.h>
#include<iostream>

static int a[100];
void move(int* num, int step, int len) {
	int b[100];
	step = step % len;
	for (int i = 0; i < len; i++) {
		if (i - step < 0) {
			b[i] = num[len + i - step];
		}
		else {
			b[i] = num[i - step];
		}
	}
	for (int j = 0; j < 100; j++) {
		a[j] = b[j];
	}
}

int main() {
	int m, n;
	std::cin >> m >> n;

	for (int i = 0; i < m; i++) {
		std::cin >> a[i];
	}
	move(a, n, m);
	for (int i = 0; i < m; i++) {
		if (i != m - 1) {
			std::cout << a[i] << ' ';
		}
		else {
			std::cout << a[i];
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章