数组右移问题

1.刚开始的思路是要搞N个变量,觉得不好也没做太占空间,后来看了别人的办法,用时间换空间的办法

#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
 int len=101,step,i,j,temp,count=0;
 scanf("%d",&len);
 scanf("%d",&step);
 int array[len];
 for(i=0;i<=len-1;i++)
 {
  scanf("%d",&array[i]);
  count++;
 }
 for(i=0;i<len;i++)
 {
  printf("%d ",array[i]);
 }
 for(i=0;i<step;i++)//循环的次数
 {
  
  temp=array[count-1];  //用一个变量存储最后一个数字,做N次循环
  for(j=len-1;j>=0;j--)
  {
   array[j]=array[j-1];
  }
   array[0]=temp;   
 }
 for(i=0;i<len;i++)
 {
  if(i==len-1)
  printf("%d",array[i]);
  else printf("%d ",array[i]);
 }
 system("pause");
 return 0;
  
}
posted @ 2019-01-23 18:14  拎着红杯子的黄鸭子  Views(120)  Comments(0Edit  收藏  举报