输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个

算法描述:

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个

解决大数问题的方法是吧数字转换成字符串

算法实现:

/*************************************************************************
	> File Name: main.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月21日 星期四 09时56分01秒
 ************************************************************************/

#include "PrintMinNumber.h"

void Test(char *name, int *numbers, int length, char *expected)
{
	if (name != NULL)
		printf("test begin:\n");
	if (expected != NULL)
		printf("expected result is %s\n", expected);
	printf("actual result is :\n");
	PrintMinNumber(numbers, length);
	printf("\n");
}
void Test1()
{
	int numbers[] = {3, 5, 1, 4, 2};
	Test("Test1", numbers, sizeof(numbers)/sizeof(numbers[0]), "12345");
}
int main()
{
	Test1();
	return 0;
}
CC = gcc
CFLAGS = -g -O2 -Wall

%.o:%.c
	$(CC) -o $@ -c $(CFLAGS) $<

main:main.o PrintMinNumber.o
	$(CC) main.o PrintMinNumber.o -o main $(CFLAGS)

clean:
	rm -rf *.o main
/*************************************************************************
	> File Name: PrintMinNumber.h
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月21日 星期四 09时27分34秒
 ************************************************************************/

#ifndef _PRINTMINNUMBER_H
#define _PRINTMINNUMBER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define G_MAXLEN 10
void PrintMinNumber(int *numbers, int length);


#endif

/*************************************************************************
	> File Name: PrintMinNumber.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年04月21日 星期四 09时28分54秒
 ************************************************************************/

#include "PrintMinNumber.h"
char g_strCombine1[G_MAXLEN*2 + 1];
char g_strCombine2[G_MAXLEN*2 + 1];
int compare(const void *strNum1, const void *strNum2)
{
	strcpy(g_strCombine1, *(const char **)strNum1);
	strcat(g_strCombine1, *(const char **)strNum2);
	strcpy(g_strCombine2, *(const char **)strNum2);
	strcat(g_strCombine2, *(const char **)strNum1);

	return strcmp(g_strCombine1, g_strCombine2);
}

/*
 * 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能
 * 拼接出的所有数字中最小的一个
 * */
void PrintMinNumber(int *numbers, int length)
{
	if (numbers == NULL || length < 0)
	{
		return;
	}
	char **strNum = (char **)malloc(length*sizeof(char *));
	int i = 0;
	for (i = 0; i < length; i++)
	{
		strNum[i] = (char *)malloc(sizeof(char));
		sprintf(strNum[i], "%d", numbers[i]);
	}

	qsort(strNum, length, sizeof(char *), compare);

	for (i = 0; i < length; i++)
	{
		printf("%s", strNum[i]);
	}
	printf("\n");

	for (i = 0; i < length; i++)
	{
		if (strNum[i] != NULL)
			free(strNum[i]);
	}

	if (strNum != NULL)
		free(strNum);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值