C 练习实例68

C 语言教程 · 2019-02-22 10:56:33

题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。

程序分析:无。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include <stdio.h> #include <stdlib.h> int main() { int arr[20]; int i,n,offset; //输入数组大小和数组内容 printf("Total numbers?\n"); scanf("%d",&n); printf("Input %d numbers.\n",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); //输入滚动偏移量 printf("Set your offset.\n"); scanf("%d",&offset); printf("Offset is %d.\n",offset); //打印滚动前数组 print_arr(arr,n); //滚动数组并打印 move(arr,n,offset); print_arr(arr,n); } //打印数组 void print_arr(int array[],int n) { int i; for(i=0;i<n;++i) printf("%4d",array[i]); printf("\n"); } //滚动数组 void move(int array[],int n,int offset) { int *p,*arr_end; arr_end=array+n; //数组最后一个元素的下一个位置 int last; //滚动直到偏移量为0 while(offset) { last=*(arr_end-1); for(p=arr_end-1;p!=array;--p) //向右滚动一位 *p=*(p-1); *array=last; --offset; } }

点击查看所有 C 语言教程 文章: https://www.codercto.com/courses/l/17.html

查看所有标签

Compilers

Compilers

Alfred V. Aho、Monica S. Lam、Ravi Sethi、Jeffrey D. Ullman / Addison Wesley / 2006-9-10 / USD 186.80

This book provides the foundation for understanding the theory and pracitce of compilers. Revised and updated, it reflects the current state of compilation. Every chapter has been completely revised ......一起来看看 《Compilers》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具