用公式e=1+1/1!+1/2!+1/3!+1/4!···

popowa 2016-09-13 09:50:51
#include<stdio.h>
void main()
{int i,j,n;
double e,fac=1;
for(e=0,i=1;1/fac>1e-6;i++,e=e+1/fac)
{for(fac=1,j=1;j<=i;fac=fac*j,j++)
;}
printf("%.20lf\n",e+1);}

定义数据fac类型的时候没有赋初值得出的结果总是1.000···是为什么啊?
为什么第二个for那里赋值不能用?


怎么样才能只一次赋值?两次感觉怪怪的···
表取笑小新人
...全文
2697 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-09-14
  • 打赏
  • 举报
回复
仅供参考:
// 按下列数级计算e的近视值,使e有小数点后1000位有效数字。
// e=1+1/1!+1/2!+1/3!+......1/n!
#include <iostream>
#include <string>
using namespace std;
inline int compare(string str1,string str2) {//相等返回0,大于返回1,小于返回-1
         if (str1.size()>str2.size()) return 1; //长度长的整数大于长度小的整数
    else if (str1.size()<str2.size()) return -1;
    else                              return str1.compare(str2); //若长度相等,则头到尾按位比较
}
string SUB_INT(string str1,string str2);
string ADD_INT(string str1,string str2) {//高精度加法
    int sign=1; //sign 为符号位
    string str;
    if (str1[0]=='-') {
        if (str2[0]=='-') {
            sign=-1;
            str=ADD_INT(str1.erase(0,1),str2.erase(0,1));
        } else {
            str=SUB_INT(str2,str1.erase(0,1));
        }
    } else {
        if (str2[0]=='-') {
            str=SUB_INT(str1,str2.erase(0,1));
        } else { //把两个整数对齐,短整数前面加0补齐
            string::size_type L1,L2;
            int i;
            L1=str1.size();
            L2=str2.size();
            if (L1<L2) {
                for (i=1;i<=L2-L1;i++) str1="0"+str1;
            } else {
                for (i=1;i<=L1-L2;i++) str2="0"+str2;
            }
            int int1=0,int2=0; //int2 记录进位
            for (i=str1.size()-1;i>=0;i--) {
                int1=(int(str1[i])-'0'+int(str2[i])-'0'+int2)%10;
                int2=(int(str1[i])-'0'+int(str2[i])-'0'+int2)/10;
                str=char(int1+'0')+str;
            }
            if (int2!=0) str=char(int2+'0')+str;
        }
    }
    //运算后处理符号位
    if ((sign==-1)&&(str[0]!='0')) str="-"+str;
    return str;
}
string SUB_INT(string str1,string str2) {//高精度减法
    int sign=1; //sign 为符号位
    string str;
    int i,j;
    if (str2[0]=='-') {
        str=ADD_INT(str1,str2.erase(0,1));
    } else {
        int res=compare(str1,str2);
        if (res==0) return "0";
        if (res<0) {
            sign=-1;
            string temp =str1;
            str1=str2;
            str2=temp;
        }
        string::size_type tempint;
        tempint=str1.size()-str2.size();
        for (i=str2.size()-1;i>=0;i--) {
            if (str1[i+tempint]<str2[i]) {
                j=1;
                while (1) {//zhao4zhong1添加
                    if (str1[i+tempint-j]=='0') {
                        str1[i+tempint-j]='9';
                        j++;
                    } else {
                        str1[i+tempint-j]=char(int(str1[i+tempint-j])-1);
                        break;
                    }
                }
                str=char(str1[i+tempint]-str2[i]+':')+str;
            } else {
                str=char(str1[i+tempint]-str2[i]+'0')+str;
            }
        }
        for (i=tempint-1;i>=0;i--) str=str1[i]+str;
    }
    //去除结果中多余的前导0
    str.erase(0,str.find_first_not_of('0'));
    if (str.empty()) str="0";
    if ((sign==-1) && (str[0]!='0')) str ="-"+str;
    return str;
}
string MUL_INT(string str1,string str2) {//高精度乘法
    int sign=1; //sign 为符号位
    string str;
    if (str1[0]=='-') {
        sign*=-1;
        str1 =str1.erase(0,1);
    }
    if (str2[0]=='-') {
        sign*=-1;
        str2 =str2.erase(0,1);
    }
    int i,j;
    string::size_type L1,L2;
    L1=str1.size();
    L2=str2.size();
    for (i=L2-1;i>=0;i--) { //模拟手工乘法竖式
        string tempstr;
        int int1=0,int2=0,int3=int(str2[i])-'0';
        if (int3!=0) {
            for (j=1;j<=(int)(L2-1-i);j++) tempstr="0"+tempstr;
            for (j=L1-1;j>=0;j--) {
                int1=(int3*(int(str1[j])-'0')+int2)%10;
                int2=(int3*(int(str1[j])-'0')+int2)/10;
                tempstr=char(int1+'0')+tempstr;
            }
            if (int2!=0) tempstr=char(int2+'0')+tempstr;
        }
        str=ADD_INT(str,tempstr);
    }
    //去除结果中的前导0
    str.erase(0,str.find_first_not_of('0'));
    if (str.empty()) str="0";
    if ((sign==-1) && (str[0]!='0')) str="-"+str;
    return str;
}
string DIVIDE_INT(string str1,string str2,int flag) {//高精度除法。flag==1时,返回商; flag==0时,返回余数
    string quotient,residue; //定义商和余数
    int sign1=1,sign2=1;
    if (str2 == "0") {  //判断除数是否为0
        quotient= "ERROR!";
        residue = "ERROR!";
        if (flag==1) return quotient;
        else         return residue ;
    }
    if (str1=="0") { //判断被除数是否为0
        quotient="0";
        residue ="0";
    }
    if (str1[0]=='-') {
        str1   = str1.erase(0,1);
        sign1 *= -1;
        sign2  = -1;
    }
    if (str2[0]=='-') {
        str2   = str2.erase(0,1);
        sign1 *= -1;
    }
    int res=compare(str1,str2);
    if (res<0) {
        quotient="0";
        residue =str1;
    } else if (res == 0) {
        quotient="1";
        residue ="0";
    } else {
        string::size_type L1,L2;
        L1=str1.size();
        L2=str2.size();
        string tempstr;
        tempstr.append(str1,0,L2-1);
        for (int i=L2-1;i<L1;i++) { //模拟手工除法竖式
            tempstr=tempstr+str1[i];
            tempstr.erase(0,tempstr.find_first_not_of('0'));//zhao4zhong1添加
            if (tempstr.empty()) tempstr="0";//zhao4zhong1添加
            for (char ch='9';ch>='0';ch--) { //试商
                string str;
                str=str+ch;
                if (compare(MUL_INT(str2,str),tempstr)<=0) {
                    quotient=quotient+ch;
                    tempstr =SUB_INT(tempstr,MUL_INT(str2,str));
                    break;
                }
            }
        }
        residue=tempstr;
    }
    //去除结果中的前导0
    quotient.erase(0,quotient.find_first_not_of('0'));
    if (quotient.empty()) quotient="0";
    if ((sign1==-1)&&(quotient[0]!='0')) quotient="-"+quotient;
    if ((sign2==-1)&&(residue [0]!='0')) residue ="-"+residue ;
    if (flag==1) return quotient;
    else         return residue ;
}
string DIV_INT(string str1,string str2) {//高精度除法,返回商
    return DIVIDE_INT(str1,str2,1);
}
string MOD_INT(string str1,string str2) {//高精度除法,返回余数
    return DIVIDE_INT(str1,str2,0);
}
int main() {
    string s1,s2,s3,s4;
    int i;

    s4="0";

    s3="1";
    for (i=0;i<1002;i++) s3+="0";

    s1="1";
    s2="1";

    s4=ADD_INT(s4,DIV_INT(s3,s1));
    s4=ADD_INT(s4,DIV_INT(s3,s1));

    for (i=2;i<451;i++) {//因为450!是一个1001位数
        s2=ADD_INT(s2,string("1"));
        s1=MUL_INT(s1,s2);
        s4=ADD_INT(s4,DIV_INT(s3,s1));
    }

    cout<<s4<<endl;

    return(0);
}
//271828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992
//181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924
//476146066808226480016847741185374234544243710753907774499206955170276183860626133138458300075204493382656029760673711320
//070932870912744374704723069697720931014169283681902551510865746377211125238978442505695369677078544996996794686445490598
//793163688923009879312773617821542499922957635148220826989519366803318252886939849646510582093923982948879332036250944311
//730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125099618188159304169035
//159888851934580727386673858942287922849989208680582574927961048419844436346324496848756023362482704197862320900216099023
//530436994184914631409343173814364054625315209618369088870701676839642437814059271456354906130310720851038375051011574770
//4171898610687396965521267154688957035035174
小灸舞 版主 2016-09-14
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
lm_whales 2016-09-14
  • 打赏
  • 举报
回复
你是每次重新求 n! 当然要每次初始化了 每次迭代,求出 当前 i 的阶乘,就不用每次初始化了。 阶乘 的递推关系是 (n+1)! =n!*(n+1); 迭代,和递归。都要求,了解 递推关系。 有了递推关系,按照它,写就成了 e =1 //初始化为1 ,0!=1 +1/1! //1!=1 +1/2! //(n+1)! =n!*(n+1) +...... //...... ------------------------- 一元n次 方程 数值方法求根 必须保证计算方法,在区间内 收敛 不然求不出来
paschen 2016-09-14
  • 打赏
  • 举报
回复
没赋初值就使用,这个值可能是个随机值 其他问题不懂你意思
popowa 2016-09-13
  • 打赏
  • 举报
回复
额,第二个好像就是单调函数,所以只有一个零点,只有零一个零点···所以facs(x-y)改成facs(z)就能得出零。······ 诶,我也不知道,求指正
popowa 2016-09-13
  • 打赏
  • 举报
回复
#include<stdio.h> #include<math.h> void main() {float a,b,c,x,y,z; x=2*a*a*a-4*a*a+3*a; y=2*b*b*b-4*b*b+3*b; for(a=-20,b=10;fabs(x-y)>1e-6;) {x=2*a*a*a-4*a*a+3*a; y=2*b*b*b-4*b*b+3*b; c=(a+b)/2; z=2*c*c*c-4*c*c+3*c; if(x*z<0) b=(a+b)/2; if(z*y<0) a=(a+b)/2; if(x*y>0) {printf("please input a,b(f(a)*f(b)<0:"); scanf("%f,%f",&a,&b);} } printf("%f\n",c); } 还有这个,······求方程2x^3-4x^2+3x=0在(-10,10)附近的根,用二分法

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧