数据结构练习(30)把数组排成最小的数
2012-12-17 22:44
288 查看
http://zhedahht.blog.163.com/blog/static/25411174200952174133707/
思路:
首先想到的应该是要把数字排序,经过简单的分析即可以得出如何自己定制compare函数。
作者博客里面的compare力求精简而忽略了效率,如果自己写的话应该能快不少。
思路:
首先想到的应该是要把数字排序,经过简单的分析即可以得出如何自己定制compare函数。
作者博客里面的compare力求精简而忽略了效率,如果自己写的话应该能快不少。
#include <cstdio> #include <cstring> #include <cstdlib> const int MAXN = 10; int mycompare(const void* s1, const void* s2) { char combine1[MAXN+1], combine2[MAXN+1]; strcpy(combine1, *(const char**)s1); strcat(combine1, *(const char**)s2); strcpy(combine2, *(const char**)s2); strcat(combine2, *(const char**)s1); return strcmp(combine1, combine2); } void PrintMinNumber(int n[], int len) { if (n == nullptr || len <= 0) return ; char** szn = (char**)(new int[len]); for (int i = 0; i < len; ++i) { szn[i] = new char[MAXN]; sprintf(szn[i], "%d", n[i]); } qsort(szn, len, sizeof(char*), mycompare); for (int i = 0; i < len; ++i) printf("%s", szn[i]); printf("\n"); for (int i = 0; i < len; ++i) delete[] szn[i]; delete[] szn; } int main() { int n[10] = {32, 321}; PrintMinNumber(n, 2); return 0; }
相关文章推荐
- 剑指offer - 面试题30 找出数组最小的k个数
- 个人练习数据结构之--------------关于线性数据的有序数组以及之上的二分法查找、不同排序方法的学习
- GO基本数据结构练习:数组,切片,映射
- 数据结构专项练习(数组、链表、二叉树)
- 数据结构练习(22)调整数组顺序使奇数位于偶数前面
- PAT 数据结构 06-图6. 公路村村通(30)Prim最小生成树算法
- 剑指offer - 面试题30 找出数组最小的k个数
- 【练习笔记】剑指offer-面试题8 :旋转数组的最小数字
- 练习5: 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 剑指Offer编程练习006——旋转数组中的最小数字
- 剑指offer-算法题练习:part6 旋转数组的最小数字
- 数据结构练习 3/30
- C#小练习(输入10个数存入数组中,求最大值、最小值和平均值.)
- 练习5: 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- PAT 1038. Recover the Smallest Number (30)(求俩个数组和,谁放在前面这个数最小,题目是一个数列的组合)
- 堆的数据结构能够使得堆顶总是维持最大(对于大根堆)或最小(对于小根堆),给定一个数组,对这个数组进行建堆,则平均复杂度是多少?如果只是用堆的 push 操作,则一个大根堆依次输入 3,7,2,4,1,5,8 后,得到的堆的结构示意图是下述图表中的哪个?
- 数据结构---数组操作练习
- 【算法30】从数组中选择k组长度为m的子数组,要求其和最小
- 练习5: 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 微软等数据结构+算法面试100题(40)-- 旋转数组中的最小元素