C语言程序 一个阶乘求和 程序

100!+99!+98!+97!+96!+95!+。。。。。。+2!+1... 100!+99!+98!+97!+96!+95!+。。。。。。+2!+1 展开
 我来答
井钰gQ
推荐于2018-03-22 · TA获得超过2480个赞
知道大有可为答主
回答量:1303
采纳率:0%
帮助的人:1497万
展开全部
参考下把,是C++的,高精度的,把它转换为C语言的即可

#include <iostream>
#include <string>
using namespace std;
template<int N>
struct BigNum {
int d[N];
int len;
BigNum()
{
len = 0;
}
BigNum(const char *);
BigNum<N> & operator=(int );
BigNum<N> & operator=(BigNum<N> & );
BigNum<N> & operator *=(int);
BigNum<N> & operator +=(int);
BigNum<N> & operator +=(BigNum<N> & );
BigNum<N> & operator /=(int);
int cmp(BigNum<N> &);
template<int O>
friend ostream& operator << (ostream& ,BigNum<O>&);
};
template<int N>
BigNum<N>::BigNum(const char *s){
len = strlen(s);
int t = len;
while(t){
d[len -t] = s[t-1] -'0';
t --;
}
}
template<int N>
BigNum<N> & BigNum<N>::operator=(int n){
len = 0;
do {
d[len++] = n%10;
n /= 10;
} while(n>0);
return *this;
}

template<int N>
BigNum<N> & BigNum<N>::operator=(BigNum<N> & rhs){
len = rhs.len;
for(int i=0;i<len;i++){
d[i] = rhs.d[i];
}
return *this;
}

template<int N>
BigNum<N> & BigNum<N>::operator *=(int m){
int carry = 0;
int i;
for(i=0;i<len;i++){
carry += d[i] * m;
d[i] = carry % 10;
carry /= 10;
}
while(carry){
d[len++] = carry % 10;
carry /= 10;
}
return *this;
}
template<int N>
BigNum<N> & BigNum<N>::operator +=(int m){
int carry = 0;
int i;
carry = d[0] + m;
d[0] = carry%10;
carry /= 10;
for( i = 1; i < len && carry; i++)
{
d[i] += carry%10;
carry /= 10;
}
while(carry){
d[len++] = carry % 10;
carry /= 10;
}
return *this;
}
template<int N>
BigNum<N> & BigNum<N>::operator +=(BigNum<N> & rhs){
int i, j, carry = 0;
for( i = 0, j = 0; i < len && j < rhs.len; i ++, j++)
{
d[i] += rhs.d[i] + carry;
carry = d[i]/10;
d[i] %= 10;
}
if(i < len)
{
if(carry)
{
for(; i < len&&carry; i ++){
d[i] += carry;
carry = d[i]/10;
d[i] %= 10;
}
}
if(carry)
d[i] = carry, len ++;
}
else if(j < rhs.len)
{
for( ; j < rhs.len; j ++){
d[j] = rhs.d[j] + carry;
carry = d[j]/10;
d[j] %= 10;
}
if(carry)
d[j] = carry, len = rhs.len +1;
else
len = rhs.len;
}
else
{
if(carry) d[i] = carry, len++;
}
return *this;
}
template<int N>
BigNum<N> & BigNum<N>::operator /=(int dm){
int carry = 0;
int i;
for(i=len-1;i>=0;i--){
carry *= 10;
carry += d[i];
d[i] = carry / dm;
carry = carry % dm;
}
while(d[--len] ==0){
;
}
len ++;
return *this;
}

template<int N>
int BigNum<N>::cmp(BigNum<N> &rhs){
if(len != rhs.len)
return len - rhs.len;
for(int i= len-1;i>=0;i--){
if(d[i] != rhs.d[i])
return d[i] - rhs.d[i];
}
return 0;
}

template<int N>
ostream& operator << (ostream& o, BigNum<N>& rhs){
for(int i=rhs.len-1;i>=0;i--){
o << rhs.d[i];
}
return o;
}
int main ()
{
int i, j, n;
BigNum <300> sum, temp;
while( cin >> n)
{
for( i = 1; i <= n; i ++)
{
temp = 1;
for( j = 1; j <= i; j ++)
temp *= j;
sum += temp;
}
for( i = sum.len-1; i >= 0; i --)
cout << sum.d[i];
cout << endl;
}
return 1;
}
卞默黎运珹
2020-05-12 · TA获得超过1.2万个赞
知道小有建树答主
回答量:3155
采纳率:34%
帮助的人:244万
展开全部
参考下把,是C++的,高精度的,把它转换为C语言的即可
#include
<iostream>
#include
<string>
using
namespace
std;
template<int
N>
struct
BigNum
{
int
d[N];
int
len;
BigNum()
{
len
=
0;
}
BigNum(const
char
*);
BigNum<N>
&
operator=(int
);
BigNum<N>
&
operator=(BigNum<N>
&
);
BigNum<N>
&
operator
*=(int);
BigNum<N>
&
operator
+=(int);
BigNum<N>
&
operator
+=(BigNum<N>
&
);
BigNum<N>
&
operator
/=(int);
int
cmp(BigNum<N>
&);
template<int
O>
friend
ostream&
operator
<<
(ostream&
,BigNum<O>&);
};
template<int
N>
BigNum<N>::BigNum(const
char
*s){
len
=
strlen(s);
int
t
=
len;
while(t){
d[len
-t]
=
s[t-1]
-'0';
t
--;
}
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator=(int
n){
len
=
0;
do
{
d[len++]
=
n%10;
n
/=
10;
}
while(n>0);
return
*this;
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator=(BigNum<N>
&
rhs){
len
=
rhs.len;
for(int
i=0;i<len;i++){
d[i]
=
rhs.d[i];
}
return
*this;
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator
*=(int
m){
int
carry
=
0;
int
i;
for(i=0;i<len;i++){
carry
+=
d[i]
*
m;
d[i]
=
carry
%
10;
carry
/=
10;
}
while(carry){
d[len++]
=
carry
%
10;
carry
/=
10;
}
return
*this;
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator
+=(int
m){
int
carry
=
0;
int
i;
carry
=
d[0]
+
m;
d[0]
=
carry%10;
carry
/=
10;
for(
i
=
1;
i
<
len
&&
carry;
i++)
{
d[i]
+=
carry%10;
carry
/=
10;
}
while(carry){
d[len++]
=
carry
%
10;
carry
/=
10;
}
return
*this;
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator
+=(BigNum<N>
&
rhs){
int
i,
j,
carry
=
0;
for(
i
=
0,
j
=
0;
i
<
len
&&
j
<
rhs.len;
i
++,
j++)
{
d[i]
+=
rhs.d[i]
+
carry;
carry
=
d[i]/10;
d[i]
%=
10;
}
if(i
<
len)
{
if(carry)
{
for(;
i
<
len&&carry;
i
++){
d[i]
+=
carry;
carry
=
d[i]/10;
d[i]
%=
10;
}
}
if(carry)
d[i]
=
carry,
len
++;
}
else
if(j
<
rhs.len)
{
for(
;
j
<
rhs.len;
j
++){
d[j]
=
rhs.d[j]
+
carry;
carry
=
d[j]/10;
d[j]
%=
10;
}
if(carry)
d[j]
=
carry,
len
=
rhs.len
+1;
else
len
=
rhs.len;
}
else
{
if(carry)
d[i]
=
carry,
len++;
}
return
*this;
}
template<int
N>
BigNum<N>
&
BigNum<N>::operator
/=(int
dm){
int
carry
=
0;
int
i;
for(i=len-1;i>=0;i--){
carry
*=
10;
carry
+=
d[i];
d[i]
=
carry
/
dm;
carry
=
carry
%
dm;
}
while(d[--len]
==0){
;
}
len
++;
return
*this;
}
template<int
N>
int
BigNum<N>::cmp(BigNum<N>
&rhs){
if(len
!=
rhs.len)
return
len
-
rhs.len;
for(int
i=
len-1;i>=0;i--){
if(d[i]
!=
rhs.d[i])
return
d[i]
-
rhs.d[i];
}
return
0;
}
template<int
N>
ostream&
operator
<<
(ostream&
o,
BigNum<N>&
rhs){
for(int
i=rhs.len-1;i>=0;i--){
o
<<
rhs.d[i];
}
return
o;
}
int
main
()
{
int
i,
j,
n;
BigNum
<300>
sum,
temp;
while(
cin
>>
n)
{
for(
i
=
1;
i
<=
n;
i
++)
{
temp
=
1;
for(
j
=
1;
j
<=
i;
j
++)
temp
*=
j;
sum
+=
temp;
}
for(
i
=
sum.len-1;
i
>=
0;
i
--)
cout
<<
sum.d[i];
cout
<<
endl;
}
return
1;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jinshanluli
2013-04-13
知道答主
回答量:21
采纳率:0%
帮助的人:5万
展开全部
#include<stdio.h>
void main()
{
int a,i;
long double sum=0,j; //这里的 j 要定义成long double型,否则下面的sum=sum+j就会出错
for(a=1;a<=100;a++)
{
for(i=1,j=1;i<=a;i++)
j=j*i;
sum=sum+j;
}
printf("%lf\n",sum);// 这里也可以用%le格式,10^157~~
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2008-05-11
展开全部
#include<stdio.h>
long double func(int iNum)
{
long double sum=1;
for(int i=1;i<=iNum;i++)
{
sum=sum*i;
}
return sum;
}
int main()
{
long double sum=0;
for(int i=1;i<101;i++)
{
sum=sum+func(i);
}
printf("%lf",sum);

return 0;
}
只怕后面有一二百个0,我看楼主纯属好耍吧。。。。。。。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
sunshare_0709
2008-05-11
知道答主
回答量:6
采纳率:0%
帮助的人:0
展开全部
vc++6.0下调通!

#include<stdio.h>
long double func(int iNum)
{
long double sum=1; //最后结果太大,只能用long double型
int i;
for(i=1;i<=iNum;i++)
sum=sum*i;

return sum;
}

void main()
{
long double sum=0;
int i;
for(i=1;i<101;i++)
sum=sum+func(i);
printf("%lf",sum);

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(6)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式