https://www.acmicpc.net/problem/1551
์ฝ๋
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];
}
}
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 3985๋ฒ: ๋กค ์ผ์ดํฌ(๊ตฌํ, ์๋ฎฌ๋ ์ด์ ) (0) | 2020.03.05 |
---|---|
[Codeforces] 509A: Maximum in Table (0) | 2020.03.05 |
[๋ฐฑ์ค] 1526๋ฒ: ๊ฐ์ฅ ํฐ ๊ธ๋ฏผ์(์๋ฎฌ๋ ์ด์ ) (0) | 2020.03.04 |
[๋ฐฑ์ค] 2563๋ฒ: ์์ข ์ด(์์ ํ์, ๊ตฌํ) (0) | 2020.03.04 |
[๋ฐฑ์ค] 1789๋ฒ: ์๋ค์ ํฉ(๊ตฌํ) (0) | 2020.03.04 |
๋๊ธ