[java] 백준 단계별로 풀어보기 6단계알고리즘/백준2021. 6. 30. 23:16
Table of Contents
1단계 15596번 정수 N개의 합
public class Test {
long sum(int[] a) {
long ans = 0;
for(int i=0; i<a.length; i++){
ans +=a[i];
}
return ans;
}
}
2단계 4673번 셀프 넘버
import java.util.Scanner;
class Main {
public static void main(String[] args) {
selfNum();
}
public static void selfNum() {
// 출력은 10,000보다 작거나 같은 이라고 했기 때문에 배열을 10001길이로 생성.
int c = 10001;
int[] arr = new int[c];
int dn;
int n;
for (int i = 1; i < c; i++) {
n = i;
dn = n;
// 수열을 구하는 코드
while (n > 0) {
dn += n % 10;
n /= 10;
}
// arr[dn] = 0이 아닌 숫자가 들어감.
if (dn < c)
arr[dn] = dn;
}
// 배열 값이 0인 배열만 출력. dn이 들어간 배열은 출력하지 않음
for (int i = 1; i < c; i++)
if (arr[i] == 0)
System.out.println(i);
}
}
3단계 1065번 한수
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HanSu(n);
}
public static void HanSu(int n) {
int count = 99;
int[] arr = new int[3];
int k=0;
if (n < 100) {
count = n;
System.out.println(count);
}else {
// 100이상인 수의 등차수열 개수 구하는 것(1000미포함)
for (int i = 100; i <= n; i++) {
// 각 자리를 떼서 일정한 차이가 있는지 확인
k=i;
for (int j = 0; j < arr.length; j++) {
arr[j] = k % 10;
k /= 10;
if(k == 10){
arr[2] = 1;
}
}
if(arr[0]-arr[1] == arr[1] - arr[2]){
count=count+1;
}
// 코드가 너무 길어질 것 같아서 그냥 count를 정해줌....
if(i==1000)
count=144;
}
System.out.println(count);
}
}
}
2번, 3번 문제 이해가 잘 안돼서 저랑 같은 생각하신 분들 질문 찾아보면서 문제 이해하느라 조금 늦었습니다. ㅎ
'알고리즘 > 백준' 카테고리의 다른 글
[java] 백준 단계별로 풀어보기 7단계 (2) (0) | 2021.07.02 |
---|---|
[java] 백준 단계별로 풀어보기 7단계 (1) (0) | 2021.07.01 |
[java] 백준 단계별로 풀어보기 5단계 (2) (0) | 2021.06.29 |
[java] 백준 단계별로 풀어보기 5단계 (1) (0) | 2021.06.28 |
[java] 백준 단계별로 풀어보기 4단계 (0) | 2021.06.25 |
@펄찌 :: Pearl's Story
펄의 일상이 궁금한 사람 요기~
즐거운 하루 되셨으면 좋겠습니다😊