๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[Codeforces] 431A: Black Square

by ์ฃผ๋ฐœ2 2020. 3. 7.
๋ฐ˜์‘ํ˜•

https://codeforces.com/problemset/problem/431/A

 

Problem - 431A - Codeforces

 

codeforces.com

์ฝ”๋“œ

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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int[] arr = new int[4];
		for(int i=0; i<arr.length; i++)
			arr[i] = Integer.parseInt(st.nextToken());
		
		String str = bf.readLine();
		int score = 0;
		
		// ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ•˜๋Š” arr ๋ฐฐ์—ด,
		for(int i=0; i<str.length(); i++) 
			score += arr[str.charAt(i) - 49];
		
		System.out.println(score);
		bf.close();
	}

}

ํ’€์ด

๋ฌธ์ž์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ๋ฌธ์ž์—ด์— ๋งž๋Š” ๋ฐฐ์—ด์„ ๋”ํ•˜๋ฉด ๋จ.

๋ฐฐ์—ด์˜ ์‹œ์ž‘์€ 0์ด๋ฏ€๋กœ, ์•„๋ž˜ ์•„์Šคํ‚ค์ฝ”๋“œ ๊ฐ’์—์„œ 48์ด ์•„๋‹Œ 49๋ฅผ ๋นผ์•ผํ•œ๋‹ค.

 

input

1 2 3 4

123214

 

arr[] = [1, 2, 3, 4]

score = arr[1] + arr[2] + arr[3] + arr[2] + arr[1] + arr[4] = 13

https://namu.wiki/w/%EC%95%84%EC%8A%A4%ED%82%A4%20%EC%BD%94%EB%93%9C

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€