九度_题目1362:左旋转字符串(Move!Move!!Move!!!)

来源:互联网 发布:手机淘宝从哪看几颗心 编辑:程序博客网 时间:2024/05/20 17:39

//连接字符串或直接输出,需要注意的是K有可能大于len,所以需要做一下取余操作

题目描述:

汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
输入:
多组测试数据,每个测试数据包含一个字符序列S和非负整数K。其中S的长度不超过1000。
输出:
对应每个测试案例,输出新序列。
样例输入:
UDBOJ 4
abba 1
样例输出:
JUDBO
bbaa
#include<iostream>#include<string>using namespace std;int main(){    string str;    int len=0;    int num;    while(cin>>str>>num)    {        string str1;        len=str.size();        num%=len;        str1+=str.substr(num,len);        str1+=str.substr(0,num);        cout<<str1<<endl;    }    return 0;} /**************************************************************    Problem: 1362    User: hndxztf    Language: C++    Result: Accepted    Time:280 ms    Memory:1520 kb****************************************************************/

#include<iostream>#include<string>using namespace std;int main(){    string str;    int len=0;    int num;    while(cin>>str>>num)    {        len=str.size();        num%=len;//num可能大于len        for(int i=num;i<len;i++)            cout<<str[i];        for(int i=0;i<num;i++)            cout<<str[i];        cout<<endl;    }    return 0;} /**************************************************************    Problem: 1362    User: hndxztf    Language: C++    Result: Accepted    Time:440 ms    Memory:1520 kb****************************************************************/



0 0
原创粉丝点击