zoukankan      html  css  js  c++  java
  • 全是套路——字符串排序

    如题:

    题目描述

    编写一个程序,将输入字符串中的字符按如下规则排序。

    规则1:英文字母从A到Z排列,不区分大小写。

          如,输入:Type 输出:epTy

    规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。

        如,输入:BabA 输出:aABb

    规则3:非英文字母的其它字符保持原来的位置。

        如,输入:By?e 输出:Be?y

    样例:

        输入:

       A Famous Saying: Much Ado About Nothing(2012/8).

        输出:

       A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

    直接上代码:

    #include <stdlib.h>
    #include <string>
    #include <string.h>
    #include <iostream>
    #include <vector>
    #include <algorithm> 
    #include <stack>
    
    using namespace std;
    
    int fun(char c)
    {
        int n = 0;
        if (c >= 'a'&&c <= 'z')
        {
            n = c - 'a';
        }
        else if (c >= 'A'&&c <= 'Z')
        {
            n = c - 'A';
        }
        else
        {
            n = -1;
        }
        return n;
    }
    
    int main()
    {
        string a;
        while (getline(cin, a))
        {
            int last = a.size() - 1;
            while (fun(a[last]) == -1 && last >0)
            {
                last--;
            }
            int last1=last-1;
            while (fun(a[last1]) == -1 && last1 >0)
            {
                last1--;
            }
            for (int i = 0; i < last1+1; i++)
            {
                for (int j = 0; j <last1+1; j++)
                {
                    while((fun(a[j]) == -1) && (j <a.size()-1))
                    {
                        j++;
                    }
                    int r = j + 1;
                    while ((fun(a[r]) == -1) && (r <a.size()-1))
                    {
                        r++;
                    }
                    if (fun(a[j])>fun(a[r]))
                    {
                        char tmp = a[j];
                        a[j] = a[r];
                        a[r] = tmp;
                    }
                }
                
            }
            cout << a << endl;
        }
        
    
        return 0;
    }

    这道题的关键其实就是:

    间隔排序!!!!!!

    但是!

    需要用一种稳定排序方式,冒泡和插入可以,选择就不行!

    冒泡的话得注意上限,上限是倒数第二个字母的下标+1.

    因为冒泡是跟下一个数比。

    比如ab012ad0a。

    最后一个交换是d和a比,就是循环到d。也就是倒数第二个字母下标+1.

    下面是我补上的:按照间隔为2来排序。

    思想是:找到倒数第二个元素下标,然后<下标+1

    #include <stdlib.h>
    #include <string>
    #include <string.h>
    #include <iostream>
    #include <vector>
    #include <algorithm> 
    #include <stack>
    
    using namespace std;
    
    
    int main()
    {
        int a[11] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,10 };
        int n = 11;
    
    
        for (int i = 0; i < (n&2==0?n-3:n-2); i++)
        {
            for (int j = 0; j <(n & 2 == 0 ? n - 3 : n - 2); j++)
            {
                if (j % 2 == 0)
                {
                    if (a[j]>a[j + 2])
                    {
                        int tmp = a[j];
                        a[j] = a[j + 2];
                        a[j + 2] = tmp;
                    }
                }
            }
            int jj = 0;
        }
    
        return 0;
    }
  • 相关阅读:
    UVA-1595 Symmetry
    UVA-10763 Foreign Exchange
    剑指Offer
    剑指Offer
    剑指Offer
    剑指Offer
    剑指Offer
    剑指Offer
    剑指Offer
    剑指Offer
  • 原文地址:https://www.cnblogs.com/wyc199288/p/5621707.html
Copyright © 2011-2022 走看看