1038. Recover the Smallest Number (30)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Do not output leading zeros.

Sample Input:
5 32 321 3214 0229 87
Sample Output:

22932132143287

这一题,看上去不难,就是对每个数字段进行排序,不过不能按照简单的字典顺序排序,比如32和3214,应该3214在前面的。所以自己写compare函数时,需要这样写,如果a+b > b+a,这样才交换a和b的位置,否则不变。这样以来排序得到的结果连起来再把前几位的0去掉就可以。

不过一开始我用C写的,就是最后一个case老是过不去,最后一个case就是测试时间复杂度的,按道理前面的都能过最后一个没理由过不了。百度了一下发现有好多人用的方法和我的是一样的,只不过用的是C++写的,于是我改成C++就AC了。实在不懂。。。。

AC代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

bool cmp(const void *a,const void *b)
{
	char *pa,*pb;
	char tmp1[20],tmp2[20];
	pa = (char *)a;
	pb = (char *)b;
	strcpy(tmp1,pa);
	strcat(tmp1,pb);
	strcpy(tmp2,pb);
	strcat(tmp2,pa);

	return strcmp(tmp1,tmp2)<0;
}

int main(int argc, char *argv[])
{
	int i,n;
	char **s;
	char *r;

	cin >> n;
	s = new char *[n];
	for (i = 0; i < n; ++i)
		s[i] = new char[10];
	for (i = 0; i < n; ++i)
		cin >> s[i];

	sort(s,s+n,cmp);
	r = new char[10*n];
	for (i = 0; i < n; ++i)
		strcat(r,s[i]);
	i = 0;
	while(r[i] == '0')
		i++;
	if( !r[i] )
		std::cout << 0 << std::endl;
	else
		std::cout << r+i << std::endl;

	delete []r;
	return 0;
}

下面的是我最后一个case过不了的C代码,几乎和上面的C++代码一样,不知到底是为何,求大神告知

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

#define PRINT_I(x) printf("%s = %d\n",#x,x);
#define PRINT_F(x) printf("%s = %f\n",#x,x);
#define PRINT_S(x) printf("%s = %s\n",#x,x);

int cmp(const void *a,const void *b);

int main(int argc,char* argv[])
{
	int i,n;
	char *result;
	char **seg;

	//freopen("input","r",stdin);
	scanf("%d",&n);

	if( !n )
		return 0;

	seg = (char **)malloc(sizeof(char *)*n);
	for (i = 0; i < n; ++i)
	{
		seg[i] = (char *)malloc(sizeof(char)*10);
	}

	for (i = 0; i < n; ++i)
		scanf("%s",seg[i]);

	qsort(seg,n,sizeof(seg[0]),cmp);

	result = (char *)malloc(10*n);
	for (i = 0; i < n; ++i)
		strcat(result,seg[i]);
	i = 0;
	while(result[i] == '0')
		i++;
	if( !result[i] )
		printf("0\n");
	else
		printf("%s\n", result+i);

	free(result);
	return 0;
}

int cmp(const void *a,const void *b)
{
	char *pa,*pb;
	char tmp1[20],tmp2[20];
	pa = *(char **)a;
	pb = *(char **)b;
	strcpy(tmp1,pa);
	strcat(tmp1,pb);
	strcpy(tmp2,pb);
	strcat(tmp2,pa);

	return strcmp(tmp1,tmp2);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值