7-31 字串迴圈左移(20 分)Java實現
阿新 • • 發佈:2019-02-17
7-31 字串迴圈左移(20 分)
輸入一個字串和一個非負整數N,要求將字串迴圈左移N次。
輸入格式:
輸入在第1行中給出一個不超過100個字元長度的、以回車結束的非空字串;第2行給出非負整數N。
輸出格式:
在一行中輸出迴圈左移N次後的字串。
輸入樣例:
Hello World!
2
輸出樣例:
llo World!He
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in );
String str=sc.nextLine();
int a=str.length();
int b=sc.nextInt();
b%=a;
for(int i=b;i<str.length();i++){
System.out.print(str.charAt(i));
}
for(int i=0;i<b;i++){
System.out.print(str.charAt(i));
}
}
}