34942023-02-28 12:48:57DoraAutókódoláscsharpTime limit exceeded 34/50301ms26124 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++;
            }

            Console.WriteLine(prevCode >= 1 ? prevCode.ToString() : "-1");
            Console.WriteLine(nextCode.ToString());
            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/028ms20936 KiB
2Time limit exceeded0/0277ms8768 KiB
3Accepted2/228ms21724 KiB
4Accepted2/228ms22668 KiB
5Accepted2/228ms22380 KiB
6Accepted2/228ms22980 KiB
7Accepted2/228ms23068 KiB
8Accepted4/428ms23592 KiB
9Accepted4/427ms24136 KiB
10Accepted4/428ms24248 KiB
11Accepted4/428ms24324 KiB
12Accepted4/428ms24592 KiB
13Time limit exceeded0/4301ms11880 KiB
14Accepted4/448ms26124 KiB
15Time limit exceeded0/4301ms11764 KiB
16Time limit exceeded0/4259ms11748 KiB
17Time limit exceeded0/4246ms12220 KiB