标题:大佬们萌新求助
只看楼主
miop
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-8
结帖率:80%
已结贴  问题点数:10 回复次数:2 
大佬们萌新求助
#include<stdio.h>
int main()
{
    double x,y,z,a,b,c;
    scanf("%lf",&x);
    b = 1;
    y = 1;
    for(z=1;z<=x;z++){
        for(a=1;a<=z;a++){
            b = b*a;
        }
    c = 1/b;
    y = y +c;
    }
    printf("%.8lf\n",y);
    return 0;
 }
这是我想求e的近似值 用的1+1/1!+1/2!+1/3!+... 但是问题是1和2都还好
到3的时候立马变成了1/12?输出的是2.58333333 应该是2.67777777
搜索更多相关主题的帖子: 近似 for 输出 return printf 
2021-09-11 21:17
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:7 
https://www.

//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

int main(int argc, char *argv[])
{
    double x, y, z, a, b;

    for(x= 3, y = 1, z = 1; z <= x; z++) {
        b = 1;
        for(a = 1; a <= z; a++) b = b * a;
        b = 1 / b;
        y = y + b;
    }
    printf("%.8lf\n",y);
    
    return 0;
}
2021-09-11 21:45
miop
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-8
得分:0 
回复 2楼 自由而无用
大佬知道是啥原因吗
2021-09-11 21:49
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <stdlib.h>

//#define SUB1
#ifdef SUB1
int sub1(int argc, char *argv[])
{
    double x, y, z, a, b, c;
    x = 3.0;
    b = 1;
    y = 1;
    for(z = 1; z <= x; z++) {
        for(a = 1; a <= z; a++){
            b = b * a;
        }
        c = 1 / b;
        y = y + c;
    }
    printf("%.8lf\n",y);
    
    return 0;
}
#else
int sub2(int argc, char *argv[])
{
    double x, y, z, a, b;

    for(x= 3, y = 1, z = 1; z <= x; z++) {
        b = 1;
        for(a = 1; a <= z; a++) b = b * a;
        b = 1 / b;
        y = y + b;
    }
    printf("%.8lf\n",y);
    
    return 0;
}
#endif

int main(int argc, char *argv[])
{
    system("ls");
#ifdef SUB1
    system("gcc -E *.c -o va.e");
#else
    system("gcc -E *.c -o vb.e");
#endif
    system("ls");
    system("diff va.e vb.e");
    
    return 0;
}
2021-09-11 22:26
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
回复 3楼 miop
check different parts, good luck!
linux diff result:
------------------------------------------------
9028259.c
9028259.out
9028259.txt
va.e
vb.e
9028259.c
9028259.out
9028259.txt
va.e
vb.e
1c1
< # 1 "4218938.c"
---
> # 1 "9028259.c"
6c6
< # 1 "4218938.c"
---
> # 1 "9028259.c"
847c847
< # 2 "4218938.c" 2
---
> # 2 "9028259.c" 2
1909c1909,1910
< # 3 "4218938.c" 2
---
> # 3 "9028259.c" 2
> # 24 "9028259.c"
1911,1915c1912,1913
<
<
<
< # 6 "4218938.c"
< int sub1(int argc, char *argv[])
---
> # 24 "9028259.c"
> int sub2(int argc, char *argv[])
1917,1926c1915,1921
<     double x, y, z, a, b, c;
<     x = 3.0;
<     b = 1;
<     y = 1;
<     for(z = 1; z <= x; z++) {
<         for(a = 1; a <= z; a++){
<             b = b * a;
<         }
<         c = 1 / b;
<         y = y + c;
---
>     double x, y, z, a, b;
>
>     for(x= 3, y = 1, z = 1; z <= x; z++) {
>         b = 1;
>         for(a = 1; a <= z; a++) b = b * a;
>         b = 1 / b;
>         y = y + b;
1932c1927,1928
< # 40 "4218938.c"
---
>
>
1937c1933,1935
<     system("gcc -E *.c -o va.e");
---
>
>
>     system("gcc -E *.c -o vb.e");
1939a1938
>     system("diff va.e vb.e");
1940a1940
>     return 0;
2021-09-11 22:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
程序代码:
#include <stdio.h>
#include <assert.h>

// 计算 1/0! + 1/1! + 1/2! + 1/3! + ... + 1/n!
int main( void )
{
    unsigned n;
    if( scanf("%u",&n) != 1 )
        return 1; // 输入错误
     if( n > 20 )
        return 1; // 溢出,因为 21! 需要的位数超过64bits

    double e = 1;
    unsigned long long factorial = 1;
    for( unsigned i=0; i!=n; ++i )
    {
        factorial *= i+1;
        e += 1.0/factorial;
    }
    printf( "%.8f\n", e );
}
2021-09-12 11:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506821-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.111978 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved