简体   繁体   中英

What are various methods to store very large integer value in a variable with less compilation time in C++ when doing operation on that variable

我应该如何处理此变量以使其在不下载任何新库的情况下存储大量变量。我正在谈论使用某种操作(例如哈希或数组)或我不知道的东西。

If you don't need to perform any operations on that variable and can't use any libraries, including the C++ standard library, then use

const char* x = "1119191991900234245239919234772376189636415308431";

else the next best thing to use is a

std::string x = "1119191991900234245239919234772376189636415308431";

Even elementary arithmetic can be performed on such an encoding, the digit value at position n in the string is x[n] - '0' .


But all this is really rather silly. Suggest you look at the big number library that's part of the Boost distribution. See www.boost.org.

For fun I've written something that works only on strings. By the way, that number you gave is awfully large number, it's something like a quintillion times the mass of our solar system in kg.

There are two methods. The first one adds one to the number and checks if it's a palindrome. This is a slow version, but can still works for numbers up to like about 16 digits in a reasonable time.

The second method is the method better way, it basically copies the left side of the number to the right side, it's pretty much instant. As the code is now you can run it through both to cross-reference the results.

I can't say it's fool-proof and I'm sure there's errors in it, but it seems to work, and I did have fun writing it. Also, if you're not allowed to use ANY libraries whatsoever, it's rather easy to refactor, just use raw strings and pass the size in the function.

#include <iostream>
#include <string>
#include <chrono>
#include <stdexcept>
#include <cstring>

using namespace std::chrono;
using namespace std;

auto startT = high_resolution_clock::now();
auto endT = high_resolution_clock::now();
double timeTaken;

#define STARTCLOCK startT = high_resolution_clock::now();
#define STOPCLOCK endT = high_resolution_clock::now();
#define PRINT_ELAPSED_TIME timeTaken = duration_cast<milliseconds>(endT - startT).count() / 1000.0; \
                            cout << "Process took " << timeTaken << " seconds\n\n";


void addOneTo(std::string& value)
{
    int64_t idx = value.size();

    do 
    {
        --idx;
        if (idx < 0) {
            memset(&value[0], '0', value.size());
            value.insert(value.begin(), '1');
            return;
        }
        value[idx] += char(1);
        if (value[idx] > '9') { value[idx] = '0'; }

    } while (value[idx] == '0');
}

bool isPalindrome(const std::string& number)
{
    const char* start = &number[0];
    const char* end = &number[number.size() - 1];

    while (start <= end)
    {
        if (*start != *end) return false;
        ++start;
        --end;
    }
    return true;
}

std::string getSmallestPalindromeByBruteForceBiggerThan(std::string num)
{
    if (num.empty()) throw std::runtime_error("Empty string");

    while (true)
    {
        addOneTo(num);
        if (isPalindrome(num)) return num;
    }
}


std::string getSmallestPalindromeOptimisedWayBiggerThan(std::string num)
{
    if (num.empty()) throw std::runtime_error("Empty string");

    addOneTo(num);
    if (num.size() == 1) return num;
    int64_t left;
    int64_t right;

    left = num.size() / 2 - 1;

    if (num.size() % 2 == 0)  right = num.size() / 2; 
    else right = num.size() / 2 + 1;

    if (num[left] < num[right])
    {
        ++num[left];
        num[right] = num[left];
    }

    for (; left >= 0 && right < num.size(); --left, ++right)
    {
        num[right] = num[left];
    }

    return num;
}

int main()
{
    string number = "60819750046451377";

    STARTCLOCK
    string palindrome = getSmallestPalindromeByBruteForceBiggerThan(number);

    cout << "____BRUTE FORCE____\n";
    cout << "Smallest palindrome = \n" << palindrome << '\n';

    STOPCLOCK
    PRINT_ELAPSED_TIME

    STARTCLOCK

    palindrome = getSmallestPalindromeOptimisedWayBiggerThan(number);

    cout << "____OPTIMISED____\n";
    cout << "Smallest palindrome = \n" << palindrome << '\n';

    STOPCLOCK
    PRINT_ELAPSED_TIME

    cin.ignore();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM