๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 14241_์Šฌ๋ผ์ž„ ํ•ฉ์น˜๊ธฐ

๋ฟŒ์•ผ._. 2023. 11. 2. 11:57

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/14241)

< ์Šฌ๋ผ์ž„ ํ•ฉ์น˜๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ •๋ ฌํ•  ํ•„์š” ์—†์ด ์ž…๋ ฅ๋ฐ›์€ ์Šฌ๋ผ์ž„์„ ์•ž์—์„œ๋ถ€ํ„ฐ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

 

 

 my solution (Java)

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

public class _14241_ { // ์Šฌ๋ผ์ž„ ํ•ฉ์น˜๊ธฐ
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		st=new StringTokenizer(bf.readLine());

		int arr[]=new int[n];
		for(int i=0; i<n; i++) {
			arr[i]=Integer.parseInt(st.nextToken());
		}

		long result=0;
		for(int i=1; i<n; i++) {
			result+=arr[i-1]*arr[i];
			arr[i]=arr[i-1]+arr[i];
		}
		System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์Šฌ๋ผ์ž„์˜ ๊ฐœ์ˆ˜
arr : ์Šฌ๋ผ์ž„ ํฌ๊ธฐ ์ €์žฅ ๋ฐฐ์—ด
result : ์ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’

 

์Šฌ๋ผ์ž„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„ ์Šฌ๋ผ์ž„์˜ ๊ฐœ์ˆ˜๋งŒํผ ํฌ๊ธฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

์ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž…๋ ฅ๋ฐ›์€ ์Šฌ๋ผ์ž„์„ ์•ž์—์„œ๋ถ€ํ„ฐ ํฌ๊ธฐ๋ฅผ ๋”ํ•ด์ฃผ๊ณ  ๊ณฑํ•œ ๊ฐ’์„ ์ ์ˆ˜๋กœ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.