본문 바로가기

알고리즘 문제연습33

[015] 입력된 숫자의 개수 출력하기 문제 - 입력된 숫자의 개수를 출력하시오 입력 : 4214215218 결과 0: 0 1: 2 2: 3 3: 0 4: 2 5: 1 6: 0 7: 0 8: 1 9: 0 - 중첩반복문 이용, 배열 보완할 점 소스코드 package Algorithm; import java.util.Scanner; public class Algorithm13 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.next(); //글자수 세기 위해서 String으로 선언 int[] arr = new int[10]; for(int i = 0; i < num.length(); i++) { //입력한 숫자 for(int.. 2021. 5. 27.
[014] 숫자 사각형 출력하기(행 자릿수 만큼 증가) 문제 - 입력된 수만큼 n행 n열의 형태로 연속으로 출력되는 숫자 사각형을 구현하세요 ex) n = 3 1 2 3 2 4 6 3 6 9 - 중첩반복문 이용, 배열 보완할 점 소스코드 public class Algorithm13 { public static void main(String[] args) { int n = 5; int snum = 1; int[][] arr = new int[n][n]; for(int i = 0; i < n; i++) { snum = i + 1; // snum 값 변경 for(int j = 0; j < n; j++) { arr[i][j] = snum; snum = snum + (i + 1); } } for(int i = 0; i < n; i++) { for(int j = 0; .. 2021. 5. 26.
[013] 숫자 사각형 출력하기(세로로) 문제 - 입력된 수만큼 n행 n열의 형태로 연속으로 출력되는 숫자 사각형을 구현하세요 ex) n = 3 1 4 7 2 5 8 3 6 9 - 중첩반복문 이용, 배열 보완할 점 소스코드 public class Algorithm12 { 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++) { snum = i + 1; // for(int j = 0; j < n; j++) { arr[i][j] = snum; snum = snum + n; } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { Sy.. 2021. 5. 26.
[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.