본문 바로가기

분류 전체보기126

[012] 숫자 사각형 출력하기(거꾸로) 문제 - 입력된 수만큼 n행 n열의 형태로 연속으로 출력되는 숫자 사각형을 구현하세요 ex) n = 3 1 2 3 6 5 4 7 8 9 - 중첩반복문 이용, 배열 보완할 점 - 처음에 배열을 사용하지 않고 풀어서 해결방법을 찾기 어려웠음 - 배열에 값을 넣는 for문과 출력을 위한 for문을 분리하는 것 기억하기 소스코드 public class Algorithm11 { public static void main(String[] args) { int n = 5; int snum = 0; int[][] arr = new int[n][n]; for(int i = 0; i < n; i++) { if(i % 2 ==0) { for(int j = 0; j < n; j++) { arr[i][j] = snum + 1;.. 2021. 5. 26.
[011] 숫자 사각형 출력하기 문제 - 입력된 수만큼 n행 n열의 형태로 연속으로 출력되는 숫자 사각형을 구현하세요 - 반복문 이용 보완할 점 소스코드 public class Algorithm10 { public static void main(String[] args) { int num = 5; for(int i = 1; i 2021. 5. 19.
[010] 각 자릿수의 합 구하기 문제 - 입력된 수의 각 자릿수 합을 구하세요 - 수 : 1242 - 정답 : 9 보완할 점 - 반복문을 사용해서 좀 더 간단한 코드 구현해보기 소스코드 public class Algorithm09 { public static void main(String[] args) { int num = 1242; int 천 = num / 1000; int 백 = (num % 1000) / 100; int 십 = ((num % 1000) % 100) / 10; int 일 = num % 10; int sum = 천 + 백 + 십 + 일; System.out.println(sum); } } 2021. 5. 19.
[009] 팩토리얼 알고리즘 문제 - 입력된 수의 팩토리얼을 구하세요. - 반복문 사용 보완할 점 소스코드 public class Algorithm08 { public static void main(String[] args) { //팩토리얼을 구하세요 int num = 6; int mul = 1; for(int i = num; i > 0; i--) { mul *= i; } System.out.println(mul); } } 2021. 5. 19.
[008] 소수 판별 알고리즘 문제 - 입력된 수가 소수인지 판별하는 코드를 작성하시오. - 반복문 사용 보완할 점 소스코드 public class Algorithm07 { public static void main(String[] args) { //입력된 수가 소수인지 판별 int num = 6; int min = 2; boolean is = true; //2~ 자기보다 작은값으로 나누었을 때 나머지가 0이 아닌수 while(is) { for(; min < num; min++) { if(num % min == 0) { //한번이라도 여기 들어오면 소수가 아님 is = false; } } if (is) { System.out.println(num + "은 소수입니다"); is = false; } else { System.out.pri.. 2021. 5. 17.
[007] 최대 공약수 구하기 문제 - 두 수의 최대 공약수를 구하시오. - 배열, for, if 사용 보완할 점 - 좀 더 코드를 간결하게 할 수 있을 것 같은데... 고민해서 수정하기 소스코드 import java.util.Arrays; public class Maxnumber { public static void main(String[] args) { // 최대 공약수 구하기 int a = 12; int b = 18; int[] a_arr = new int[a]; int[] b_arr = new int[b]; for(int i=1; i 2021. 5. 13.