33592023-02-27 11:10:02CWMAutókódoláscpp17Hibás válasz 0/503ms4088 KiB
// 0227.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
string ConvertToBits(ll a) {
	ll divisor = pow(2, 41);
	string s = "";
	for (size_t i = 0; i < 42; i++)
	{
		if (a - divisor >= 0) {
			s += '1';
			a -= divisor;
		}
		else s += '0';
		divisor/=2;
	}
	return s;
}
string FindNext(string cur) {
	int oc = 0;
	bool hadOne = false;
	for (int i = 41; i >= 0; i--)
	{
		if (cur[i] == '0' && hadOne) {
			cur[i] = '1';
			for (int j = i+1; j < 42; j++)
			{
				if (j > 42-oc) cur[j] = '1';
				else cur[j] = '0';
			}
			return cur;
		}
		else if (cur[i] == '1') {
			hadOne = true;
			oc++;
		}
	}
}
string FindPrevious(string cur) {
	for (int i = 41; i > 0; i--)
	{
		if (cur[i] == '0' && cur[i - 1] == '1') {
			cur[i] = '1';
			cur[i - 1] = '0';
			return cur;
		}
	}
	return "-1";
}
ll Reconvert(string cur) {
	if (cur == "-1") return -1;
	ll val = 0;
	ll addition = 1;
	for (int i = 41; i >= 0; i--)
	{
		if (cur[i] == '1') val += addition;
		addition *= 2;
	}
	return val;
}
int main()
{
	string c = ConvertToBits(4294967296);
	string n = FindNext(c);
	string p = FindPrevious(c);
	ll nI = Reconvert(n);
	ll pI = Reconvert(p);
	cout << pI << "\n" << nI;
}

// 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
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Hibás válasz0/03ms1808 KiB
2Elfogadva0/03ms2336 KiB
3Hibás válasz0/23ms2388 KiB
4Hibás válasz0/23ms2508 KiB
5Hibás válasz0/23ms2760 KiB
6Hibás válasz0/23ms2780 KiB
7Hibás válasz0/23ms2920 KiB
8Hibás válasz0/43ms3120 KiB
9Hibás válasz0/43ms3284 KiB
10Hibás válasz0/43ms3520 KiB
11Hibás válasz0/43ms3576 KiB
12Hibás válasz0/43ms3776 KiB
13Hibás válasz0/43ms3864 KiB
14Hibás válasz0/43ms3876 KiB
15Hibás válasz0/43ms3876 KiB
16Hibás válasz0/43ms4004 KiB
17Hibás válasz0/42ms4088 KiB