35302023-02-28 17:54:49DoraAutókódoláscsharpTime limit exceeded 34/50301ms25260 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace autokodolas
{
    internal class Program
    {
        static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());

            // Count the number of 1 bits in the binary representation of n
            int oneBits = CountOneBits(n);

            // Find the previous code with the same number of 1 bits
            long prevCode = n - 1;
            while (prevCode >= 1 && CountOneBits(prevCode) != oneBits)
            {
                prevCode--;
            }

            // Find the next code with the same number of 1 bits
            long nextCode = n + 1;
            while (CountOneBits(nextCode) != oneBits)
            {
                nextCode++;
            }

            // Kiírás
            if (prevCode > 0)
            {
                Console.WriteLine(prevCode.ToString());
            }
            else
            {
                Console.WriteLine("-1");
            }

            if (nextCode > 0)
            {
                Console.WriteLine(nextCode.ToString());
            }
            else
            {
                Console.WriteLine("-1");
            }

            Console.ReadKey();
        }

        static int CountOneBits(long n)
        {
            string kettes = Convert.ToString(n, 2);         //átírja kettes számrendszerbe a számot
            int count = 0;
            int tmp;
            /*
            while (n > 0)
            {
                if (n % 2 == 1)
                {
                    count++;
                }
                n /= 2;
            }*/
            //Console.WriteLine(n + " " + kettes);
            for(int i = 0; i<kettes.Length; i++)
            {
                tmp = (int) char.GetNumericValue(kettes[i]);
                //Console.WriteLine("tmp: " + tmp);
                if (tmp == 1)
                {
                    count++;
                }
            }
            return count;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base34/50
1Accepted0/028ms21132 KiB
2Time limit exceeded0/0301ms8864 KiB
3Accepted2/228ms21996 KiB
4Accepted2/227ms22148 KiB
5Accepted2/227ms22156 KiB
6Accepted2/227ms22240 KiB
7Accepted2/228ms22668 KiB
8Accepted4/428ms23084 KiB
9Accepted4/427ms22768 KiB
10Accepted4/428ms22748 KiB
11Accepted4/428ms22936 KiB
12Accepted4/427ms23248 KiB
13Time limit exceeded0/4252ms10892 KiB
14Accepted4/446ms25260 KiB
15Time limit exceeded0/4261ms10848 KiB
16Time limit exceeded0/4257ms11060 KiB
17Time limit exceeded0/4282ms11268 KiB