首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎的输出~

2012-08-16 
怎样输出~~~Description用高精度计算出S1!+2!+3!+…+n!(n≤50)其中“!”表示阶乘,例如:5!5*4*3*2*1。Input只

怎样输出~~~
Description 

用高精度计算出S=1!+2!+3!+…+n!(n≤50) 
其中“!”表示阶乘,例如:5!=5*4*3*2*1。 


Input 

只有一行,一个正整数n。

Output 

只有一行,一个正整数S。

Sample Input 


48


Sample Output 


12678163798554051767172643373255731925167694226950680420940313



我的代码:
#include "stdafx.h"
#include "iostream"
using namespace std;

int main(int argc, char* argv[])
{
printf("Hello World!\n");

int n,t = 1;
double sum = 0;
cin>>n;

for(int i = 1;i <= n;i++)
{
t = t * i;
sum +=t; 
}
cout<<sum<<endl;
return 0;
}
输入48时 输出的是
-1.25962e+008


[解决办法]

C/C++ code
#include <iostream>#include <string>using namespace std;string mul(string multiplicand, string multiplier){    string rst;    int i = multiplicand.size();    int j = multiplier.size();    int k;    int rstlen = i + j;    rst.append(rstlen, 0);    for(i = multiplicand.size() - 1; i >= 0; --i)    {        for(j = multiplier.size() - 1; j >= 0; --j)        {            if(rst.at(i+j+1) > 46)//防止溢出            {                rst.at(i+j) += rst.at(i+j+1) / 10;                rst.at(i+j+1) = rst.at(i+j+1) % 10;            }            rst.at(i+j+1) += (multiplicand.at(i) - '0') * (multiplier.at(j) - '0');        }    }    //进位    for(k = rstlen - 1; k > 0; --k)    {        if(rst.at(k) >= 10)        {            rst.at(k-1) += rst.at(k) / 10;            rst.at(k) = rst.at(k) % 10;        }        rst.at(k) += '0';    }        if(rst.at(0) == 0)        rst.erase(0, 1);    else        rst.at(0) += '0';    return rst;}string add(string summand, string addend){    int i, j;        int rstlen;    if(summand.size() < addend.size())    {        string tmp;        tmp = addend;        addend = summand;        summand = tmp;    }        i = summand.size() - 1;    rstlen = i;    j = addend.size() - 1;    while(j >= 0)    {        summand.at(i) += (addend.at(j) - '0');                --i;        --j;    }    while(rstlen > 0)    {        if(summand.at(rstlen) >= '0' + 10)        {            ++summand.at(rstlen-1);            summand.at(rstlen) = (summand.at(rstlen) - '0') % 10 + '0';        }        --rstlen;    }    if(summand.at(0) >= '0' + 10)    {        summand.at(0) = (summand.at(0) - '0') % 10 + '0';        summand.insert(summand.begin(), '1');    }    return summand;}string factorial(string str){    string rst, tmp;    rst = "1";    tmp = "0";    while(str != tmp)    {        ++tmp.at(tmp.size() - 1);        if(tmp.at(0) == '0' + 10 && tmp.size() == 1)        {            tmp.at(0) = '0';            tmp.insert(tmp.begin(), '1');        }        for(int i = tmp.size() - 1; i > 0; --i)        {            if(tmp.at(i) >= '0' + 10)            {                ++tmp.at(i-1);                tmp.at(i) = (tmp.at(i) - '0') % 10 + '0';            }        }        rst = mul(rst, tmp);    }    return rst;}int main(){    string rst = "0", num;    cin>>num;    while(num.size() >= 1 && num != "0")    {        rst = add(rst, factorial(num));        --num.at(num.size()-1);        for(int i = num.size() - 1; i > 0; --i)        {            if(num.at(i) < '0')            {                --num.at(i-1);                num.at(i) += 10;            }        }        if(num.at(0) == '0')            num.erase(0, 1);                    }    cout<<rst<<endl;        system("pause");    return 0;} 

热点排行