공부기록/백준

[백준] 25304번 영수증, 10951번 A+B-4, 10870번 피보나치 수 5

메델 2023. 1. 31. 17:12

10870번  피보나치 수 5

 

import java.util.Scanner;

public class Main {
	
	public static int fibo(int n) {
		if(n == 0) {
			return 0;
		}
		if(n==1) {
			return 1;
		}
		else {
			return fibo(n-1) + fibo(n-2);
		}
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		System.out.println(fibo(n));
		scanner.close();
		
	}
}

25304번 영수증

대소문자 잘입력하자 ^^ (NO라고 썼다가 한 5분 날렸다..)

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int total = scanner.nextInt();
		int type = scanner.nextInt();
		int sum = 0;

		for (int i = 0; i < type; i++) {
			int item = scanner.nextInt();
			int number = scanner.nextInt();
			sum += item * number;
		}

		if (total == sum) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}

		scanner.close();

	}
}

 

10951번 A+B-4

이 문제의 경우 런타임 에러를 조심하자 

while문에 scanner.hasNext()를 넣어주는게 포인트인 문제 

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		while(scanner.hasNext()) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			System.out.println(a+b);		
		}
	}	
}