ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준 알고리즘] 단계별로 풀어보기 1단계 JAVA
    코딩테스트/백준 2023. 4. 14. 08:47

     

    백준 알고리즘 문제 단계별로 풀어보기를 시작하려 한다. 1단계부터 차근차근 문제 풀며 나오는 개념을 정리 start! 

     

     

    https://www.acmicpc.net/step/1

     

    입출력과 사칙연산 단계

    입출력과 사칙연산

    www.acmicpc.net

     

     

    📑 1. Hello World ( 2557)

     

    public class Main {
        public static void main(String args[]){
            System.out.println("Hello World!");
        }
    }

     

     

    📑 2. A+B (1000)

     

    Scanner sc = new Scanner(System.in);
    
    int A = sc.nextInt();
    int B = sc.nextInt();
    System.out.println(A+B);

     

    조건에 보면 (0<A, B<10)이 나와있길래 조건을 줬는데 답변 찾아보면 굳이 주지 않아도 정답으로 체크된다. 

     

    package step1;
    
    import java.util.Scanner;
    
    public class Q2_1000 {
    
    	public static void main(String args[]) {
    		
    		Scanner sc = new Scanner(System.in);
    		
    		int A = sc.nextInt();
    		int B = sc.nextInt();
    		
    		while(true) {
    		if(A>0 && B<10) {
    			System.out.print(A+B);
    			break;
    		}else {
    			A = sc.nextInt();
    			B = sc.nextInt();
    		}
    		}
    		
    		
    		
    		sc.close();
    	}
    }

     

     

     

    📑 5. A/B (1008)

     

     처음에 float 했더니 틀렸다.  double로 바꾸면 된다! 

     

    package step1;
    
    import java.util.Scanner;
    
    public class Q5_1008 {
    
    	public static void main(String args[]) {
    		
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		int b = sc.nextInt();
    		
    		
    		System.out.println((double)a/b);
    		
    	}
    }

     

     

     

     

    📑 6. 사칙연산 (10869)

     

    package step1;
    
    import java.util.Scanner;
    
    public class Q6_10869 {
    
    	public static void main(String[] args) {
    
    
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		int b = sc.nextInt();
    		
    		System.out.println(a+b);
    		System.out.println(a-b);
    		System.out.println(a*b);
    		System.out.println(a/b);
    		System.out.println(a%b);
    		
    		sc.close();
    	}
    
    }

     

     

    📑 7. ??!(10926)

     

    <Scanner 사용>

    package step1;
    
    import java.util.Scanner;
    
    public class Q7_10926 {
    
    
    	public static void main(String[] args) {
    
    
    		Scanner sc = new Scanner(System.in);
    		String name = sc.next();
    		
    		System.out.println(name + "??!");
    	}
    }

     

     

    <BufferedReader 사용>

    package step1;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Q7_10926 {
    
    
    	public static void main(String[] args) throws IOException{
    
    
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String name = br.readLine();
    		System.out.println(name + "??!");
    	}
    }

     

     

     

     

    📑 10. 빈 칸에 들어갈 수는?

     

    package step1;
    
    import java.util.Scanner;
    
    public class Q10_2588 {
    
    	public static void main(String[] args) {
    
    		Scanner sc = new Scanner(System.in);
    		String A = sc.next();
    		String B = sc.next();
    		int multipl = 0;
    		
    		for(int i = 2; i>=0; i--) {
    			
    			int subB = Integer.parseInt(B.substring(i, i+1)); 
    			multipl = Integer.parseInt(A) * subB;
    			System.out.println(multipl);
    		}
    		
    		System.out.println(Integer.parseInt(A) * Integer.parseInt(B));
    		
    		sc.close();
    		
    		
    	}
    
    }

     

     

    String의 substring을 이용해서 문자열 자릿수를 자르는 방식을 이용했다. 다른 정답들은 다음과 같다. 

     

     

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            int A = Integer.parseInt(br.readLine());
            String B = br.readLine();
            
            char[] arr = B.toCharArray();
            
            System.out.println(A * (arr[2] - '0'));
            System.out.println(A * (arr[1] - '0'));
            System.out.println(A * (arr[0] - '0'));
            System.out.println(A * Integer.parseInt(B));
        }
    }

     

     

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class Main{
        public static void main(String args[])throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int a = Integer.parseInt(br.readLine());
            int b = Integer.parseInt(br.readLine());
          
            int n100 = b/100;
            int n10 = b/10- n100*10;
            int n1 = b - n100*100 - n10*10;
          
            int m1 = a * n1;
            int m2 = a * n10;
            int m3 = a * n100;
            
            System.out.println(m1);
            System.out.println(m2);
            System.out.println(m3);
            System.out.println(m1+m2*10+m3*100); 
        }
    }

     

     

    반응형

    댓글

Designed by Tistory.