"프로그래머스 181880 1로 만들기"의 두 판 사이의 차이

(새 문서: ==개요== {{프로그래머스|레벨=0|페이지=4}})
 
1번째 줄: 1번째 줄:
==개요==
==개요==
{{프로그래머스|레벨=0|페이지=4}}
{{프로그래머스|레벨=0|페이지=4}}
==C++==
<syntaxhighlight lang='cpp'>
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> num_list) {
    int size = num_list.size();
    int x = 0;
    while(true) {
        int ones = 0;       
        for(int i=0; i<size; i++) {
            if(num_list[i] == 1) {
                ones++;
                continue;
            }
            x++;
            if(num_list[i]%2==0) {
                num_list[i] /= 2;
            } else {
                num_list[i] = (num_list[i]-1)/2;
            }
        }
        if(ones == size) {
            break;
        }
    }
    return x;
}
</syntaxhighlight>

2023년 9월 20일 (수) 23:14 판

1 개요

프로그래머스 181880 1로 만들기


2 C++

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> num_list) {
    int size = num_list.size();
    int x = 0;
    while(true) {
        int ones = 0;        
        for(int i=0; i<size; i++) {
            if(num_list[i] == 1) {
                ones++;
                continue;
            }
            x++;
            if(num_list[i]%2==0) {
                num_list[i] /= 2;
            } else {
                num_list[i] = (num_list[i]-1)/2;
            }
        }
        if(ones == size) {
            break;
        }
    }
    return x;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}