• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ZOJ1225_Scramble Sort

            Posted on 2010-10-09 15:38 李東亮 閱讀(550) 評論(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;
            }

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

            Copyright © 李東亮

            久久国产视屏| 中文字幕热久久久久久久| 久久综合九色综合97_久久久 | 久久AAAA片一区二区| 精品久久人人做人人爽综合| 久久综合九色欧美综合狠狠 | 久久99精品国产麻豆不卡| 久久午夜福利电影| 国产∨亚洲V天堂无码久久久| 久久99精品久久久久久水蜜桃| 99久久无色码中文字幕人妻| 99热成人精品免费久久| 国产香蕉久久精品综合网| 久久免费高清视频| 久久婷婷激情综合色综合俺也去| 91久久精品电影| 精品久久久噜噜噜久久久 | 深夜久久AAAAA级毛片免费看 | 亚洲综合久久久| 亚洲国产成人久久综合一| 亚洲国产精品无码久久98| 久久久久噜噜噜亚洲熟女综合| 国产成人久久精品激情| 国内精品人妻无码久久久影院导航| 狠狠久久综合伊人不卡| 亚洲国产成人久久综合碰碰动漫3d| 久久亚洲AV成人出白浆无码国产| 伊人久久大香线蕉综合网站| 日韩精品无码久久一区二区三| 一本久久久久久久| 久久99久久99小草精品免视看| 伊人久久大香线蕉av不变影院| 一级A毛片免费观看久久精品| 无码人妻久久一区二区三区蜜桃| 久久精品国产亚洲综合色| 久久精品一区二区三区不卡| 欧美一区二区三区久久综| 久久人人爽人人爽人人AV东京热| 日产精品久久久久久久性色| 色欲综合久久躁天天躁蜜桃| 久久精品人成免费|