35292023-02-28 17:52:03DoraAutókódoláscsharpPartially correct 27/50270ms32052 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace autokodolas_gyorsabb
{
    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);
            string x = Convert.ToString(n, 2);
            string x_prev = x;
            string x_next = x;

            if (x == "1")
            {
                x_prev = "-1";
                x_next = "10";
            }
            else if (oneBits == 1)
            {
                x_prev = x.Remove(x.Length - 1, 1);
                x_next = x + "0";
            }
            else if (oneBits == x.Length)
            {
                x_prev = "-1";
                x_next = x + "0";
            }
            else
            {
                // x_prev
                char[] ch = x.ToCharArray();
                for (int i = x.Length - 1; 0 < i; i--)
                {
                    if (x[i] == '1' && x[i + 1] == '0')
                    {
                        ch[i] = '0';
                        ch[i + 1] = '1';
                        break;
                    }
                }
                x_prev = new string(ch);
                // x_next

                long nextCode = n + 1;
                while (CountOneBits(nextCode) != oneBits)
                {
                    nextCode++;
                }

                x_next = Convert.ToString(nextCode, 2);
                /*
                ch = x.ToCharArray();
                for (int i = 1; i < x.Length - 1; i++)
                {
                    if (x[i] == '0' && x[i + 1] == '1')
                    {
                        ch[i] = '1';
                        ch[i + 1] = '0';
                        break;
                    }
                }

                x_next = new string(ch);*/
            }

            if (x_prev != "-1")
            {
                Console.WriteLine(Convert.ToInt64(x_prev, 2).ToString());
            }
            else
            {
                Console.WriteLine(x_prev);
            }

            Console.WriteLine(Convert.ToInt64(x_next, 2).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;

            for (int i = 0; i < kettes.Length; i++)
            {
                if (kettes[i] == '1')
                {
                    count++;
                }
            }
            return count;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base27/50
1Accepted0/027ms20496 KiB
2Accepted0/028ms21060 KiB
3Partially correct1/227ms21356 KiB
4Accepted2/227ms21672 KiB
5Runtime error0/228ms20952 KiB
6Runtime error0/229ms21684 KiB
7Accepted2/227ms22844 KiB
8Accepted4/428ms22584 KiB
9Runtime error0/428ms21796 KiB
10Accepted4/428ms22764 KiB
11Accepted4/427ms23168 KiB
12Accepted4/427ms23180 KiB
13Partially correct2/427ms23688 KiB
14Accepted4/434ms25020 KiB
15Time limit exceeded0/4240ms32052 KiB
16Time limit exceeded0/4266ms11880 KiB
17Time limit exceeded0/4270ms11900 KiB