ZOJ1225_Scramble Sort

Posted on 2010-10-09 15:38 李东亮 阅读(535) 评论(0)  编辑 收藏 引用

该题在ZOJ上的题号是1225

题目描述如下:

Background
In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.
Input
The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single
period.


Output

For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.


Sample Input

0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.
Sample Output

0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, wor

         本题不是难题,根据题意,只需把输入中的数字和字符串分开,然后分别按照相应的规则进行排序,并记录下第i个是数字或者是字符,最后按照记录情况一次输出相应的元素即可。因为需要对字符串数组进行排序,因此第一印象是使用C++stringSTL中的sort函数,但是结果却因为懒得写一个排序函数多写了很多代码。

具体代码如下:

#include <iostream>
#include 
<cstdio>
#include 
<cstdlib>
#include 
<string>
#include 
<cstring>
#include 
<algorithm>
#include 
<cctype>

using namespace std;

string s[80];
int a[80];
bool flag[80];
char buf[80];
bool cmp(string a, string b)
{
    
string tmpa = a;
    
string tmpb = b;
    transform(a.begin(),a.end(), tmpa.begin(), ::tolower);
    transform(b.begin(),b.end(), tmpb.begin(), ::tolower);
    
return tmpa < tmpb;
}
int main(void)
{
    
int alpha, num;
    
string t;
    
char *p;
    
int i, j;
    
int sign;
    
int tmp;
    
int k;
    
int m, n;
    freopen(
"in.txt""r", stdin);
    
while (fgets(buf, 80, stdin) != NULL && buf[0!= '.')
    {
        p 
= buf;
        i 
= j = 0;
        alpha 
= num = 0;
        k 
= 0;
        
while (*!= '\n' && *!= '.')
        {
            
while (*== ' ')
                
++p;
            sign 
= 1;
            
if (*== '-' || isdigit(*p) || *== '+')
            {
                
if (*== '-')
                {
                    sign 
= -1;
                    
++p;
                }    
                
else if (*== '+')
                    
++p;
                tmp 
= 0;
                
while (*!= ',' && *!= '.')
                {
                    tmp 
= tmp*10 + (*p-'0');
                    
++p;
                }
                a[num
++= tmp*sign;
                flag[k
++= false;
            }    
            
else
            {
                i 
= 0;
                t 
= "";
                
while (*!= ',' && *!= '.')
                {
                    t 
+= *p;
                    
++p;
                    
++i;
                }
                s[alpha
++= t;
                flag[k
++= true;
            }
            
++p;
        }
        sort(a, a
+num);
        sort(s, s
+alpha, cmp);
        m 
= n  = 0;
        
if (!flag[0])
        {
            printf(
"%d", a[0]);
            
++m;
        }
        
else
        {
            printf(
"%s", s[0].c_str());
            
++n;
        }
        
for (i = 1; i < k; ++i)
        {
            
if (!flag[i])
            {
                printf(
", %d", a[m]);
                
++m;
            }
            
else
            {
                printf(
", %s", s[n].c_str());
                
++n;
            }
        }
        printf(
".\n");
    }
    
return 0;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


posts - 12, comments - 1, trackbacks - 0, articles - 1

Copyright © 李东亮