AX: 分割排序(基础上机试题)

输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。
你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。
输入
输入包含多组测试数据。每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。
输入数据保证:分割得到的非负整数不会大于100000000,输入数据不可能全由‘5’组成。
输出
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。
样例输入 Copy
0051231232050775
样例输出 Copy
0 77 12312320

代码示例(自己写的,只能过平台,不完美)

//#include <algorithm>
//#include <iostream>
//using namespace std;
//#include <vector>
//#include <stack>
//#include <queue>
//#include <cmath>
//#include <string>
//#include <string.h>
//
//int main() {
//    int t = 0;
////    cin >> t;
////    getchar();
//    scanf("%d",&t);
//    getchar();//当输入之后接着要输入字符(串)变量时,需要用getchar()吃掉上面输入之后的空格 
//    while(t--){
//        int k = 0;
//        string str;
//        getline(cin,str);
//        for(int i = 0; i < str.size(); i++){
//            if(str[i] == ' ' ){
//                for(int j = i - 1; j >= k; j--){
//                    cout << str[j];
//                }
//                cout << " ";
//                k = i + 1;
//            }    
//        }
//        for(int l = str.size() - 1; l >= k; l--){
//            cout << str[l];
//        }
//        cout << endl;
//    }
//    return 0;
//}

给的标程是这样的,可以参考一下

//#include<cstdio>
//#include<cstring>
//#include<algorithm>
//using namespace std;
//int main()
//{
//    char s[5005];
//    int a[5005];
//    int i,len,k,n;
//    while(scanf("%s",s)!=EOF)
//    {
//        n=0;
//        k=0;
//        len=strlen(s);
//        s[len]='5';   
//        i=0;
//        while(s[i]=='5'){
//            i++;
//        } //跳过前缀5,防止多输出0
//        for(;i<=len;++i)
//        {
//            if(i>0&&s[i]=='5'&&s[i-1]=='5') //忽略连续的5,防止多输出0
//                continue;
//            if(s[i]!='5'){
//                k=k*10+s[i]-'0';
//            } else{
//                a[n]=k;
//                n++;
//                k=0;
//            }
//        }
//        sort(a,a+n);
//        printf("%d",a[0]);
//        for(i=1;i<n;++i)
//            printf(" %d",a[i]);
//        printf("\n");
//    }
//    return 0;
//}

评论

暂无评论