C - 排序

输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。


Input输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。  

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。
Output对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。
Sample Input
0051231232050775
Sample Output
0 77 12312320

 1 //
 2 // Created by w on 2021-01-14.
 3 //
 4 
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <set>
 8 using namespace std;
 9 int main()
10 {
11     ios::sync_with_stdio(false);//关闭流输入
12     string s1;
13     while (cin>>s1)//多组输入
14     {
15         multiset<int >ms;//可重复集合,自动排序
16         string num;
17         int len=s1.size();
18         for (int i = 0; i < len; )
19         {
20             if(s1[i]!='5')
21             {
22                 num+=s1[i];//把5以外的数字放到一个数组中
23                 i++;
24             } else
25             {
26                 while (s1[i]=='5')
27                     i++;//向后移一个位置
28                 if(!num.empty())
29                     ms.insert(atoi(num.c_str()));//atoi传参数为char *类型,所以要用c_str()将str转换为char型,存放之前不为5的数
30 //                for(auto it=ms.begin();it!=ms.end();it++)
31 //                    cout<<*it<<endl;
32                 num.clear();//清空num,再向里面存不为5的数
33             }
34         }
35         if(!num.empty())
36             ms.insert(atoi(num.c_str()));
37         multiset<int >::iterator it=ms.begin(),end=--ms.end();//end输出最后一位,前缀--优先级高
38         for ( ; it!=end  ; it++)
39             cout<< *it <<' ';
40         cout<< *end <<endl;//保证最后一个没空格
41 
42     }
43     return 0;
44 }

 

posted @ 2021-01-14 21:07  BlackSnow  阅读(121)  评论(0)    收藏  举报