hdu1106

来源:互联网 发布:笔记本win7找不到网络 编辑:程序博客网 时间:2024/05/16 07:33

排序

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34503    Accepted Submission(s): 9716


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

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

 

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

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。
 

Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。
 

Sample Input
0051231232050775
 

Sample Output
0 77 12312320
解题思路:
题目有两处坑,第一点注意中间是否有两个或多个5连着的,第二点注意最后一个5之后还有没有数了。
另外cal函数感觉写的还不错。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int num[10000010];string s;int cal(string p){    int res = 1;    int result = 0;    for(int i = p.length() - 1 ; i >= 0 ; i --)    {        result += (p[i] - '0') * res;        res *= 10;    }    return result;}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    while(cin >> s)    {        string tmp = "";        int t = 0;        for(int  i = 0 ; i < s.length() ; i ++)        {            if(s[i] == '5')            {                if(tmp != "")                {                    num[t ++] = cal(tmp);                    tmp = "";                }            }            else                tmp += s[i];        }        if(tmp != "")            num[t ++] = cal(tmp);        sort(num , num + t);        for(int i = 0 ; i < t ; i ++)        {            printf("%d%c", num[i] , i == t - 1 ? '\n' : ' ');        }    }}


0 0
原创粉丝点击