[C++]判断最大字符串
来源:互联网 发布:龙虎榜数据哪里查 编辑:程序博客网 时间:2024/04/30 02:08
判断最大字符串
题目描述如下:
Giving N numbers, try to use them to construct a max number.
For example, there are three numbers 56 54 5, you can make numbers 56545, 56554, 54565, 54556, 55654, 55456. The answer is the max number 56554. It is guranteed that all numbers are positive integers and less than 1000.
There is only two lines in Input.
First line is a integer N which means that there are N positive integers.
Second lines are N positive integers.
Sample Inputs
3
56 54 5
Sample Output
56554
#include <iostream>#include <vector>#include <algorithm>#include <numeric>using namespace std;bool compare_bewteen_strings(string a, string b) { for (int j = 0; j != b.length(); j++) { if (b[j] > a[j]) { return false; } if (b[j] < a[j]) { return true; } } int i = 0; while (b[b.length() - 1] == a[b.length() + i]) { if (b.length() + i == a.length()) { return true; } else { if (b[b.length() - 1] > a[b.length() + i]) { return false; } else if (b[b.length() - 1] < a[b.length() + i]) { return true; } } i++; } if (b[b.length() - 1] > a[b.length()]) { return false; } else { return true; }}bool special_cmp(string a, string b) { if (a.length() > b.length()) { return compare_bewteen_strings(a, b); } if (b.length() > a.length()) { return (!compare_bewteen_strings(b, a)); } if (a.length() == b.length()) { for (int i = 0; i != a.length(); i++) { if (a[i] > b[i]) { return true; } if (a[i] < b[i]) { return false; } } } return true;}int main() { vector<string> test; int n; cin >> n; string temp; for (int i = 0; i != n; i++) { cin >> temp; test.push_back(temp); } sort(test.begin(), test.end(), special_cmp); for (vector<string>::iterator iter = test.begin(); iter != test.end(); iter++) { cout << *iter; } cout << endl; return 0;}
答案用的是相邻两个字符串相加,判断谁加谁更大。(相当精妙。)
#include <iostream>#include <algorithm>#include <string>#include <vector>using std::cin;using std::cout;using std::sort;using std::endl;using std::string;using std::vector;bool cmp(string a, string b) { return a+b > b+a;}int main() { int N; string tmp; vector<string> max; vector<string>::iterator iter; cin >> N; while (N--) { cin >> tmp; max.push_back(tmp); } sort(max.begin(), max.end(), cmp); if (!max.empty()) { for (iter = max.begin(); iter != max.end(); ++iter) { cout << *iter; } cout << endl; } return 0;}
0 0
- [C++]判断最大字符串
- C#->求最大子字符串
- 字符串的最大回文子串判断
- C语言 判断回文字符串
- 【c语言】判断回文字符串
- c语言(判断最大数)
- C语言数组实现最大公共字符串
- 73 对策字符串的最大长度 回文串的判断
- 【C】判断一个字符串是否是整数
- C语言判断字符串是否为数字
- 【C语言】判断回文数及字符串
- Problem C: 判断字符串是否为回文
- Problem C: 判断字符串是否为回文
- Problem C: 判断字符串是否为回文
- Problem C: 判断字符串是否为回文
- 问题 C: 判断字符串是否为回文
- 【C语言】判断字符串是否是回文
- 回文字符串C语言判断函数
- 项目布局
- linux shell 字符串操作(长度,查找,替换)详解
- No result defined for action action.LoginAction and result success
- IntelliJ IDEA spring mvc +mybatis 环境搭建服务器(下)
- 搜索引擎原理简介和简单实现
- [C++]判断最大字符串
- Android------startActivityForResult的详细用法
- android的编码习惯
- 为什么找不到js文件?
- 背包入门
- IOS开发之改变UIWebView文本字体的样式和大小
- 基于 Openstack 的 Tempest 测试框架的原理与实践
- ExpandableListView控件的使用
- OSX下虚拟域名问题(laravel)