목록bit (5)
N
https://leetcode.com/problems/hamming-distance/ Hamming Distance - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 주어진 x와 y를 이진수로 바꿔서 각 자릿수를 비교했을 때 다른 비트의 수를 구하면 된다. 먼저 x를 이진수로 바꾼 값을 저장할 배열 xArr과 y를 이진수로 바꿔 저장할 배열 yArr을 선언한다. 그리고 리턴할 answer = 0. divide라는 함수를 이용해 인자로 들어온 n을 이진수로 만들어..
191. Number of 1 Bits Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same,..
문제 어떤 N개의 수가 주어져 있다. 그런데 중간에 수의 변경이 빈번히 일어나고 그 중간에 어떤 부분의 합을 구하려 한다. 만약에 1,2,3,4,5 라는 수가 있고, 3번째 수를 6으로 바꾸고 2번째부터 5번째까지 합을 구하라고 한다면 17을 출력하면 되는 것이다. 그리고 그 상태에서 다섯 번째 수를 2로 바꾸고 3번째부터 5번째까지 합을 구하라고 한다면 12가 될 것이다. 입력 첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. M은 수의 변경이 일어나는 횟수이고, K는 구간의 합을 구하는 횟수이다. 그리고 둘째 줄부터 N+1번째 줄까지 N개의 수가 주어진다. 그리고 N+2번째 줄부터 N+M+K+1번째 줄까지 세 ..
https://programmers.co.kr/learn/courses/30/lessons/77885?language=cpp 코딩테스트 연습 - 2개 이하로 다른 비트 programmers.co.kr https://ansohxxn.github.io/programmers/148/ [C++로 풀이] 2 개 이하로 다른 비트 ⭐⭐ 📌 2 개 이하로 다른 비트 ansohxxn.github.io 비트 관련 문제는 아직 어려움이 있는 것 같다...ㅠ 다른 분의 사이트에서 참고하였고, 다시 공부해서 풀어봐야겠다. #include #include #include using namespace std; vector solution(vector numbers) { vector answer; for(int i = 0; i <..

bitset을 이용하여 문제를 풀었다..! bitset은 int형 n을 bit 형태로 만들어 주기 때문에 이 문제에 적용하기 좋다..! 우선 1의 개수를 알아야 하기 때문에 count 변수에 n의 1의 개수를 넣어준다. 그리고 answer에는 n + 1을 넣어주고, n + 1을 bit로 바꾸었을 때 1의 개수가 count와 같은 것이 나올 때까지 answer++을 해주면 쉽게 구할 수 있다. #include #include #include using namespace std; int solution(int n) { int answer = n + 1; int count = bitset(n).count(); while(bitset(answer).count() != count) answer++; return ..