把数组排成最小的数
来源:互联网 发布:sql查询语句表别名 编辑:程序博客网 时间:2024/06/05 12:57
代码:
#define _CRT_SECURE_NO_WARNINGS#include <iostream>using namespace std;const int g_max_length = 10; // 0~9char* g_str_combine1 = new char[2 * g_max_length + 1];char* g_str_combine2 = new char[2 * g_max_length + 1];int Compare(const void* strNumber1, const void* strNumber2) //比较大小{ strcpy(g_str_combine1, *(char**)strNumber1); strcat(g_str_combine1, *(char**)strNumber2); strcpy(g_str_combine2, *(char**)strNumber2); strcat(g_str_combine2, *(char**)strNumber1); return strcmp(g_str_combine1,g_str_combine2);}void FindMinNumberSequenceOfArray(int* number, int nLength){ if (number == nullptr || nLength <= 0) return; char** strNumbers = (char**)(new int[nLength]); for (int i = 0; i < nLength; i++) { strNumbers[i] = new char[g_max_length + 1]; /* sprintf(char*buffer, const char *format, [argument]...) 把格式化的数据写入某个字符缓冲区*/ sprintf(strNumbers[i], "%d", number[i]); } qsort(strNumbers, nLength, sizeof(char*), Compare); for (int i = 0; i < nLength; i++) { cout << strNumbers[i]; } cout << endl; for (int i = 0; i < nLength; i++) { delete[] strNumbers[i]; } delete[] strNumbers;}int main(){ int number[] = { 3, 32, 321 }; int nLength = sizeof(number) / sizeof(number[0]); FindMinNumberSequenceOfArray(number, nLength); cout << endl; system("pause"); return 0;}
测试:
0 0
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数--总结
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- apt-get 与 yum的区别 (转)
- 经典SQL语句——基础篇
- Hibernate 错误和异常,无法生成hbm.xml、SessionFactory报空....
- DSP优化,通用的调整性能的策略 .
- linux压缩和解压缩
- 把数组排成最小的数
- HDU 4055 Number String
- HBase shell常用命令
- Android笔记之自定义View——绘制圆环比例展示图
- centOS6.5下安装redis扩展
- embed和video的使用
- Android 获取机型设备信息
- 数据结构示例之根据位置和长度删除子字符串
- 惠普奥德赛计划:谁的掘墓人?