• 面试题 33 把数组排成最小的数


    const int g_maxlen = 10;
    char * g_strCombine1 = new char[g_maxlen*2 +1];
    char * g_strCombine2 = new char[g_maxlen*2 +1];
    int compare(const void *str1, const void *str2)
    {
    	strcpy(g_strCombine1, *(const char **)str1);
    	strcat(g_strCombine1, *(const char **)str2);// important
    
    	strcpy(g_strCombine2, *(const char **)str2);
    	strcat(g_strCombine2, *(const char **)str1);
    
    	return strcmp(g_strCombine1, g_strCombine2);
    }
    void printMinNum(int number[], int len)
    {
    	if(number == NULL || len < 1) return ;
    
    	char **strNum = (char **)(new int[len]) ;// 指针的大小是int的大小
    
    	for(int i = 0; i< len ; i++)
    	{
    		strNum[i] = new char[g_maxlen +1];
    		sprintf(strNum[i], "%d", number[i]); // important
    	}
    
    	qsort(strNum,len, sizeof(char*), compare);// important
    
    	for(int i = 0; i < len; ++i)
    		printf("%s", strNum[i]);
    	for(int i = 0; i< len; ++i)
    		delete [] strNum[i];
    	delete [] strNum;
    }
    

      

  • 相关阅读:
    高精度乘法
    阶乘
    高精度减法
    高精度加法
    曹冲养猪
    采药2
    nginx.conf详解
    系统盘脚本扩容
    IDEA中编写脚本并运行shell脚本
    常用的pdf工具
  • 原文地址:https://www.cnblogs.com/graph/p/3324331.html
Copyright © 2020-2023  润新知