34952023-02-28 12:55:43DoraAutókódoláscsharpTime limit exceeded 34/50280ms26540 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++;
            }

            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/027ms21068 KiB
2Time limit exceeded0/0280ms8880 KiB
3Accepted2/228ms22076 KiB
4Accepted2/228ms22352 KiB
5Accepted2/227ms22720 KiB
6Accepted2/228ms23504 KiB
7Accepted2/228ms23288 KiB
8Accepted4/428ms23148 KiB
9Accepted4/428ms23564 KiB
10Accepted4/427ms23772 KiB
11Accepted4/428ms24036 KiB
12Accepted4/427ms24060 KiB
13Time limit exceeded0/4261ms11684 KiB
14Accepted4/446ms26540 KiB
15Time limit exceeded0/4256ms12572 KiB
16Time limit exceeded0/4261ms12768 KiB
17Time limit exceeded0/4266ms12744 KiB