网易有道内推笔试题

1. 洗牌

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description:
洗牌在生活中十分常见,现在需要写一个程序模拟洗牌的过程。
现在需要洗2n张牌,从上到下依次是第1张,第2张,第3张一直到第2n张。首先,我们把这2n张牌分成两堆,
左手拿着第1张到第n张(上半堆),右手拿着第n+1张到第2n张(下半堆)。接着就开始洗牌的过程,先放下右手的
最后一张牌,再放下左手的最后一张牌,接着放下右手的倒数第二张牌,再放下左手的倒数第二张牌,直到最后放下
左手的第一张牌。接着把牌合并起来就可以了。
例如有6张牌,最开始牌的序列是1,2,3,4,5,6。首先分成两组,左手拿着1,2,3;右手拿着4,5,6。在洗牌过程中按
顺序放下了6,3,5,2,4,1。把这六张牌再次合成一组牌之后,我们按照从上往下的顺序看这组牌,就变成了序列
1,4,2,5,3,6。
现在给出一个原始牌组,请输出这副牌洗牌k次之后从上往下的序列。

输入
第一行一个数T(T<=100),表示数据组数。对于每组数据,第一行两个数n,k(1<=n,k<=100),接下来一行有2n个数
a1,a2,...,a2n(1<=ai<=1000000000)。表示原始牌组从上到下的序列。

输出
对于每组数据,输出一行,最终的序列。数字之间用空格隔开,不要在行末输出多余的空格。

样例输入
3
3 1
1 2 3 4 5 6
3 2
1 2 3 4 5 6
2 2
1 1 1 1
样例输出
1 4 2 5 3 6
1 5 4 3 2 6
1 1 1 1

2. 构造队列

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description:
小明同学把1到n这n个数字按照一定的顺序放入了一个队列Q中。现在他对队列Q执行了如下程序:
while(!Q.empty())              //队列不空,执行循环
{
int x=Q.front();            //取出当前队头的值x
Q.pop();                 //弹出当前队头
Q.push(x);               //把x放入队尾
x=Q.front();              //取出这时候队头的值
printf("%d\n",x);          //输出x
Q.pop();                 //弹出这时候的队头
}
做取出队头的值操作的时候,并不弹出当前队头。
小明同学发现,这段程序恰好按顺序输出了1,2,3,...,n。现在小明想让你构造出原始的队列,你能做到吗?
输入
第一行一个整数T(T<=100)表示数据组数,每组数据输入一个数n(1<=n<=100000),输入的所有n之和不超过200000。
输出
对于每组数据,输出一行,表示原始的队列。数字之间用一个空格隔开,不要在行末输出多余的空格。

样例输入
4
1
2
3
10
样例输出
1
2 1
2 1 3
8 1 6 2 10 3 7 4 9 5
3.查找矩形
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description:
对于一组水平线段及垂直线段,共有n条,已知其中必定存在一个若干条线段组成的矩形,输出这个矩形的左下角和右上角点,
定义左下角点有最小的x值和y值,右上角点有最大的x值和y值。
线段可以重合,线段必须恰好能组成矩形,即线段不能头或尾部超出矩阵边界,如下图:

输入
第一行为线段数 n (4 <= n <= 25)
接下来有n行,每行为一条线段的首尾点坐标: xi yi xj yj
坐标值范围 -1e9 <= x, y <= 1e9
输出
在一行输出矩形的左下角点和右上角点坐标:
xi yi xj yj 
根据定义有 xi < xj && yi < yj

样例输入
4
0 0 0 1
0 0 1 0
0 1 1 1
1 0 1 1 
样例输出
​0 0 1 1
1:
import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNextInt())
		{
			int num = scanner.nextInt();
			for(int i=0;i<num;i++)
			{
				int n=scanner.nextInt();
				int k=scanner.nextInt();
				int[] add = new int[2*n];
				int[] arry_1 = new int[n];
				int[] arry_2 = new int[n];
				for(int j=0;j<2*n;j++)
				{
					if(j<n)
					{
						arry_1[j]=scanner.nextInt();
					}
					else
					{
						arry_2[j-n]=scanner.nextInt();
					}
				}
				add=comput1(arry_1,arry_2,k,n);
				for(int h=0;h<add.length;h++)
				{
					if(h==add.length-1)
						System.out.print(add[h]);
					else
						System.out.print(add[h]+" ");
				}
				System.out.println();
			}	
		}	
	}
	public static int[] comput1(int[] arry_1,int[] arry_2,int k,int n)
	{
		int[] add = new int[2*n];
		for(int i=0;i<k;i++)
		{
			add=comput(arry_1,arry_2,n);
			for(int j=0;j<2*n;j++)
			{
				if(j<n)
				{
					arry_1[j]=add[j];
				}
				else
				{
					arry_2[j-n]=add[j];
				}
			}
		}
		return add;
	}
	public static int[] comput(int[] arry_1,int[] arry_2,int n)
	{
		int[] add = new int[2*n];
		int[] add1= new int[2*n];
		int i=0;
		for(int w=n;w>0;w--)
		{
			add[i++]=arry_2[w-1];
			add[i++]=arry_1[w-1];	
		}
		for(int h=0;h<2*n;h++)
		{
			add1[2*n-h-1]=add[h];
		}
		return add1;
	}
}
2:
import java.util.*;
public class Main {
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int k=sc.nextInt();
			for(int i=0;i<k;i++)
			{
				int w = sc.nextInt();
				comput_1(w);
			}
		}
	}
	public static void comput_1(int n)
	{
		Queue<Integer> queue = new LinkedList<Integer>();
		for(int i=n;i>=1;i--)
		{
			queue.offer(i);
			int k = queue.peek();
			queue.poll();
			queue.offer(k);
		}
		int arry[] = new int[queue.size()];
		for(int i=0;i<arry.length;i++)
		{
			arry[i]=queue.poll();
		}
		for(int i=0;i<arry.length;i++)
		{
			if(i==arry.length-1)
				System.out.print(arry[arry.length-i-1]);
			else
				System.out.print(arry[arry.length-i-1]+" ");
		}
	}
}
3:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值