From: 合工宣OJ http://xcacm.hfut.edu.cn/problem.php?id=1218
时间限制: 1 Sec 内存限制: 2 MB
提交: 16 解决: 8
题目描述
给一个长度等于N的字符串,求它左移M位后的字符串。
输入
每组数据两行,第一行N M,0<=N<=1000,0<=M<=1500
第二行给出字符串,字符串只包含大小写字母。
输出
输出左移后的结果。
样例输入
6 3
NetCan
6 2
NetCan
10 4
HelloWorld
样例输出
CanNet
tCanNe
oWorldHell
本题难点在于字符串左移的长度可能会大于字符串本身的长度,那么实现程序应该可以循环搜索,用%取余恰化简。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char d[1550];
int a,b,i,j,c;
while(scanf("%d %d",&a,&b)!=EOF)
{
cin>>d;
if(b==0){cout<<d<<endl;}
else{
c=strlen(d);
b=b%a;
for(j=b;j<a;j++)
{
printf("%c",d[j]);
}
for(j=0;j<=b-1;j++)
{
printf("%c",d[j]);
}
for(j=a;j<c;j++)
{
printf("%c",d[j]);
}
for(i=0;i<=c;i++)
{
d[i]=0;
}
printf("\n");
}
}
return 0;
}
/**************************************************************
Problem: 1218
User: 2014217052
Language: C++
Result: 正确
Time:34 ms
Memory:1504 kb
****************************************************************/