向后移动M个数
有n个整数,使其前面各数顺序向后移动m个位置,最后m个数变成最前面的m个数。例如 12345 向后移动2个数 变成45123.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int total, a[20], m;
printf("the total numbers is:");
scanf("%d", &total);
printf("back :");
scanf("%d", &m);
if (total > 20 || m > total) {
printf("error!");
return 0;
}
for (int i = 0; i < total; i++) {
scanf("%d", &a[i]);
}
for (; m > 0; m--) { //移动m次
int end = a[total - 1];
for (int p = total - 1; p > 0; p--) {
a[p] = a[p - 1];
}
a[0] = end;
}
for (int j = 0; j < total; j++) {
printf("%d ", a[j]);
}
return 0;
}
运行结果: