1218: 字符串左移

From: 合工宣OJ  http://xcacm.hfut.edu.cn/problem.php?id=1218

时间限制: 1 Sec  内存限制: 2 MB  

题目描述 给一个长度等于N的字符串,求它左移M位后的字符串。

输入

每组数据两行,第一行N M,0<=N<=1000,0<=M<=1500 第二行给出字符串,字符串只包含大小写字母。

输出

输出左移后的结果。

样例输入

6 3 NetCan 6 2 NetCan 10 4 HelloWorld

样例输出

CanNet tCanNe oWorldHell

本题难点在于字符串左移的长度可能会大于字符串本身的长度,那么实现程序应该可以循环搜索,用%取余恰化简。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 int main()
 6 {
 7     char d[1550];
 8     int a,b,i,j,c;
 9     while(scanf("%d %d",&a,&b)!=EOF)
10     {
11 
12         cin>>d;
13         if(b==0){cout<<d<<endl;}
14         else{
15         c=strlen(d);
16         b=b%a;
17         for(j=b;j<a;j++)
18         {
19             printf("%c",d[j]);
20         }
21         for(j=0;j<=b-1;j++)
22         {
23             printf("%c",d[j]);
24         }
25         for(j=a;j<c;j++)
26         {
27             printf("%c",d[j]);
28         }
29         for(i=0;i<=c;i++)
30         {
31             d[i]=0;
32         }
33         printf("
");
34       }
35     }
36     return 0;
37 }
38 /**************************************************************
39     Problem: 1218
40     User: 2014217052
41     Language: C++
42     Result: 正确
43     Time:34 ms
44     Memory:1504 kb
45 ****************************************************************/
原文地址:https://www.cnblogs.com/dzzy/p/4605057.html