Sunday, August 7, 2022
HomeWordPress DevelopmentClosest quantity to N having the given digit sum

Closest quantity to N having the given digit sum


  

#embody <bits/stdc++.h>

utilizing namespace std;

  

ulong toInt(const vector<int>& digits)

{

    ulong n = 0;

    for (int i = digits.measurement() - 1; i >= 0; i--)

        n = n * 10 + digits[i];

    return n;

}

  

ulong closest_number(ulong N, int D)

{

    vector<int> digits;

    ulong temp_num = N;

  

    int cur_sum = 0, temp_cur_sum, index;

    whereas (temp_num > 0) {

        int digit = temp_num % 10;

        digits.push_back(digit);

        cur_sum += digit;

        temp_num /= 10;

    }

    temp_cur_sum = cur_sum;

    if (D == cur_sum)

        return N;

  

    else if (D < cur_sum) {

        auto n1 = digits;

        for (index = 0; cur_sum - D >= n1[index]; index++) {

            cur_sum -= n1[index];

            n1[index] = 0;

        }

        n1[index] -= (cur_sum - D);

        auto n2 = digits;

        cur_sum = temp_cur_sum;

        for (index = 0; cur_sum >= D; index++) {

            cur_sum -= n2[index];

            n2[index] = 0;

        }

        if (index < n2.measurement())

            ++n2[index];

        else

            n2.push_back(1);

        ++cur_sum;

        for (int i = index; i < n2.measurement() && n2[i] > 9;

             i++) {

            n2[i] = 0;

            if (i == n2.measurement() - 1)

                n2.push_back(1);

            else

                ++n2[i + 1];

            cur_sum -= 9;

        }

        for (index = 0; D - cur_sum + n2[index] >= 10;

             index++) {

            cur_sum += (9 - n2[index]);

            n2[index] = 9;

        }

        n2[index] += D - cur_sum;

        if (N - toInt(n1) <= toInt(n2) - N)

            return toInt(n1);

        else

            return toInt(n2);

    }

    else {

        auto n1 = digits;

        for (index = 0; D - cur_sum + n1[index] >= 10;

             index++) {

            if (index == n1.measurement())

                n1.push_back(0);

            cur_sum += (9 - n1[index]);

            n1[index] = 9;

        }

        if (index == n1.measurement())

            n1.push_back(0);

        n1[index] += D - cur_sum;

        auto n2 = digits;

        int cur_sum = temp_cur_sum;

        for (index = 0; cur_sum < D && index < n2.measurement();

             index++) {

            cur_sum += (9 - n2[index]);

            n2[index] = 9;

        }

        --n2[index];

        --cur_sum;

        if (cur_sum < D)

            n2.clear();

        else {

            for (int i = index; i < n2.measurement() && n2[i] < 0;

                 i++) {

                n2[i] = 9;

                if (i == n2.measurement() - 1)

                    n2.pop_back();

                else

                    --n2[i + 1];

                cur_sum += 9;

            }

            for (index = 0; cur_sum - D >= n2[index];

                 index++) {

                cur_sum -= n2[index];

                n2[index] = 0;

            }

            n2[index] = 9 - (cur_sum - D);

        }

        if (n2.measurement() > 0 && N - toInt(n2) <= toInt(n1) - N)

            return toInt(n2);

        else

            return toInt(n1);

    }

}

  

int major()

{

    ulong N = 87220000221835856;

    int D = 102;

  

    

    cout << closest_number(N, D);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments