• [面试编程题]算法基础-字符移位


    错误代码

    #include<iostream>
    #include<string>
    using namespace std;
    bool isLow(char c)
    {
        if( (c>='A') && (c<='Z'))
            return false;
        else 
            return true;
    }
    bool isCap(char c)
    {
        return !isLow(c);    
    }
    
    int main()
    {
        string s;
        while(cin>>s)
        {
            int n=s.size();
            int l=0,r=n-1;
            
            while(l<r)
            {
                while(r>=0 && isCap(s[r])) r--;//r最右边的一个小写字母
                while(l<=n-1 && isLow(s[l])) l++;//l指向最左边的大写字母
                if(l<r)
                    swap(s[l],s[r]);
                else 
                    break;
            }
            cout<<s<<endl;
        }
    }

    测试用例

    测试用例:
    hxKLAGLLzPyTxsFsrUnnSKQBHdQQrOyaEYJRgiJbHIDXFcQkFmIhPNKIBfHxXDBdKAvgZiBLVwnlxJAHmttsSJkZhSmQneNVoKoIYZRjPqsrFFaaqZbyNyeRjVKVFrCGdfycidTqbyQcpAtdRGzzBAaKoqybWMOyhrCQdwcRwQQpQavTnAbjriVwxJOrTYJVGYSWzKYeNAGqBzkJLucabNYvyVFxAGKLfqHXNttaqZfncEdTroGMzZnDbvZBBaRbJvuYIvlWrKaaGrvtyxrsCUOqxdwCrmVEeDrLKZKFJVRmrLsmbmOGUJyfdZIrFhuSwJQGRTYMLxKQNMaCavatlQIRZmFQvyWgQTVENxUcPKQCaUQbjyfaNuwoNdTBNldgrtPUcQodqsuJOdDpUczJWCZaasDdEYJkvituMHrCmZQSlRjIefVisatIUtfxBeKnHPyvWUKzRliFsYWgeXogiEgXDbfxAybwFuqFyEvjfIHEPDPKqEiGUtZhdDIDBGKpvBFyqHeEEhAToAbqHEpIdIhIGBtWjGHiQRctZxQQYkfFoWUbqZyIcjRPQBilHrnqNBzFmoRUYCSrGkawJCcOrMceegISpIpSGVjbngWVMTYtGoAlQFPFyOFAxndJZNfKDTwFIxisKTjyjchidXpYgLfoBOLriuIAHmAbQwoHBgbdUYBHlDQGZJASsHszOEPthLVnYbNqWegmONexfdsTVYHgtDmlyugefOBsqmgNDBoxkkhVHfvrYooVOyxDJQJLjYSngksbTopoPJFsKQzHePLukXyYTYCeW
    对应输出应该为:
    hxzyxssrnndryagibckmhfxdvgiwnlxmttskhmneoojqsraaqbyyejrdfycidqbycptdzzaoqybyhrdwcwpavnbjriwxrzeqzkucabvyxfqttaqfncdroznbvabvuvlraarvtyxrsqxdwrmermrsmbmyfdrhuwxaavatlmvygxcabjyfauwodldgrtcodqsudpczaasdkviturmljefisattfxenyvzlisgeogigbfxybwuqyvjfqithdpvyqehobqpdhtjictxkfobqycjilrnqzmorkawcrceegppjbngtolyxndfwxisjyjchidpgforiumbwogbdlsszthnbqegmexfdsgtmlyugefsqmgoxkkhfvrooyxjngksboposzeukyeKLAGLLPTFUSKQBHQQOEYJRJHIDXFQFIPNKIBHXDBKAZBLVJAHSJZSQNVKIYZRPFFZNRVKVFCGTQARGBAKWMOCQRQQQTAVJOTYJVGYSWKYNAGBJLNYVFAGKLHXNZETGMZDZBBRJYIWKGCUOCVEDLKZKFJVRLOGUJZIFSJQGRTYMLKQNMCQIRZFQWQTVENUPKQCUQNNTBNPUQJODUJWCZDEYJMHCZQSRIVIUBKHPWUKRFYWXEXDAFFEIHEPDPKEGUZDIDBGKBFHEEATAHEIIIGBWGHQRZQQYFWUZIRPQBHNBFRUYCSGJCOMISISGVWVMTYGAQFPFOFAJZNKDTFIKTXYLBOLIAHAQHBUYBHDQGZJASHOEPLVYNWONTVYHDOBNDBVHYVODJQJLYSTPJFKQHPLXYTYCW
    你的输出为:
    hxeykuezzsyoxspsronnbskgndjxryyaoorvgifbhkkxocgkmmqhsfegufyxlmtdgsvgdifxewnlxmgemttsqbknhhmtnezsosoldbgjoqsrwbaaqmbyuyeijrofgrpddfycidiqbyhcpctdjyzzjsaioqybxwfyhrdndwcxwylpoavtngbjrinwxbjrppgeecrczwaekroqmzkzqucabnrvylixjcyqfqbofttaqkfncxdtrocizjntbvhdpaqbbvuohvlerqaayrvtyxrsvpdqxdwhrmtieqrfjvyquwbmrysmbmxfbgyfdigrohuewgsilzvynxexftatavatlasifmejvylgmrutixvckdsaaazbjyfacuwopddusldgrtqdcoQUPNBJOTDNUNQJWCZUCQDKEYJPUNEVMHTCQZQSWRQIFZVRIQCIUMNQBKKLHPMYWUKTRRGFQYWJSXFIZEJXDUGOALRVFJFFKEZKLIHEPDPKDEEGUVZCODIDBGKUCBFGKHWEEIATYAJRHEBIBIZIGBDWZGHMQRGTZEQQYZNFXWUHLZKIGARPQBFVHYNLNBJFBGRUYCSAGNYKJCWOSMYGVJISYITSGVOJVAWVMTYTGQAQQFPFQOFARQCJZNOKDTMFIWKAKTBGRAQTGXCYFLVKBOLVRNIAHZAFQFPHBRZYUYBHIDQGZJASKHVNOEPQSLVZYJNSWHAJONVLBZATVYHKBDDXHBIKNOBPIFQNDBFXDIHVHJRJYYEVOOQDJQJLQYSHBQKSTUFTPJFPKQLHLPLGAXLYTYCKW
  • 相关阅读:
    4.Android开发笔记:Activity的生命周期、启动方式、最佳实践
    3.Android开发笔记:Activity 数据传递
    2.Android开发笔记:Activity
    1.《Android开发笔记》系列
    JS
    Mongodb 学习笔记简介
    Sql Server MySql 日期
    实现tomcat与IIS共用80端口
    学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础--第4章
    学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础
  • 原文地址:https://www.cnblogs.com/learning-c/p/5700773.html
Copyright © 2020-2023  润新知