192052025-11-29 14:39:12szabelrAutókódoláscpp17Wrong answer 41/501ms540 KiB
// autokodolas_optimal.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <algorithm>
using namespace std;
int vissza(string n) {
    long long uj=0,c=1;
    for (int i = n.size() - 1; i >= 0; i--) {
        if(n[i]=='1')
         uj = uj + c;
        c = c * 2;
    }
    return uj;
}
int main()
{
    long long n;
    cin >> n;
    string kettes;
    while (n > 0) {
        if(n%2==0)
            kettes = '0' + kettes;
        else
            kettes = '1' + kettes;
        n = n / 2;
    }
    string kisebb = kettes;
    string nagyobb = kettes;
    int volte =0;
    for (int i = kisebb.size() - 1; i > 0; i--) {
        if (kisebb[i] == '0' and kisebb[i-1]=='1')
        {
            kisebb[i] = '1';
            kisebb[i-1] = '0';
            volte = 1;
            sort(kisebb.begin() + i, kisebb.end(), greater<char>());
            break;
        }
    }
    if (volte == 0)
    {
        kisebb.pop_back();
    }
    if (n == 1)
        cout << -1;
    else
        cout << vissza(kisebb)<<endl;
    volte = 0;
    for (int i = nagyobb.size() - 1; i > 0; i--) {
        if (nagyobb[i] == '1' and nagyobb[i - 1] == '0')
        {
            nagyobb[i] = '0';
            nagyobb[i - 1] = '1';
            volte = 1;
            sort(nagyobb.begin() + i, nagyobb.end());
            break;
        }
    }
    if (volte == 0)
    {
        nagyobb = '0' + nagyobb;
        for (int i = nagyobb.size() - 1; i > 0; i--) {
            if (nagyobb[i] == '1' and nagyobb[i - 1] == '0')
            {
                nagyobb[i] = '0';
                nagyobb[i - 1] = '1';
                sort(nagyobb.begin() + i, nagyobb.end());
                break;
            }
        }
    }
    cout << vissza(nagyobb) << endl;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base41/50
1Accepted0/01ms500 KiB
2Wrong answer0/01ms508 KiB
3Partially correct1/21ms316 KiB
4Accepted2/21ms316 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms540 KiB
8Accepted4/41ms500 KiB
9Accepted4/41ms508 KiB
10Accepted4/41ms316 KiB
11Accepted4/41ms316 KiB
12Accepted4/41ms316 KiB
13Wrong answer0/41ms316 KiB
14Accepted4/41ms316 KiB
15Accepted4/41ms316 KiB
16Accepted4/41ms316 KiB
17Wrong answer0/41ms316 KiB