简单题练习——Move!Move!!Move!!!

题目描述:
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
输入:
多组测试数据,每个测试数据包含一个字符序列S和非负整数K。其中S的长度不超过1000。
输出:
对应每个测试案例,输出新序列。
样例输入:
UDBOJ 4
abba 1
样例输出:
JUDBO
bbaa

总结:题目很简单,注意一开始得k%=n;惯性很容易忽略

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <assert.h>
#include <vector>
#include <limits.h>
using namespace std;
char StrDic[1005];     
 
void Reverse(char* str, int start, int end)  
{      
    while (start<end)      
    {          
        char temp = str[start];          
        str[start] = str[end];          
        str[end] = temp;          
        start++;          
        end--;     
    }  
}  
 
int main()
{
    //freopen("in.txt","r",stdin);
    int k;
    while(cin>>StrDic>>k)
    {
        int len=strlen(StrDic);
        k%=len;
        Reverse(StrDic,0,k-1);
        Reverse(StrDic,k,len-1);
        Reverse(StrDic,0,len-1);
        cout<<StrDic<<endl;
    }
     
    return 0;
}
/**************************************************************
    Problem: 1362
    User: xjbscut
    Language: C++
    Result: Accepted
    Time:350 ms
    Memory:1512 kb
****************************************************************/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值