35312023-02-28 18:04:56DoraAutókódoláscsharpPartially correct 28/5028ms23496 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();
                int j = 0;
                int n_ones = oneBits;
                while ((j < x.Length - 1) && !(x[j] == '0' && x[j+1] == '1'))
                {
                    if (x[j] == '1')
                    {
                        n_ones--;
                    }
                    j++;
                }
                if (j < x.Length - 1) // ennek mindig igaznak kéne lennie
                {
                    ch[j] = '1';
                    ch[j+1] = '0';
                    n_ones--;

                    j += 2;
                    for (; j < x.Length - n_ones; j++)
                    {
                        ch[j] = '0';
                    }
                    for (; j < x.Length; j++)
                    {
                        ch[j] = '1';
                    }
                }
                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
base28/50
1Accepted0/027ms20540 KiB
2Accepted0/027ms21076 KiB
3Partially correct1/227ms21360 KiB
4Accepted2/227ms21628 KiB
5Runtime error0/228ms21424 KiB
6Runtime error0/228ms21084 KiB
7Partially correct1/227ms22052 KiB
8Partially correct2/428ms22516 KiB
9Runtime error0/428ms21708 KiB
10Partially correct2/428ms22748 KiB
11Accepted4/427ms23408 KiB
12Partially correct2/428ms23460 KiB
13Partially correct2/427ms23056 KiB
14Partially correct2/427ms23496 KiB
15Accepted4/427ms23268 KiB
16Partially correct2/426ms23252 KiB
17Accepted4/428ms23244 KiB