大连理工c语言第五次上机作业参考答案

2020-12-24 20:32

第五次上机作业:(请用指针实现)

1. 数据移位

有n(n<=10)个整数,使前面各数顺序向后移m(m<=n)个位置,最后m个数变成最前面的m个数,如图所示。编写函数shift(a,n,m)实现上述功能,在主函数中输入n个整数和输出调整后的n个数。

SAMPLE OUTPUT nInput n(n<=10):10

Input 10 integers:1 2 3 4 5 6 7 8 9 10

Output the original:1 2 3 4 5 6 7 8 9 10

Input m(m<=10):4

Output the shifted:7 8 9 10 1 2 3 4 5 6

#include <stdio.h>

#define N 10

void shift(int a[ ],int n,int m);

void main( )

{ int i,n,m;

int a[N];

printf("Input n(n<=10):");

scanf("%d",&n);

printf("Input %d integers:\n",n);

for(i=0;i<n;i++)

scanf("%d",a+i);

printf("Output the original:\n");

for(i=0;i<n;i++)

printf("%d ",*(a+i));

printf("\n");

printf("Input m(m<=%d):",n);

scanf("%d",&m);

shift(a,n,m);

printf("Output the shifted:\n");

for(i=0;i<n;i++)

printf("%d ",*(a+i));

printf("\n");

}

void shift(int a[ ],int n,int m)

{ int i,j,t;

for(i=0;i<m;i++)

{ t=a[n-1];

for(j=n-2;j>=0;j--)

a[j+1]=a[j];

a[0]=t;

}

}


大连理工c语言第五次上机作业参考答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:回首过去,我们激情满怀,展望未来,我们信心百倍

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: