【排序】【字符串】数字积木

问题描述

小明有一款新式积木,每个积木上都有一个数,一天小明突发奇想,要是
把所有的积木排成一排,所形成的数目最大是多少呢?
你的任务就是读入 n 个数字积木,求出所能形成的最大数。


数据输入

第一行是一个整数 n(n<=1000),接下来 n 行每行是一个正整数。


数据输出

所能形成的最大整数

输入输出样例

brick.in
3
13
131
343

brick.out
34313131


数据范围

30%的数据,n<=10,每个数<10^3
50%的数据,n<=100
100% 的数据,n<=200,每个数<10^200


思路:

这题就是一道字符串排序,贪心能拿30分


C o d e Code Code:

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int n,t;
string str[10101];
bool cmp (string x,string y)//排序
{
    
    
	  string bc,bs;
      bc = x + y;//两串字符串看哪个值大
      bs = y + x;
        if(bc > bs)return 1;
        return 0; 
}
int main ()
{
    
    
	freopen ("brick.in","r",stdin);
	freopen ("brick.out","w",stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	  cin>>str[i];
	  for (int i = 1; i <= n ;++i)
	    for (int j = i + 1; j <= n ; ++j)
	    {
    
    
	    	if (! cmp(str[i], str[j]))
	    	swap(str[i], str[j]);//交换
	    }
	    for (int i = 1; i <= n ;++i)
	    cout << str[i];//输出
     	return 0;
} 

猜你喜欢

转载自blog.csdn.net/hunkwu/article/details/108168808