Captain Jack decides to to take over a rival’s ship. He needs to send his henchmen over on rowboats that can hold 6 pirates each. You will help him count out pirates in groups of 6. The last rowboat may have fewer than 6 pirates. To make your task easier each pirate has been assigned a number from 1 to N
.***
The input will be N
, the number of pirates you need to send over on rowboats.
The output will be the number of each pirate separated by spaces, with the word ”Go!
” after every 6th pirate, and after the last pirate.
10
1 2 3 4 5 6 Go! 7 8 9 10 Go!
18
1 2 3 4 5 6 Go! 7 8 9 10 11 12 Go! 13 14 15 16 17 18 Go!
High School > University of Maryland High School Programming Contest > HSPC 2007 P1번
import java.util.Scanner;
public class Code5300 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++){
System.out.print((i+1)+" ");
if((i+1)%6==0 || i==n-1){
System.out.print("Go! ");
}
}
}
}
6
다음 Go!
를 해야 하니까 만약 i+1
이 6의 배수라면 뒤에 Go!
를 출력해주면 된다. 또는, 마지막 원소이면 출력해주면 된다.