您的位置:首页 > 编程语言 > Go语言

Google 2014校园招聘9月23日笔试题:Sorting

2013-09-24 16:29 387 查看
题目描述:

Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges if they can put their books together. Thus he
has bought an one-row bookshelf for them today and put all his sons' books on it in random order. He labeled each position of the bookshelf the owner of the corresponding book ('Alex' or 'Bob').

Unfortunately, Alex and Bob went outside and didn't know what their father did. When they were back, they came to realize the problem: they usually arranged their books in their own orders, but the books seem to be in a great mess on the bookshelf now. They
have to sort them right now!!

Each book has its own worth, which is represented by an integer. Books with odd values of worth belong to Alex and the books with even values of worth belong to Bob. Alex has a habit of sorting his books from the left to the right in an increasing order of
worths, while Bob prefers to sort his books from the left to the right in a decreasing order of worths.

At the same time, they do not want to change the positions of the labels, so that after they have finished sorting the books according their rules, each book's owner's name should match with the label in its position.

Here comes the problem. A sequence of N values s0, s1, ..., sN-1 is given, which indicates the worths of the books from the left to the right on the bookshelf currently. Please help the brothers to find out the sequence of worths after sorting such that it
satisfies the above description.

Input

The first line of input contains a single integer T, the number of test cases. Each test case starts with a line containing an integer N, the number of books on the bookshelf. The next line contains N integers separated by spaces, representing s0, s1, ...,
sN-1, which are the worths of the books.

Output

For each test case, output one line containing "Case #X: ", followed by t0, t1, ..., tN-1 in order, and separated by spaces. X is the test case number (starting from 1) and t0, t1, ..., tN-1 forms the resulting sequence of worths of the books from the left
to the right.

Limits

1 ≤ T ≤ 30.

Small dataset

1 ≤ N ≤ 100

-100 ≤ si ≤ 100

Large dataset

1 ≤ N ≤ 1000

-1000 ≤ si ≤ 1000

Sample

Input



Output



2

5

5 2 4 3 1

7

-5 -12 87 2 88 20 11

Case #1: 1 4 2 3 5

Case #2: -5 88 11 20 2 -12 87

解析:

对于这题,我们只可以任何排序算法,不同的是我们在排序的过程中,需要根据元素的类型来判断它们是否为相邻的元素。此处用快速排序即可,只需要对快速排序稍加进行修改即可。具体参见如下代码:

#include <iostream>
using namespace std;

enum Type{EVEN, ODD};

inline int lessThan(int a, int b)
{
	if(a < b) return 1;
	if(a == b) return 0;
	return -1;
}

inline int greaterThan(int a, int b)
{
	if(a > b) return 1;
	if(a == b) return 0;
	return -1;
}

inline bool isTargetType(int x, Type t)
{
	if(t == EVEN) return (x&1) == 0;
	return (x&1) != 0;
}

void sort(int *A, int low, int high, Type t, int comp(int, int))
{
	if(A == NULL || low >= high) return;
	int i = low;
	while(!isTargetType(A[i], t)) i++;
	low = i;
	int pivotKey = A[low];
	int j = high;

	while(j > i)
	{
		while(j > i && (!isTargetType(A[j], t) || comp(pivotKey, A[j]) >= 0)) j--;
		A[i] = A[j];
		while(i < j && (!isTargetType(A[i], t) || comp(A[i], pivotKey) >= 0)) i++;
		A[j] = A[i];
	}
	A[i] = pivotKey;
	sort(A, low, i-1, t, comp);
	sort(A, i+1, high, t, comp);
}

void printArray(int *A, int n)
{
	for(int i = 0; i < n; i++)
	{
		cout << A[i] << " ";
	}
	cout << endl;
}

int main()
{
	int A[100];
	int n  = 0;
	while(cin >> n && n > 0 && n <= 100)
	{
		for(int i = 0; i < n; i++) cin >> A[i];
		sort(A, 0, n-1, EVEN, greaterThan);	//降序排列偶数
		sort(A, 0, n-1, ODD, lessThan);	//升序排列奇数
		printArray(A, n);
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: