inblog logo
|
LHS's Study Space
    알고리즘문제풀기C#

    [알고리즘 문제 풀기] 4와 7(2877)

    C#
    lhs's avatar
    lhs
    Feb 17, 2025
    [알고리즘 문제 풀기] 4와 7(2877)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    www.acmicpc.net
    https://www.acmicpc.net/problem/2877
    notion image

    1. 문제 풀이 아이디어

    • k번째 4와 7로 이루어진 수를 찾기 위해 이진수 변환 방식을 사용하여 문제를 해결한다.

    2. 나의 정답 코드

    using (StreamReader sr = new StreamReader(Console.OpenStandardInput())) using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput())) { int k = int.Parse(sr.ReadLine()); int len = 1; int sum = 0; while (true) { if (sum + Math.Pow(2, len) >= k) break; sum += (int)Math.Pow(2, len); len++; } k -= sum + 1; string s=Convert.ToString(k, 2).PadLeft(len, '0').Replace('0','4').Replace('1','7'); sw.WriteLine(s); }

    3. 정리

    • len을 1부터 시작해 자리수를 증가시키며 k가 속한 자리수를 찾는다.
    • sum에 이전 자리수까지의 개수를 누적하여 k에서 제외한다.
    • k-1을 이진수로 변환하여 len 길이로 맞춘다.
    • 0을 4, 1을 7로 변환하여 출력한다.
     
    Share article

    LHS's Study Space

    RSS·Powered by Inblog