# include
# include
using namespace std;
Int main ()
{
Unsigned long long n;
Double k=0.0;
Cin> n;
For (unsigned long long I=1; i<=n; I++) {
Unsigned long long v=1;
For (unsigned long long j=1; j<=I; J++) {
V *=j;
}
K +=1.0/v;
}
Printf (" %. 10 f \ n ", k);
return 0;
}
The problem is where I do wrong?
O give directions!
I write on their own compiler 10, are the result of 2.7182787698, 15, 2.7182818288, 2, 1.5000000000, yes
Why didn't to this problem is a test point?
CodePudding user response:
Have you set your initial value of k is 1.0CodePudding user response:
In addition, unsigned long long not necessary, and to all the int, although does not affect the result, but this didn't asked for such a large integer types, are you afraid of v integer value is not big enough, it will be v type into a double line,Specific you can look at the code
# include
# include
using namespace std;
Int main ()
{
int n;
Double k=1.0;
Cin & gt;> n;
For (int I=1; i <=n; I++) {
A double v=1.0;
For (int j=1; J & lt;=I; J++) {
V *=j;
}
K +=1.0/v;
}
Printf (" %. 10 f \ n ", k);
return 0;
}
CodePudding user response:
Wrong is wrong, when the input 2,1 + 1/1! + 1/2! Rather than 2.5=1.5
CodePudding user response:
According to your codeThe results are as follows:
1
1.0000000000
2
1.5000000000
3
1.6666666667
4
1.7083333333
5
1.7166666667
6
1.7180555556
7
1.7182539683
8
1.7182787698
9
1.7182815256
10
1.7182818011