-1
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
   ll n,k;
   int count=0;
   cin>>n>>k;
   for(int i=0;i<k;i++)
   {
       if(n%10!=0){
           n=n--;
       }
       else {
           n=n/10;
       }
   }
   cout<<n<<endl;
}

value of n isn't changing after performing decrement operation inside for loop

  • 2
    not sure whats a good duplicate. This is related: https://stackoverflow.com/questions/56179965/pre-vs-post-increment. You want `n--;` not `n = n--;` – 463035818_is_not_an_ai Jun 09 '21 at 07:27
  • 2
    `n=n--` is undefined behaviour. Aside: competitive programming reinforces bad habits that you will need to forget once you do any real programming. Never use `#include` or `using namespace std;` or `#define ll long long int`. Always use [`-Wall -Werror`](https://godbolt.org/z/b5f1EcTxb) – n. m. could be an AI Jun 09 '21 at 07:28
  • 1
    Decrement *or* assign - don't try to do both. Choose between `n--`, `--n`, `n -= 1`, and `n = n - 1`. (And do read about what they all mean.) – molbdnilo Jun 09 '21 at 07:35
  • This is terrible idea `#define ll long long int` especially when you can easily replace it with proper `typedef` or using directive – Slava Jun 09 '21 at 15:43

2 Answers2

3

While you decrease n with n--, you assign it back by

n = n--;

Just n--; is enough (cleaned-up code):

#include <iostream>

int main()
{
    long long n,k;
    std::cin >> n >> k;
    for(int i = 0; i < k; i++)
    {
        if (n % 10 != 0) { n--; }
        else { n /= 10; }
    }
    std::cout << n << '\n';
}

Input : 15 6

Output : 1

As usual, assigning and incrementing/decrementing should be done separately, or it will lead to undefined behavior. n -= 1, n = n-1 or --n will also do the job.

Also see

silverfox
  • 1,568
  • 10
  • 27
0

You are decrementing the value of N, but assigning it the old value. Replace:

n=n--;

with:

n--;
robthebloke
  • 9,331
  • 9
  • 12
  • "You are decrementing the value of N, but assigning it the old value." this is not quite correct statement – Slava Jun 09 '21 at 15:26