[纠错]用1+1/1!+1/2!+1/3!+、、、+1/n!计算e的近似值
#include<stdio.h>
float factorial(int n)
{ if(n==0) return 1;
else return n*factorial(n-1);
}
int main(void)
{ int i,c,m;
float e,sum=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
{c=1/(factorial(i));
sum=sum+c;
}
e=sum;
printf("e=%f\n",&e);
return 0;
}
[解决办法]
printf("e=%f\n",e);//输出的时候有错
[解决办法]
c=1.0/(factorial(i));//这儿用1.0,不要用1
[解决办法]
#include <stdlib.h>#include <stdio.h>float factorial(int n){ if(n==0 || n == 1) return 1; else return n*factorial(n-1);}int main(void){ int i,m; float e,sum = 0; scanf("%d",&m); for(i=1;i<=m;i++) { sum += 1.0/(factorial(i)); } e=sum; printf("e=%f\n", e); system("pause"); return 0;}
[解决办法]
常量也有类型!
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.
Syntax
integer-constant :
decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
'c-char-sequence'
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffixopt
long-suffix unsigned-suffixopt
unsigned-suffix : one of
u U
long-suffix : one of
l L
64-bit integer-suffix :
i64
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.
Syntax
floating-constant :
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
fractional-constant :
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part :
e signopt digit-sequence
E signopt digit-sequence
sign : one of
+ –
digit-sequence :
digit
digit-sequence digit
floating-suffix :one of
f l F L