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

[Codeforces] 1077A: Frog Jumping

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

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

 

Problem - 1077A - Codeforces

 

codeforces.com

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		for(int tc=0; tc<t; tc++) {
			int a = scan.nextInt();	// Right
			int b = scan.nextInt();	// Left
			int k = scan.nextInt();	// Jumps
			int ans = 0;			// Print
			for(int i=0; i<k; i++) 
				ans += (i%2 == 0) ? a : -b;
			System.out.println(ans);
		}
		scan.close();
	}

}

ํ’€์ด

k๋ฒˆ ๋งŒํผ ์ ํ”„ํ•˜๊ณ , a๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ(+a) , b๋Š” ์™ผ์ชฝ์œผ๋กœ(-b) ์ ํ”„ ํ•œ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ ์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ.

k๋ฒˆ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ํ™€์ˆ˜์ผ๋•Œ a๋ฅผ ๋”ํ•˜๊ณ , ์ง์ˆ˜์ผ๋•Œ b๋ฅผ ๋นผ์ฃผ๋ฉด ๋.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€