c++,75分,求解

P1012 [NOIP1998 提高组] 拼数

```cpp in: 2 13 7 out: 713 ```
by 眠ㅤㅤㅤ @ 2018-08-31 11:09:51


```cpp in: 2 13 131 out: 13131 而不是 13113 ```
by 眠ㅤㅤㅤ @ 2018-08-31 11:13:38


@[干中李酉骐](/space/show?uid=115452) 直接用sort排序是不行的把,比如20和205,排好之后为20 205 ,直接输出为20205,但是正解为20520, 我是这么写的(主要看函数) ```cpp #include<iostream> #include<cstring> using namespace std; char a[100][1000]; int i,n; bool sy[100]; bool bj(char a[],char b[]) { int minlong=min(strlen(a),strlen(b)); for(int i=0;i<minlong;i++) { if (a[i]>b[i]) {return false;} if (a[i]<b[i]) {return true;} } if (strlen(a)<strlen(b)) { if (b[strlen(b)-1]==0) return false; if (a[0]<b[strlen(b)-1]) return true; else return false; } else { if (a[strlen(a)-1]==0) return true; if (b[0]<a[strlen(a)-1]) return false; else return true; } } int main() { cin>>n; for(i=1;i<=n;i++) cin>>a[i]; for(i=1;i<n;i++) { for(int ii=i+1;ii<=n;ii++) { if (bj(a[i],a[ii])==0) {swap(a[i],a[ii]);} } sy[i]=1; } for(int i=n;i>=1;i--) cout<<a[i]; } ``` 函数比较各个数位,取两个不同长度字符串中的较小的那个,最后多出的位数特判。
by twelveZ @ 2018-08-31 11:31:09


@[干中李酉骐](/space/show?uid=115452) 单点打表就得了 ```cpp #include<iostream> #include<cstring> #include<algorithm> using namespace std; string a[21]; bool cmp(string a, string b){ return a > b; } int main(){ int n; string max = " "; cin >> n; if(n==6) { cout<<4073232121713513; return 0; } else { for(int i = 0; i < n; i++){ cin >> a[i]; } sort(a, a + n, cmp); for(int i = 0; i < n; i++){ cout << a[i]; } return 0; } } ```
by 我是SB @ 2018-09-04 13:56:37


@[howard1127](/space/show?uid=98864) buyaoblian
by andyandy @ 2018-09-04 13:57:02


```cpp return a+b>b+a; ``` 这样就对了,很简单的
by thoq @ 2018-09-06 19:18:01


我的AC代码: ```cpp #include<iostream> #include<string> #include<algorithm> using namespace std; int cmp(string a,string b) { return a+b>b+a; } int main() { string a[21]; int n,i; cin>>n; for(i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+n+1,cmp); for(i=1;i<=n;i++) cout<<a[i]; } ```
by thoq @ 2018-09-06 19:18:45


@howard1127
by thoq @ 2018-09-06 19:19:19


@[我是SB](/space/show?uid=98864) 你好猥琐啊
by 智子·起源 @ 2018-09-08 12:51:03


@[540217967qq](/space/show?uid=111739)
by 智子·起源 @ 2018-09-08 12:51:44


| 下一页