解题思路:
1、用数组存储输入的序列,因为不知道输入数字的个数,所以需要使用malloc开辟内存。
2、移动前的序列与移动后的序列的关系,设 n 为数字的个数,m 为移动的距离,用a[n]存储输入,b[n]是最终结果,则b[n] 和 a[n] 的关系是 b[i] = a[(n-m+i)%n],此处 0<=i<n 。(程序中并没有使用b[n],此处只为说清楚思路)
注意事项:
1、使用malloc开辟内存后,一定要在程序结束前释放掉内存,不然会造成内存泄露。
参考代码:
#include<stdio.h> #include<malloc.h> //清除可能多余的输入 void cleanCache() { while(getchar() != '\n') continue; } int main() { int *a, n, m, i; //获取序列长度 scanf("%d", &n); cleanCache(); a = (int *)malloc(sizeof(int)*n); //获取数字序列 for(i = 0; i < n; i++) { scanf("%d", &a[i]); } cleanCache(); //获取移动长度 scanf("%d", &m); //输出最终结果 for(i = 0; i < n; i++) { printf("%d ", a[(n-m+i)%n]); } free(a); return 0; }
0.0分
3 人评分
输出九九乘法表 (C语言代码)浏览:1560 |
K-进制数 (C++代码)浏览:858 |
简单的a+b (C语言代码)浏览:652 |
Biggest Number (C++代码)回溯法浏览:1614 |
妹子杀手的故事 (C语言代码)浏览:1234 |
A+B for Input-Output Practice (V) (C语言代码)浏览:625 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:514 |
printf基础练习2 (C语言代码)浏览:646 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:591 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:605 |