-8

I have this integer:

4732891432890432432094732089174839207894362154

It's big so I want to delete all digits in it except the digit 4 and I don't know how. This is my code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
   unsigned ll n, lastDigit, count = 0;
    ll t;
    cin >> t;
    while(t--)
    {
        cin >> n;

        while (n !=0)
        {
            lastDigit = n % 10;
            if(lastDigit == 4)
                count++;
            n /= 10;
        }
        cout << count << "\n";
    }
     
    return 0;
}

I used the while loop because I have multiple test case not only that number.

273K
  • 29,503
  • 10
  • 41
  • 64
OUTSEET
  • 1
  • 2
  • 1
    When I see any of `#include using namespace std; #define ll long long`, I vote down three bad habits in a question. – 273K Dec 31 '22 at 16:41
  • Again `cin >> t;` will not work. You can not use an integer type for this. The largest possible integer in current c++ implementations is too small to store `4732891432890432432094732089174839207894362154` – drescherjm Dec 31 '22 at 16:41
  • @273k why what is the problem :( – OUTSEET Dec 31 '22 at 16:43
  • 4
    At StackOverflow many of us are professional software developers. `#include ` is very unprofessional. If you show that on a job interview with me you will absolutely not get the job. – drescherjm Dec 31 '22 at 16:44
  • 3
    @OUTSEET: (1). [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) (2). [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/12149471) – Andreas Wenzel Dec 31 '22 at 16:45
  • 2
    Even when doing so-called "competitive" programming, use good habits. Good habits leads to good code. And good code is much easier to test and debug. – Some programmer dude Dec 31 '22 at 16:46
  • As for your problem, it becomes trivial if you use *strings* instead. Though what your code is doing isn't "removing" digits, it's *counting* the digit `4`. Which also is trivial when using strings (in fact it's a single call to [a standard function](https://en.cppreference.com/w/cpp/algorithm/count)). – Some programmer dude Dec 31 '22 at 16:46
  • @drescherjm i am 16 so no job in 6 years i am just practicing for future contest and also learning c++ – OUTSEET Dec 31 '22 at 16:48
  • 3
    Please don't use so-called "competition" or "judge" sites to learn programming or languages. That's not their purposes. The examples typically are bad, with bad habits, bad code, and sometimes even *invalid* code. If you can then please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). They might seem expensive right now, but will pay of in the long run. A good book is worth its weight in gold (or other precious metals). – Some programmer dude Dec 31 '22 at 16:51
  • 2
    Point is, you learn what you do. If you do competitive programming, you learn competitive programming, which is different from normal programming. I would recommend doing some real-world projects on your own. *"i am 16 so no job in 6 years"* Good, this gives you extra time. :P – HolyBlackCat Dec 31 '22 at 16:53
  • 3
    @OUTSEET An important side effect of using those bad coding habits, that you are experiencing just now: people focus on commenting this bad coding style and down vote, and nearly nobody actually answers the real question. – prapin Dec 31 '22 at 16:57
  • 3
    You might as well delete [your previous question](https://stackoverflow.com/questions/74970211/find-the-number-of-time-you-can-skip-a-number) as this is effectively a duplicate. – Blastfurnace Dec 31 '22 at 17:03
  • For starters, please **remove** these two nonsensical lines from your code: (1) `#include `, (2) `using namespace std;`. – Andrej Podzimek Dec 31 '22 at 18:49
  • 1
    @OUTSEET `#define ll long long` -- This macro is totally unnecessary since there is `int64_t` that is available. It isn't that difficult to type 7 letters. – PaulMcKenzie Dec 31 '22 at 18:52
  • 1
    [Please do not post the same question multiple times.](https://stackoverflow.com/questions/74970211/find-the-number-of-time-you-can-skip-a-number) – Andrej Podzimek Dec 31 '22 at 18:55

1 Answers1

3

Just to show you current C++ (C++20) works a bit different then wat most (older) C++ material teaches you.

#include <algorithm>
#include <iostream>
#include <string>
#include <ranges>

bool is_not_four(const char digit)
{
    return digit != '4';
}

int main()
{
    // such a big number iwll not fit in any of the integer types 
    // needs to be stored in memory
    std::string big_number{ "4732891432890432432094732089174839207894362154" };

    // or input big_number from std::cin
    
    //  std::cout >> "input number : "
    //   std::cin >> big_number;
    
    // NEVER trust user input
    if (!std::any_of(big_number.begin(), big_number.end(), std::isdigit))
    {
        std::cout << "input should only contain digits";
    }
    else
    {
        // only loop over characters not equal to 4
        for (const char digit : big_number | std::views::filter(is_not_four))
        {
            std::cout << digit;
        }

        // you can also remove characters with std::remove_if
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19