computer_study

[탐색] BAEKJOON '1568'번 '새'문제 (C++/python) 본문

알고리즘/문제풀이

[탐색] BAEKJOON '1568'번 '새'문제 (C++/python)

knowable 2020. 8. 11. 16:51

문제 : www.acmicpc.net/problem/1568

 

1568번: 새

N마리의 새가 나무에 앉아있고, 자연수를 배우기 원한다. 새들은 1부터 모든 자연수를 오름차순으로 노래한다. 어떤 숫자 K를 노래할 때, K마리의 새가 나무에서 하늘을 향해 날아간다. 만약, 현��

www.acmicpc.net

 

 

 

  • python

n = int(input())
result = 0
to_sing_num = 1

while n != 0:
    if to_sing_num > n:
        to_sing_num = 1
    n -= to_sing_num
    to_sing_num += 1
    result += 1

print(result)

 

 

  • c++

 

#include "iostream"

using namespace std;

int result=0;

void count_seconds(int birds, int to_sing_num){
    
    if(birds == to_sing_num){
        result ++;
        printf("%d\n",result);
    }
    
    else if(birds > to_sing_num){
        result ++;
        count_seconds(birds - to_sing_num, to_sing_num + 1);
    }
    else
        count_seconds(birds, 1);
    
}


int main(){
    unsigned int N;
    scanf("%d",&N);
    
    count_seconds(N, 1);
    
    return 0;
}
Comments