HDOJ 1106 排序

时间:2015-02-01 13:32:28   收藏:0   阅读:183

【题意】:直接求解就行。注意这个测试用例 15555555555552。

WA一次(缺少temp为0的判断):

            if (!strlen(temp))
                continue;

【代码:AC】

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX 1000+10

int toInt(char str[])
{
    return atoi(str);
}

int main()
{
    char str[MAX];
    while (cin >> str)
    {
        int i = 0, cnt = 0;
        int num[MAX];
        char temp[MAX];
        for (i = 0; i < strlen(str); i++)
        {
            int j = 0;
            while (true)
            {
                if ('5' == str[i] || '\0' == str[i])
                    break;
                temp[j++] = str[i++];
            }
            temp[j] = '\0';
            if (!strlen(temp))
                continue;
            num[cnt++] = toInt(temp);
        }
        sort(num, num+cnt);
        for (i = 0; i < cnt; i++)
        {
            cout << num[i];
            if (i != cnt-1)
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!