把数组排成最小的数
来源:互联网 发布:苹果一键刷机软件 编辑:程序博客网 时间:2024/06/08 19:56
/*输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这3个数字能排成的最小数字321323*/#include "iostream"#include "sstream"#include "vector"#include "algorithm"using namespace std;//实质:确定一种排序规则,哪个数字排前面。bool compare(const string &str_n, const string &str_m){string nm = str_n + str_m;string mn = str_m + str_n;if (nm < mn)return true;elsereturn false;}vector<string> convert2string(int a[], int n){vector<string> strArr(n);for (int i = 0; i < n; i++){stringstream tmp;tmp << a[i];string str;tmp >> str;strArr[i] = str;}return strArr;}void printMinCombine(int a[], int n){if (a == NULL || n <= 0)return;vector<string> strArr = convert2string(a, n);sort(strArr.begin(), strArr.end(), compare);string result;for (auto &r : strArr)result += r;cout << result << endl;}void test(){int a1[] = {1};//int a1[] = { 1, 21, 113 };//int a1[] = { 3, 32, 321 };printMinCombine(a1, sizeof(a1)/sizeof(int));}int main(){test();return 0;}这道题太棒了!
0 0
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数--总结
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- Phaser.js物理引擎篇
- Java BufferedReader读取UTF-8文件中文乱码
- iOS常用公共方法
- Quartz的Misfire处理规则
- 学着写LUA脚本(二)
- 把数组排成最小的数
- iOS之网络篇
- lockf()函数
- JAVA学习(10) 继承(3) 继承中的构造函数
- 通讯录
- 如何正确使用const,static,extern
- Python 基础学习7 - print line
- android studio 真机运行的时候gradle 报错 AGPBI: {"kind":"SIMPLE","text":"Error: C
- iOS9通讯录框架Contacts的简单使用