45はOffer--顔の質問に勝つ:アレイの最小数は、配置します

45件の顔質問:アレイの最小数は配置
タイトル:正の整数配列を入力し、すべての数字の配列は、スプライスはすべての数字最小のものを印刷することができ、一緒にスプライス数、に配置されています。3入力配列は{例えば、32、321}は、3桁の最小数は321323に配置することができる印刷します。

#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
const int g_MaxNumberLength=10;
char* g_StrCombine1=new char[g_MaxNumberLength*2+1];
char* g_StrCombine2=new char[g_MaxNumberLength*2+1];
int compare(const void* strNumber1, const void* strNumber2) {
	strcpy(g_StrCombine1, *(const char**)strNumber1);
	strcat(g_StrCombine1, *(const char**)strNumber2);

	strcpy(g_StrCombine2, *(const char**)strNumber2);
	strcat(g_StrCombine2, *(const char**)strNumber1);
	return strcmp(g_StrCombine1, g_StrCombine2);
}
void PrintMinNumber(int* numbers, int length) {
	if(numbers==NULL || length<=0) return ;
	char** strNumbers=(char**)(new int[length]);
	for(int i=0; i<length; i++) {
		strNumbers[i]=new char[g_MaxNumberLength+1];
		sprintf(strNumbers[i], "%d", numbers[i]);
	}
	qsort(strNumbers, length, sizeof(char*), compare);
	for(int i=0; i<length; i++) {
		printf("%s", strNumbers[i]);
	}
	printf("\n");
	for(int i=0; i<length; i++) {
		delete[] strNumbers[i];
	}
	delete[] strNumbers;
}
int main() {
	int number[]= {3, 32, 321};
	PrintMinNumber(number, 3);
	return 0;
}
公開された48元の記事 ウォン称賛48 ビュー1594

おすすめ

転載: blog.csdn.net/qq_35340189/article/details/104446596