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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言程序 一个阶乘求和 程序相关的知识,希望对你有一定的参考价值。

100!+99!+98!+97!+96!+95!+。。。。。。+2!+1

参考下把,是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;
参考技术A 参考下把,是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;
参考技术B #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~~
参考技术C #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,我看楼主纯属好耍吧。。。。。。。。
参考技术D 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);

c_cpp 奇数阶乘求和

#include<stdio.h>
long long factorial(int n);//函数声明 
int main()
{
	long long sum = 0;//奇数阶乘求和结果 
    int max_odd = 15;//最大奇数值 
	for( int n = 1; n <= max_odd; n += 2){ //定义n=1,3,5...max 
		sum += factorial(n);//n的阶乘累加求和 
	} 
	printf("1!+3!+5!+...+%d!=%lld\n",max_odd,sum);//显示结果 
	return 0;	
}
long long factorial(int n)
{   
	if( n == 0 ) return 1;
	return n * factorial( n - 1 );//递归求阶乘 
}	

以上是关于C语言程序 一个阶乘求和 程序的主要内容,如果未能解决你的问题,请参考以下文章

C语言,dev cpp,编写的计算阶乘程序,数值较大时输出不正确,求助.

C语言求1-20的阶乘之和

用C语言求1~20的阶乘之和!

c语言20个阶乘相加

用C语言 从一的阶乘加到二十的阶乘

c语言怎么用递归调用函数的方法求n的阶乘?