알고리즘/백준

[BAEKJOON] 6810 ISBN

응애~ 개발자 2022. 11. 13. 00:03
728x90
반응형

문제 요약

  • 알고리즘 분류: 베열, 정렬
  • 난이도: Bronze5
  • 문제내용:
    • 9780921418 뒤에 숫자 3개 추가한다.
    • 각 숫자마다 1, 3 교차로 곱해서 더한 값을 출력해라
  • 사이트 주소: https://www.acmicpc.net/problem/2587

Code

Python

code = [9, 7, 8, 0, 9, 2, 1, 4, 1, 8]
code.extend([int(input()) for _ in range(3)])
s = 0
for i in range(len(code)):
    s += code[i] * (1, 3)[i % 2]
    
print("The 1-3-sum is", s)

Java

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

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int[] array = {1, 3};
		int[] code = {9, 7, 8, 0, 9, 2, 1, 4, 1, 8, 0, 0, 0};
		for(int i = 0; i < 3; i++) {
			code[i + 10] = Integer.parseInt(br.readLine());
		}
		int sum = 0;
		for(int i = 0; i < code.length; i++) {
			sum += code[i] * array[i % 2];
		}
		System.out.println("The 1-3-sum is " + sum);
		
	}
	
}
728x90
반응형