자연수 n이 주어졌을 때, n의 다음 큰 숫자는 다음과 같이 정의 합니다.
예를 들어서 78(1001110)의 다음 큰 숫자는 83(1010011)입니다.
자연수 n이 매개변수로 주어질 때, n의 다음 큰 숫자를 return 하는 solution 함수를 완성해주세요.
n | result |
---|---|
78 | 83 |
15 | 23 |
입출력 예#1
입출력 예#2
using System;
using System.Linq;
class Solution
{
public int solution(int n)
{
int answer = 0;
int count = BinaryNumber(n);
bool check = true;
while(check == true)
{
n++;
if(count == BinaryNumber(n))
{
answer = n;
break;
}
}
return answer;
}
public int BinaryNumber(int n)
{
string s = Convert.ToString(n, 2);
int count = 0;
for(int i = 0; i < s.Length; i++)
{
if(s[i] == '1')
{
count++;
}
}
return count;
}
}