GameChoi
Choi Programming
GameChoi
전체 방문자
오늘
어제
  • 분류 전체보기 (468)
    • C++ Algorithm & Study (184)
      • C++ & Algorithm Strategies (45)
      • Game Math & DirectX 11 (72)
      • Server + UE5 (29)
      • Lyra Clone Coding (37)
    • Create Game (284)
      • [Window API] Game Client & .. (55)
      • [DirectX] DirectX 2D & 3D (155)
      • [UE5] BLUEPRINT & C++ (74)
    • odds and ends (0)
      • English (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Direct11
  • Network Worker
  • protobuf
  • Player State
  • UE5
  • Destination Move Packet
  • client
  • c++
  • GAME Client
  • core
  • Player Move Packet
  • Algorithm Strategies
  • server
  • session
  • Other Character
  • RPG Game
  • Game Room
  • Direct3D
  • Game Server
  • job queue

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
GameChoi

Choi Programming

[Programmers] 이진 변환 반복하기
C++ Algorithm & Study

[Programmers] 이진 변환 반복하기

2023. 1. 19. 23:55

 

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

0. Headers

#include <string>
#include <vector>
using namespace std;

1. 알고리즘

 - 총 횟수 count, 0의 개수 zero 생성

 - 조건이 1이 될때까지이므로 반복문 생성

   - 1의 개수를 확인하고 전체의 개수에서 1의 개수를 빼면 0의 개수가 나옴

vector<int> solution(string s) {
    int count = 0, zero = 0;
    
    while(s != "1"){
        int temp = 0;
        for(int i = 0; i < s.length(); i++)
            if (s[i] == '1') temp++;        
        zero += s.length() - temp;
    }
}

 - 그 후 1의 개수를 이진수로 변경

vector<int> solution(string s) {
    while(s != "1"){
        s = "";
        while(temp > 0){
            if (temp % 2 == 0) s += "0";
            else s += "1";
            temp /= 2;
        }
        reverse(s.begin(), s.end());
        count++;
    }
    return {count, zero};
}

2. 완성 코드

#include <string>
#include <vector>
using namespace std;

vector<int> solution(string s) {
    int count = 0, zero = 0;
    
    while(s != "1"){
        int temp = 0;
        for(int i = 0; i < s.length(); i++)
            if (s[i] == '1') temp++;        
        zero += s.length() - temp;
        
        s = "";
        while(temp > 0){
            if (temp % 2 == 0) s += "0";
            else s += "1";
            temp /= 2;
        }
        reverse(s.begin(), s.end());
        count++;
    }
    return {count, zero};
}

저작자표시 (새창열림)
    GameChoi
    GameChoi

    티스토리툴바