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

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[C++/SWEA] 21425. += (D2)

728x90

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AZD8K_UayDoDFAVs

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

sw expert academy

๋‚œ์ด๋„ D2๋ฌธ์ œ

a์— b๋ฅผ ๋”ํ• ๋•Œ, ํ•ญ์ƒ ๋” ํฐ ์ˆ˜๋ฅผ ๋”ํ•˜๋ฉด ๋œ๋‹ค.

 

์ตœ๊ทผ์—๋Š” ํ•ญ์ƒ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋งŒ ํ’€์–ด์„œ ์ž…๋ ฅ ์ฝ”๋“œ ๋ถ€๋ถ„์ด ๋‚ฏ์„ค์–ด์กŒ๋‹ค

๋‹ค์‹œ ์—ฐ์Šตํ•ด์•ผ์ง€!!

 

 

๋‚˜์˜ ํ’€์ด

#include <iostream>
#include <algorithm>
using namespace std;

int answer = 0;

void add(int a, int b, int n, int cnt){
    if(a + b > n){
        answer = cnt;
        return;
    }
    if (a > b)
    { // b๊ฐ€ ๋” ํฐ ์ˆ˜, ํ•ญ์ƒ ํฐ ์ˆ˜๋ฅผ ๋”ํ•˜๊ธฐ
        int tmp = a;
        a = b;
        b = tmp;
    }
    add(a+b, b, n, cnt + 1);
}

int main() {
	int t;
    cin >> t;
    int a;
    int b;
    int n;
    for (int i = 0; i < t; ++i)
    {
        cin >> a >> b >> n;
        add(a, b, n, 1);
        cout << answer << "\n";
    }
    return 0;
}
728x90