1
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int nine = floor(((n*4)-n)/4);
        int pos = 0;
        int ans=0;
        for(int i=0;i<nine;i++){
            ans = ans + 9*pow(10,pos);
            cout<<ans<<'\n';
            pos++;
        }
        
        cout<<ans<<'\n';
        
    }
    return 0;
    

}

this is my code. I wrote this as a solution for cp problem. in there if i give the value of n ,

6

the answer is

9998

but it should be giving 9999. can anybody tell me what is wrong here?

Sanjayk
  • 11
  • 1
  • Out of interest, try removing `#include `, and `using namespace std`. Write `#include ` instead, then try `std::pow`. Personally I consider an implementation of `std::pow` that goes off for integer arguments to be defective. – Bathsheba Sep 10 '20 at 13:37
  • @Bathsheba why should that be defective? `Promoted pow ( Arithmetic1 base, Arithmetic2 exp );` : `If any argument has integral type, it is cast to double. If any argument is long double, then the return type Promoted is also long double, otherwise the return type is always double.` ([std::pow](https://en.cppreference.com/w/cpp/numeric/math/pow)) I don't have the corresponding part for the specs at hand but it's described there the same way. – t.niese Sep 10 '20 at 14:37
  • @t.niese all integers up to 2^mantissa are exactly representable as `double`, which includes all the values used here – Caleth Sep 10 '20 at 14:47
  • aside: why `((n*4)-n)/4` instead of `(3 * n) / 4`? – Caleth Sep 10 '20 at 14:49
  • @Caleth Yes I'm aware of that. But then the implementation would need to limit itself to a possibly slower algorithm/implementation, e.g. one that determines at runtime if the value stored in the double represents an exactly as double representable integral type. It would be better if they would provide a `pow` for integral types. – t.niese Sep 10 '20 at 15:02
  • @t.niese no, the expectation is that `std::pow(10.0, 0.0)` be exactly `1.0`, `std::pow(4.0, 0.5)` be exactly `2.0`, etc – Caleth Sep 10 '20 at 15:06
  • Change `int pos = 0;` to `int pos = 1;`. Change `pow(10, pos)` to `pos`. Change `pos++;` to `pos *= 10;`. – Pete Becker Sep 10 '20 at 16:30

0 Answers0