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

[๋ฐฑ์ค€] 1551๋ฒˆ: ์ˆ˜์—ด์˜ ๋ณ€ํ™”(์ˆ˜ํ•™, ์‹œ๋ฎฌ๋ ˆ์ด์…˜)

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

https://www.acmicpc.net/problem/1551

 

1551๋ฒˆ: ์ˆ˜์—ด์˜ ๋ณ€ํ™”

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด์˜ ํฌ๊ธฐ N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 20๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , K๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N-1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด์ด ‘,’๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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());
		
		/** 1 **/
		int N = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(bf.readLine() , ",");	// ,(์ฝค๋งˆ)๋กœ ์ž๋ฅด๊ธฐ.
		int[] arr = new int[N];
		for(int i=0; i<arr.length; i++)
			arr[i] = Integer.parseInt(st.nextToken());
		
		/** 2 **/
		int[] newArr = Arrays.copyOf(arr, arr.length);
		int arrLength = N-1;
		
		/** 3 **/
		while(K != 0) {
			int[] moveArr = new int[arrLength];
			
			// B[i] = A[i+1] - A[i]
			for(int i=0; i<newArr.length-1; i++) 
				moveArr[i] = newArr[i+1] - newArr[i];
			
			arrLength --;
			K --;
			newArr = Arrays.copyOf(moveArr, moveArr.length);
		}
		
		/** 4 **/
		for(int i=0; i<newArr.length; i++) {
			if(i != newArr.length-1) 
				System.out.print(newArr[i]+",");
			else
				System.out.println(newArr[i]);
		}
		bf.close();
	}

}

ํ’€์ด

์ƒ๊ฐ๋ณด๋‹ค ๊ฝค ๊นŒ๋‹ค๋กญ๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค ..

ํฌ๊ธฐ๊ฐ€ N์ธ ์ˆ˜์—ด A๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ, B[i] = A[i+1] - A[i]์˜ ๊ทœ์น™์„ ๊ฐ€์ง€๊ณ , K๋ฒˆ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ์‹œํ–‰ํ–ˆ์„๋•Œ ๋‚˜์˜ค๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฌธ์ œ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„์„ ํ–ˆ๋Š”๋ฐ,,

 

/** 1 **/

๋ณ€์ˆ˜ ์„ ์–ธ ๋ถ€๋ถ„์ด๋‹ค.

๋ฌธ์ œ์—์„œ ์ž…๋ ฅ๋ฐ›์„๋•Œ ,๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ›์œผ๋ฏ€๋กœ StringTokenizer์˜ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ","๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฝค๋งˆ๋ฅผ ์ž๋ฅธ๋‹ค.

int[] arr = ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›๋Š” ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด

 

/** 2 **/

int[] newArr = ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›์€ arr ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๊ณ ,

์ถ” ํ›„ while๋ฌธ์—์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ์˜ฎ๊ธธ๋•Œ ์‚ฌ์šฉํ•  ๋ฐฐ์—ด.

int arrLength = ๋ฐฐ์—ด์˜ ๊ธธ์ด๋กœ, ํ•œ๋ฒˆ์”ฉ ์ˆ˜ํ–‰ํ• ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œํ•˜๊ณ , ์ฒซ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” N-1 ์ด๋‹ค.

 

/** 3 **/

K๊ฐ€ 0์ด๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ,

moveArr ์€ ๊ธฐ์กด์˜ newArr ๋ฐฐ์—ด์„ B[i] = A[i+1] - A[i] ์˜ ๊ทœ์น™์— ๋”ฐ๋ผ ์˜ฎ๊ฒจ์„œ ์ €์žฅํ•  ๋ฐฐ์—ด์ด๋‹ค.

๊ทธ ํ›„ ํ•œ๋ฒˆ ์ˆ˜ํ–‰๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์”ฉ ๊ฐ์†Œํ•˜๊ณ , K๋ฅผ 1์”ฉ ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

์œ„ ๊ณผ์ •์ด ๋๋‚˜๋ฉด newArr ๋ฐฐ์—ด์— moveArr์— ์ €์žฅ๋œ ๊ฐ’๋“ค์„ ๋ณต์‚ฌํ•œ๋‹ค.

(moveArr ๋ฐฐ์—ด์€ while๋ฌธ์„ ๋Œ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง€๋ฏ€๋กœ, ๋น„๊ตํ•  ๋ฐฐ์—ด์€ newArr์ด ๋œ๋‹ค.)

 

/** 4 **/

์ถœ๋ ฅํ• ๋•Œ๋„ ,(์ฝค๋งˆ)๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰๊ฐ’์—๋Š” ,๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

 

 

 

๋‹ค๋ฅธ ํ’€์ด

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

public class Main{
	public static int SIZE;
	public static int[] arr;
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(in.readLine());
		int i=0, n = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken());
		SIZE = n; arr = new int[n];
		st = new StringTokenizer(in.readLine());
		while(st.hasMoreTokens()) arr[i++] = Integer.parseInt(st.nextToken(","));
		
		for(i=0;i<k;i++) transform();
		i=0;
		while(true){
			System.out.print(arr[i++]);
			if(i==SIZE) break;
			System.out.print(",");
		}
	}
	private static void transform(){
		--SIZE;
		for(int i=0;i<SIZE;i++) arr[i] = arr[i+1] - arr[i];
	}
}

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€