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://codercto.com/courses/l/17.html
Web Applications (Hacking Exposed)
Joel Scambray、Mike Shema / McGraw-Hill Osborne Media / 2002-06-19 / USD 49.99
Get in-depth coverage of Web application platforms and their vulnerabilities, presented the same popular format as the international bestseller, Hacking Exposed. Covering hacking scenarios across diff......一起来看看 《Web Applications (Hacking Exposed)》 这本书的介绍吧!
MD5 加密
MD5 加密工具
RGB CMYK 转换工具
RGB CMYK 互转工具