游客 Signup | Login
中文 | En

3571 - 9.cpp

编写程序,程序的功能是移动一维数组a中的内容,首先对一维数组赋值,再从键盘输入一个整数n,要求把下标从0n(n本身,n应小于数组元素的总和)的数组元素平移到数组的最后。

提示 将t=a[0],然后a[2]…a[n-1]依次向前移动,然后a[n-1]=t,重复n+1

定义数组大小为10


Input

首先输入一个整数n,表示数组下标n

1行输入10个整数。每个整数用1个空格分隔

Output

平移后数组中的元素,每个元素用1个空格分隔。

注意最后1个数据后不需要输出空格

Examples

Input

3
1 2 3 4 5 6 7 8 9 10
4
9 8 7 6 5 4 3 2 1 10

Output

5 6 7 8 9 10 1 2 3 4
4 3 2 1 10 9 8 7 6 5

Solution C

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define N 10

int main()
{
    //freopen("in","r",stdin);
  //  freopen("out","w",stdout);

   int arr[N];
	int n=0;
	int i=0,j=0;
while(scanf("%d",&n)!=EOF)
{
	for(i=0; i<N; i++)
	{
		scanf("%d",&arr[i]);
	}

	if(n>=N)
		continue ;

	for(j=0; j<=n; j++)
	{
		int tmp = arr[0];
		for(i=0; i<N-1; i++)
		{
			arr[i] = arr[i+1];
		}
		arr[N-1] = tmp;
	}

	for(i=0; i<N-1; i++)
	{
		printf("%d ",arr[i]);
	}
	printf("%d\n",arr[i]);
}


  return 0;
}

Solution C++

#include<stdio.h>
#define N 10
main()
{
	int a[N];
	int n,t,i,j,l,k;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<10;i++)
		{
			scanf("%d",&a[i]);
		}
		for(j=1;j<=n+1;j++)
		{
			t=a[0];
			for(l=1;l<=9;l++)
			{
				a[l-1]=a[l];
			}
			a[9]=t;
		}
		for(k=0;k<=8;k++)
		{
			printf("%d ",a[k]);
		}
		printf("%d\n",a[9]);
	}
}

Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题