有n个整数 使前面各数顺序向后移m个位置 最后m个数变成最前面m个数 C++编程

2016-06-23 15:40:59  分类: c++程序设计第三版谭浩强课后答案  参与:

C++编程,要求用指针或引用方法处理:
 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见下图,写一个函数实现以上功能,在主函数中输入n个整数和输出调整后的n个数。

谭浩强c++程序设计第3版答案
以下是此题的【c++源代码】,需要【c源代码】请点击进入
#include <iostream>
using namespace std;
int main()
 {void move(int *array,int n,int m);
  int number[20],n,m,i;
  cout<<"how many numbers?";               // 询问共有多少个数
  cin>>n;
  cout<<"input "<<n<<" numbers:"<<endl;    // 要求输入n个数
  for (i=0;i<n;i++)
    cin>>number[i];
  cout<<"how many places do you want move?";  // 询问后移多少个位置
  cin>>m;
  move(number,n,m);                       //调用move 函数
  cout<<"Now,they are:"<<endl;
  for (i=0;i<n;i++)
    cout<<number[i]<<" ";
  cout<<endl;
  return 0;
}

void move(int *array,int n,int m)             //使循环后移一次的函数
 {int *p,array_end;
  array_end=*(array+n-1);
  for (p=array+n-1;p>array;p--)
    *p=*(p-1);
  *array=array_end;
  m--;
  if (m>0) move(array,n,m); //递归调用,当循环次数m减至为0时,停止调用
 }

 
 

 
  


 

来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_6.4.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接