Algorithm/Jungol
[기초다지기] : 반복제어문1 - 형성평가
히욤이
2019. 2. 26. 00:45
125 : 반복제어문1 - 형성평가1
제한시간: 1000 ms 메모리제한: 0 MB
해결횟수: 7547 회 시도횟수: 13527 회
정수를 입력받아 1부터 입력받은 정수까지를 차례대로 출력하는 프로그램을 작성하시오.
![]() 5 | ![]() 1 2 3 4 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n =sc.nextInt(); int i =1; while (i<=n) { System.out.print(i +" "); i++; } } } | cs |
126 : 반복제어문1 - 형성평가2
제한시간: 1000 ms 메모리제한: 0 MB
해결횟수: 7097 회 시도횟수: 14217 회
정수를 입력받다가 0 이 입력되면 그 때까지 입력받은 홀수의 개수와 짝수의 개수를 출력하는 프로그램을 작성하시오.
![]() 9 7 10 5 33 65 0 | ![]() odd : 5 even : 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int odd = 0; int even =0; while (true) { int n = sc.nextInt(); if (n==0) { System.out.println("odd : " + odd); System.out.println("even : " + even); } else if (n%2 ==0) { even++; } else { odd++; } } } } | cs |
127 : 반복제어문1 - 형성평가3
제한시간: 1000 ms 메모리제한: 0 MB
해결횟수: 6430 회 시도횟수: 17852 회
0 부터 100 까지의 점수를 계속 입력받다가 범위를 벗어나는 수가 입력되면 그 이전까지 입력된 자료의 합계와 평균을 출력하는 프로그램을 작성하시오.
(평균은 반올림하여 소수 첫째자리까지 출력한다.)
![]() 55 100 48 36 0 101 | ![]() sum : 239 avg : 47.8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int score, sum =0; float cnt =0; float avg; while (true) { score = sc.nextInt(); if (score >100 || score<0) { avg = sum/cnt; System.out.println("sum : " +sum); System.out.println("avg : " + Math.round(avg*10)/10.0); } else { sum = sum+score; cnt++; } } } } | cs |
128 : 반복제어문1 - 형성평가4
제한시간: 1000 ms 메모리제한: 0 MB
해결횟수: 6263 회 시도횟수: 10287 회
0 이 입력될 때까지 정수를 계속 입력받아 3의 배수와 5의 배수를 제외한 수들의 개수를 출력하는 프로그램을 작성하시오.
![]() 1 2 3 4 5 6 7 8 9 10 0 | ![]() 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; int sum = 0; int cnt =0; while (true) { n = sc.nextInt(); sum++; if (n==0) { System.out.println(sum-cnt-1); break; } else if (n%3==0 || n%5 ==0) { cnt++; } } } } | cs |
129 : 반복제어문1 - 형성평가5
제한시간: 1000 ms 메모리제한: 0 MB
해결횟수: 5471 회 시도횟수: 16974 회
삼각형의 밑변의 길이와 높이를 입력받아 넓이를 출력하고, "Continue? "에서 하나의 문자를 입력받아 그 문자가 'Y' 나 'y' 이면 작업을 반복하고 다른 문자이면 종료하는 프로그램을 작성하시오.
(넓이는 반올림하여 소수 첫째자리까지 출력한다.)
![]() Base = 11 Height = 5 Triangle width = 27.5 Continue? Y Base = 10 Height = 10 Triangle width = 50.0 Continue? N |
문자를 입력받을 때에는 %c 앞에 공백을 넣어 주어야 엔터, 공백과 같은 구분자를 입력받지 않고 한 개의 문자를 입력받을 수 있다. 예 : scanf(" %c", &문자변수);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { System.out.print("Base = "); int base = sc.nextInt(); System.out.print("Height = "); int height = sc.nextInt(); float width = ((float)base*height)/2; System.out.println("Triangle width = " +width); System.out.print("Continue? "); String answer = sc.next(); if (answer.equals("Y") || answer.equals("y")) { continue; } else { break; } } } } | cs |