說到循環移動,一開始有點懵,后來一想也好像挺簡單的
7-31 字符串循環左移(20 分)
輸入一個字符串和一個非負整數N,要求將字符串循環左移N次。
輸入格式:
輸入在第1行中給出一個不超過100個字符長度的、以回車結束的非空字符串;第2行給出非負整數N。
輸出格式:
在一行中輸出循環左移N次后的字符串。
輸入樣例:
Hello World!
2
輸出樣例:
llo World!He
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include <string.h> 5 /* 6 輸入在第1行中給出一個不超過100個字符長度的、以回車結束的非空字符串;第2行給出非負整數N。 7 */ 8 9 int main() 10 { 11 char a[101]={0}; 12 int n; 13 int i,j; 14 char c; 15 gets(a); 16 scanf("%d",&n); 17 18 int len = strlen( a ); 19 for ( i=0; i<n; i++){ 20 c = a[0]; 21 for( j=0; j<len; j++){ 22 a[j] = a[j+1]; 23 24 } 25 a [len-1] = c; 26 } 27 puts(a); 28 29 return 0; 30 }
有點迷糊的是在自己的code:blocks上沒運行成功,提交在網頁上竟然過了,過了!!