3515 2023. 02. 28 14:48:21 Benedek Autókódolás csharp Időlimit túllépés 38/50 300ms 23956 KiB
using System;

class Program
{
    static void Main(string[] args)
    {
        long n = long.Parse(Console.ReadLine());
        long prev = GetPreviousCode(n);
        long next = GetNextCode(n);

        Console.WriteLine(prev >= 0 ? prev.ToString() : "-1");
        Console.WriteLine(next >= 0 ? next.ToString() : "-1");
        Console.ReadKey();
    }

    static long GetPreviousCode(long n)
    {
        int bitCount = CountOnes(n);
        for (long i = n - 1; i >= 1; i--)
        {
            if (CountOnes(i) == bitCount) return i;
        }
        return -1;
    }

    static long GetNextCode(long n)
    {
        int bitCount = CountOnes(n);
        for (long i = n + 1; i <= long.MaxValue; i++)
        {
            if (CountOnes(i) == bitCount) return i;
        }
        return -1;
    }

    static int CountOnes(long n)
    {
        int count = 0;
        while (n > 0)
        {
            if ((n & 1) == 1) count++;
            n >>= 1;
        }
        return count;
    }
}
Részfeladat Összpont Teszt Verdikt Idő Memória
base 38/50
1 Elfogadva 0/0 28ms 20432 KiB
2 Időlimit túllépés 0/0 300ms 4156 KiB
3 Elfogadva 2/2 27ms 21216 KiB
4 Elfogadva 2/2 28ms 21396 KiB
5 Elfogadva 2/2 26ms 21972 KiB
6 Elfogadva 2/2 28ms 22024 KiB
7 Elfogadva 2/2 28ms 22320 KiB
8 Elfogadva 4/4 27ms 22820 KiB
9 Elfogadva 4/4 27ms 23216 KiB
10 Elfogadva 4/4 27ms 23200 KiB
11 Elfogadva 4/4 28ms 23684 KiB
12 Elfogadva 4/4 27ms 23956 KiB
13 Időlimit túllépés 0/4 300ms 7204 KiB
14 Elfogadva 4/4 29ms 23936 KiB
15 Elfogadva 4/4 82ms 23812 KiB
16 Időlimit túllépés 0/4 268ms 7376 KiB
17 Időlimit túllépés 0/4 246ms 8048 KiB