组成最大数

标题:组成最大数 | 时间限制:1秒 | 内存限制:65536K | 语言限制:不限
小组中每位都有一张卡片,卡片上是6位内的正整数,将卡片连起来可以组成多种数字,计算组成的最大数字。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void strTok(const string numStr, vector<string> &numArr){
	int pos = 0;
	int cnt = 0;
	for (int i = 0; i < numStr.size(); ++i){
		if (numStr[i] == ','){
			numArr.push_back(numStr.substr(pos, cnt));
			pos = i + 1;
			cnt = 0;
		}
		else{
			cnt += 1;
		}
	}
	numArr.push_back(numStr.substr(pos, cnt));
}
int main(){
	string numStr;
	cin >> numStr;
	vector<string> numArr;
	strTok(numStr, numArr);
	sort(numArr.begin(),numArr.end(),[](string x, string y)
         { 
             return x + y > y + x;
         });
	for (int i = 0; i < numArr.size(); ++i){
		cout << numArr[i];
	}
	return 0;
}
def judgeStr(str1,str2):
    sum1 = str1 + str2
    sum2 = str2 + str1
    if sum1 >= sum2:
        return True
    else:
        return False
def biggestNum(list_str):
    for i in range(len(list_str)):
        for j in range(len(list_str)):
            if j > i:
                if judgeStr(list_str[i],list_str[j]) == False:
                    list_str[i],list_str[j] = list_str[j],list_str[i]
    print(''.join(list_str))

while True:
    try:
        str1 = input().strip()
        list_str = str1.split(',')
        biggestNum(list_str)
    except:
        break

from functools import cmp_to_key


def compare(a, b):
    return 1 if int(a + b) < int(b + a) else -1


while True:
    try:
        num_list = sorted(input().split(","), key=cmp_to_key(compare))
        print("".join(num_list))

    except:
        break
import java.util.Scanner;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.io.BufferedReader;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = null;
          while(sc.hasNext()){
            input = sc.nextLine();
            String[] str = input.split(",");
            List<String> list = Arrays.asList(str);
            list.sort((next, pre) ->next.length() == pre.length() ? pre.compareTo(next) : (pre+next).compareTo(next + pre));
            StringBuffer strBu = new StringBuffer();
                for(String li: list){
                      strBu.append(li);    
                }
            System.out.println(strBu);
        }
    }
}




全部评论

相关推荐

05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务