数字游戏(东华大学OJ练习题)

问题描述 :

现在,有许多给小孩子玩的数字游戏,这些游戏玩起来简单,但要创造一个就不是那么容易的了。 在这,我们将介绍一种有趣的游戏。

你将会得到N个正整数,你可以将一个整数接在另一个整数之后以制造一个更大的整数。 例如,这有4个数字123, 124, 56, 90,他们可以制造下列整数─ 1231245690, 1241235690, 5612312490, 9012312456, 9056124123....等,总共可以组合出24(4!)种数字。 但是,9056124123是最大的那一个。

你可能会想这是个简单的事情,但对刚有数字概念小孩来说,这会是个简单的任务吗?

输入说明 :

输入含有多组测试数据。

每组测试资料两行,第一行为一个正整数N(N<= 50),第二行将有N 个正整数。

当N=0代表输入结束。

输出说明 :

对每一组测试数据,输出一行,输出利用这N个整数可结合成的最大整数。

我的思路: 从题中要求输出组合中最大的一个数让我想到的是先排序,把各个整数排好序后输出就好了,排序不是比大小的排序,而是比较两个数组合起来的大小,如'991'和'99'  不能判断991比99大就把他放前面了,应该是 '99991'>'99199',排序考虑到可能会有操蛋的50个整数一组的数据,我选择使用快速排序,OK,动手吧。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//将两个整数转化位字符串,比较两个字符串组合,若A+B>B+A 返回true
int strComp(int A, int B)
{
	int res1, res2;
	char cA[10], cB[10], res[10];
	itoa(A, cA, 10);
	itoa(B, cB, 10);
	strcpy(res, cA);
	strcat(res, cB);
	res1 = atoi(res);

	strcpy(res, cB);
	strcat(res, cA);
	res2 = atoi(res);

	return res1 >= res2 ? 1 : 0;
}

//快速排序,返回枢轴的位置
int QuickSort(int *A, int low, int high)
{
	//牺牲一个存储空间存放枢轴
	A[0] = A[low];

	while (low < high)
	{
        //比较两个数拼接后的大小
		while (low < high &&  strComp(A[0], A[high]))
		{
			high--;
		}
		A[low] = A[high];

		while (low < high &&  strComp(A[low], A[0]))
		{
			low++;
		}
		A[high] = A[low];
	}
	A[low] = A[0];

	return low;
}

void Sort(int *A, int low, int high)
{
	if (low < high)
	{
		int pr = QuickSort(A, low, high);
		Sort(A, low, pr - 1);
		Sort(A, pr + 1, high);
	}
}

int main() {
	int  i, n, temp_n, arr[51];
	while (scanf("%d", &n) != EOF)
	{
		if (n == 0)
		{
			break;
		}

		if (n <= 50 && n > 0)
		{
			temp_n = n;
			while (temp_n)
			{
				scanf("%d", &arr[temp_n]);
				temp_n--;
			}

			Sort(arr, 1, n);

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

			printf("\n");
		}
	}
	return 0;
}

提交了之后,发现有用例不对,我一想可能会有很大的整数,于是换了long long int ,完事儿后还是有一个错的,忍不住看了一眼,结果有一个测试用例是这样的,差点一口老血吐出来,这尼玛不带这样玩的。

整理了思绪,还是老老实实换成字符串接收输入再处理吧。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char arr[51][100];

//将 arr[x]和arr[y] 拼接后比较
int strComp(int x,int y)
{
	char temp1[100],temp2[100];
	strcpy(temp1,arr[x]);
	strcat(temp1,arr[y]);

	strcpy(temp2, arr[y]);
	strcat(temp2, arr[x]);

	return strcmp(temp1, temp2);
}

//快速排序,返回枢轴的位置
int QuickSort(int low, int high)
{
	//牺牲一个存储空间存放枢轴
	strcpy(arr[0], arr[low]);

	while (low < high)
	{
		while (low < high &&  strComp(0, high) >= 0)
		{
			high--;
		}
		strcpy(arr[low], arr[high]);

		while (low < high &&  strComp(low, 0) >= 0)
		{
			low++;
		}
		strcpy(arr[high], arr[low]);
	}
	strcpy(arr[low], arr[0]);

	return low;
}

void Sort(int low, int high)
{
	if (low < high)
	{
		int pr = QuickSort(low, high);
		Sort(low, pr - 1);
		Sort(pr + 1, high);
	}
}

int main() {
	int  i, n;

	while (scanf("%d", &n) != EOF)
	{
		if (n == 0)
		{
			break;
		}

		if (n <= 50 && n > 0)
		{
			for (i = 1; i <= n; i++)
				scanf("%s", arr[i]);

			Sort(1, n);

			for (i = 1; i <= n; i++)
			{
				printf("%s", arr[i]);
			}

			printf("\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/x15037308498/article/details/114416481