1998 NOIP 拼數 string 和sort的新理解

題目:https://www.luogu.org/problemnew/show/P1012ios

今天真是長了見識。這道題作了十幾分鍾,用模擬愣是調不出來。直到我看了題解——(當場去世)……spa

 

題的意思是n個數拼出一個最大的數,我竟真的傻傻的輸進n個數。。。。。code

用string 輕鬆解決!!!blog

用sort排列,而後直接從1到n輸出,秒AC.....ci

關鍵是關於string的加法性質:ac+cd=abcd;string

因此把cmp寫一下就行了;it

上代碼,一看全明白!!!io

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int n;
string a[50];
bool cmp(string x,string y)
{
    return x+y>y+x;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        cout<<a[i];
    }
    return 0;
}
相關文章
相關標籤/搜索