백준/JAVA

[백준/JAVA] 단계별로 풀어보기(5단계) 코드 모음

누룽지맛치킨 2023. 1. 18. 09:54

문제 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;
    }
}

문제 4673) 셀프 넘버

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        boolean self[] = new boolean[10001];
        for(int i=1;i<10000;i++) {
            d(self,i);
        }
        for(int i=1;i<self.length;i++) {
            if(!self[i]) {
                sb.append(i).append("\n");
            }
        }
        System.out.print(sb);
    }

    static void d(boolean self[], int i) {
        int temp = i;
        while(temp>0) {
            i+=temp%10;
            if(i>10000)
                return;
            temp/=10;
        }
        self[i]=true;
    }
}

문제 1065) 한수

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class b_1065 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        System.out.println(hansu(N));
    }

    private static int hansu(int N) {
        int count = 0;
        for(int i=1;i<=N;i++) {
            if(i<100) {
                count++;
            }
            else if(i<1000) {
                int h = i/100;
                int t = i/10%10;
                int k = i%10;
                if(h-t==t-k) {
                    count++;
                }
            }
        }

        return count;
    }
}