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

Jmnote (토론 | 기여)님의 2023년 11월 16일 (목) 21:59 판
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

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& n: num_list) {
            if(n == 1) {
                ones++;
            } else {
                n /= 2;
                x++;
            }
        }
        if(ones == size) {
            break;
        }
    }
    return x;
}
#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 }}