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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); switch (num) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; case 4: System.out.println("four"); break; case 5: System.out.println("five"); break; case 6: System.out.println("six"); break; case 7: System.out.println("seven"); break; case 8: System.out.println("eight"); break; case 9: System.out.println("nine"); break; default: System.out.println("other"); } } } | cs |
2번 문제
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("연산을 입력하세요"); String s = sc.nextLine(); System.out.println("피연산자를 입력하세요"); int x = sc.nextInt(); int y = sc.nextInt(); //여기서 중요하다 생각되는 것은 if문 조건식에서 //String s의 연산자 값을 +,-,*,/을 입력해준 뒤 //==를 이용해서 +,-,*,/ 비교하면 //조건식에서는 s의 값과 +,-,*,/이 같은지 아닌지를 비교하는 것이 아니라 //String s의 주소와 +,-,*,/이 같은지 비교해서 바로 else문으로 간다는 것이다. //따라서 equals()를 사용하여 //입력한 String s의 값 자체와 +,-,*,/를 비교해서 //true 또는 false의 결과가 나오게 해주는 것이 중요하다 if (s.equals("+")) { double sum = x + y; System.out.println(sum); } else if (s.equals("-")) { double sum = x - y; System.out.println(sum); } else if (s.equals("*")) { double sum = x * y; System.out.println(sum); } else if (s.equals("/") && y != 0) { //소수점 자리도 계산하기 위해서 형변환을 해줬다. double sum = (double)x / (double)y; System.out.println(sum); } else { System.out.println("오류입니다."); } } } | cs |
3번 문제
(생각보다 간단한 문제임에도 생각을 잘못하는 바람에 한참이나 돌아갔습니다.....num[((i+1)*3)-1] == 3 * (i + 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 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i; int[] num = new int[50]; for (i = 0; i < num.length; i++) { num[i] = i + 1; if (num[i] % 3 == 0) { System.out.println("짝"); } else { System.out.println(num[i]); } } } } | cs |
4번 문제
(4번 문제는 https://aeunhi99.tistory.com/149의 글을 참고해서 작성했습니다. 제가 저작권 관련해서는 아직 미흡하다 보니,
문제가 되는 부분이 있으면 알려주세요! 조치를 취하겠습니다.)
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 27 28 29 30 31 32 33 34 35 36 37 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0; for (int i = 1; i <= 100; i++) { //3과4의 배수값만 남는다. if (i % 3 == 0) sum += i; //3의 배수의 합 if (i % 4 == 0) sum += i; //4의 배수의 합 //문제는 중복으로 3과 4의 배수중에서 //중복으로 배수가 되는 경우가 있다. ex) 12 같은 수. //그렇게 되면 12 같은 중복이 되는 수를 //3의 배수 if에서 1번 4의 배수에서 1번 총 2번을 더하게 되니 //따라서 중복을 제거해줄 필요가 있다. //즉 순수 3의 배수의 합+ 순수 4의 배수의 합+ 중복이 되는 배수가 되야한다. //3과 4의 배수 값들 중에서 //중복 값 제거용이다. if (i % 3 == 0 && i % 4 == 0) sum -= i; } System.out.println(sum); } | cs |
5번 문제
(4번 문제는 https://aeunhi99.tistory.com/149의 글을 참고해서 작성했습니다. 제가 저작권 관련해서는 아직 미흡하다 보니,
문제가 되는 부분이 있으면 알려주세요! 조치를 취하겠습니다.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] dice1 = { 1, 2, 3, 4, 5, 6 }; int[] dice2 = { 1, 2, 3, 4, 5, 6 }; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (dice1[i] + dice2[j] == 6) { System.out.printf("(%d,%d)", dice1[i],dice2[j]); } } } } } | cs |
6번 문제
(5번 문제를 응용했더니 순식간에 해답이 나왔습니다. 다만 아쉬운 점은... 범위를 지정하는 부분의 코드가 더러운 듯하네요..)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, j; for (i = 0; i <= 100; i++) { for (j = 0; j <= 100; j++) { if ((3 * i) + (10 * j) == 100 && 0 <= i && i <= 10&& 0 <= j && j <= 10) { System.out.printf("(%d,%d) ", i, j); } } } } } | cs |
7번 문제: 진짜...될듯말듯...떠오를듯 말듯 하다가 결국.... 이번에도 인용하게 되네요...
(7번 문제는 https://parkdream.tistory.com/59의 글을 참고해서 작성했습니다. 제가 저작권 관련해서는 아직 미흡하다 보니,
문제가 되는 부분이 있으면 알려주세요! 조치를 취하겠습니다.)
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 27 28 29 30 31 32 33 34 35 36 37 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("2부터 100사이의 소수는? "); // 2와 100사이에 있는 소수를 구하기 위한 범위 설정을 첫번재 for문 // 두번째 for문을 통해 2부터 100까지의 수인 i를 j로 나눠서 // 소수 여부를 구별한다. // 그리고 소수는 1과 자신만으로 나눠지는 수여야 한다. // 즉 자기자신으로만 나눴을 때만 나머지가 0이 나와야 한다. //count는 몇번 나눠지는지 알려준다. //소수는 1과 자신으로 나눠지기에 2이상의 수로 나눌 시 자신으로만 나눠지기에 //당연히 딱 1번만 나눠진다. int count = 0; for (int i = 2; i <= 100; i++) { for (int j = 2; j <= i; j++) { if (i % j == 0) { count++; } } if (count == 1) { System.out.println(i); } count = 0; } } } | cs |
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 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = 0; for (int i = 1; i <= 99; i++) { for (int j = 1; j <= 99; j++) { for (int k = 1; k <= 99; k++) { if ((i * i + j * j) == (k * k)) { System.out.printf("(%d,%d,%d)\n", i, j, k); count++; } } } } System.out.print(count); } } | cs |
9번 문제
(다른 사람의 풀이가 더 간단하지만... 따라하고 싶지 않다는 고집하에 했습니다 ㅎㅎㅎ
지저분한 게 좀 흠이지만.. 그래도 혼자서 한데 의의를 두려고요)
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 27 28 29 30 31 32 | package test; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("출력할 항의 개수: "); int num = sc.nextInt(); int f1 = 0; int f2 = 1; int f3 = 0; for (int i = 1; i <= num; i++) { if (i == 1) { System.out.println(f1); f1++; } else if (i == 2) { System.out.println(f1); } else { f1 = (f2 + f3); System.out.println(f1); f3 = f2; f2 = f1; } } } } | cs |
10번 문제
(안좋은 버릇이긴 한데, 하다가 귀찮아서 결국 다른 사람의 풀이를 보고 반을 풀었네요... 프로그래밍도 끈기라는 것 같던데, 벌써 편한 길을 찾아가려는 거 같아 걱정입니다;; , 출처는 https://aeunhi99.tistory.com/149 입니다.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double[] list = { 1.0, 2.0, 3.0, 4.0 }; double sum = 0; double max_value = list[0]; // max_value = (x > y) ? x : y; for (double d : list) { sum += d; if(max_value < d) max_value = d; } System.out.println("합은 " + sum); System.out.println("최대값은 " + max_value); } } | cs |
11번 문제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] s = {"Hello", "Java", "World"}; for(String str : s) { System.out.println(str); } } } | cs |
12번 문제
(22.9.21: -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 27 28 29 30 | package test; import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("입력할 과목 수: "); int count1 = sc.nextInt(); int count2 = 0; double sum = 0; for (int i = 0; i < count1; i++) { System.out.print("성적을 입력하세요: "); int grade = sc.nextInt(); if (count2 > count1 || count1 ==-1 ) break; ArrayList<Integer> num = new ArrayList<>(); num.add(grade); sum += grade; count2++; } System.out.println("합계: " + sum); System.out.println("평균: " + sum / count2); } } | cs |
'It Study > POWER JAVA(기본서)' 카테고리의 다른 글
POWER JAVA 14번 문제 ~ 15번 문제 (2) | 2022.09.25 |
---|---|
POWER JAVA 3장 프로그래밍 13번 문제 (트럼프 카드 뽑기) (2) | 2022.09.25 |
POWER JAVA 3장 숫자 추측 게임 (0) | 2022.09.11 |
POWER JAVA 3장 프로그래밍 while문 활용 예제(구구단) (0) | 2022.09.09 |
POWER JAVA 3장 프로그래밍 가위, 바위, 보 예제 (0) | 2022.09.04 |