【PAT甲級A1038】Recover the Smallest Number (30分)(c++)
1038 Recover the Smallest Number (30分)
作者:CHEN, Yue
單位:浙江大學
程式碼長度限制:16 KB
時間限制:400 ms
記憶體限制:64 MB
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤104) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
題意:
將所給數字組合成最小數。
思路:
用string儲存每個數,在排列的時候我們只要考慮,字串a和b的組合誰大,及cmp函式中,return a+b<b+a;注意,當最後的組合數為0的時候,也要輸出0,不能輸出空。
參考程式碼:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool cmp(string a,string b){
return a+b<b+a;
}
int main(){
int n;
string sum;
scanf("%d",&n);
vector<string> v(n);
for(int i=0;i<n;i++)
cin>>v[i];
sort(v.begin(),v.end(),cmp);
for(int i=0;i<n;i++)
sum+=v[i];
while(sum[0]=='0')
sum.erase(sum.begin());
if(!sum.empty())cout<<sum<<endl;
else printf("%d\n",0);
return 0;
}
如有錯誤,歡迎指正
相關文章
- PAT甲級1023 Have Fun with Number
- PAT甲級1126~1130|C++實現C++
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級考試題庫題目分類
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- PAT甲級-1005. Spell It Right (20)各位之和
- PAT甲級-1010. Radix (25)進位制
- PAT甲級-1012. The Best Rank (25)並列排序排序
- 2024 秋季PAT認證甲級(題解A1-A4)
- 【PAT乙級、C++】1024 科學計數法 (20分)C++
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- 2021.9.12週六PAT甲級考試覆盤與總結
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- PAT乙 1041 考試座位號 (15分)(C C++)C++
- [PAT]Table Tennis (30)Java實現Java
- PAT 乙級 1094 谷歌的招聘 (20分)---【素數 字串】谷歌字串
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- [LeetCode] 230. Kth Smallest Element in a BSTLeetCode
- 【PAT乙級】1048 數字加密加密
- PAT乙級1004 成績排名 (20分)(C語言版)及解析C語言
- PAT1013數素數C++C++
- Recover database after disk loss (Doc ID 230829.1)Database
- [LeetCode] 305. Number of Islands IILeetCode
- PAT乙級——1093(字串匹配)Java實現字串匹配Java
- PAT乙級比賽-互評成績計算
- 冷備手工完全恢復(recover database,recover tablespace,recover datafile)Database
- [LeetCode] K-th Smallest Prime Fraction 第K小的質分數LeetCodeFraction質分數
- C++ 測試框架 GoogleTest 初學者入門篇 甲C++框架Go
- Recover a Database Having Added a Datafile Since Last Backup (Doc ID 29430.1)DatabaseAST
- Dart: The World's Smallest Laptop AdapterDartAPT
- cron with recover