# 请问这个代码哪里有问题？

weixin_39393548 2017-12-06 09:50:10
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int fac(int n)
{
if(n < 0)
return 0;
if(n == 0 || n == 1)
return 1;
else
return n * fac(n - 1);
}
int main()
{
double x;
double factor = 1;
double numerator = x;
double denominator ;
double item = x, sum = 1;
const double error = 1e-8;
int n = 1;
cin >> x;
for(;abs(item) > error(n <= n0;);n++)
{
sum += item;
item = factor*numerator/fac(n);
factor = pow(-1,n+1);
numerator = pow(x,n);
denominator = fac(n);
cout.precision(10);
cout << left;
}
//cout << sum << endl;

}

...全文
473 19 点赞 打赏 收藏 举报

19 条回复

• 打赏
• 举报

• 打赏
• 举报

``````//1+x-x^2/2！+x^3/3!-…………(-1)^(n+1)x^n/n!

#include <iostream>
#include <cmath>
//#include <iomanip>

using namespace std;

/*
int fac(int n)
{
if(n < 0)
return 0;
if(n == 0 || n == 1)
return 1;
else
return n * fac(n - 1);
}
int main()
{
double x;
double factor = 1;
double numerator;
double denominator ;
double item, sum = 1;
const double error = 1e-8;
int n = 1;
cin >> x;
numerator = x;
item = x;

for(;abs(item) > error;n++)
{
sum += item;
item = factor*numerator/fac(n);
factor = pow(-1,n+1);
numerator = pow(x,n);
denominator = fac(n);
cout.precision(10);
cout << left;
}
//cout << sum << endl;

}
*/

#define MIN     1e-8

long fac(long n)
{
if (n == 0 || n == 1)
return 1;
else
return (n * fac(n-1));
}

int main(void)
{
double sum = 1.0, item;
int n, x, sign = 1;

while (cin>>x && x < 0)
;

n = 1;
item = pow(-1, n+1) * pow(x, n) / fac(n);

while (fabs(item) >= MIN) {
sum += item;
n++;
item = pow(-1, n+1) * pow(x, n) / fac(n);
cout.precision(9);
cout<<fabs(item)<<"\t";
}

cout.precision(9);
cout<<"sum = "<<sum<<endl;

return 0;
}``````

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

wodexiaojidan 2017-12-07

``````[code=c]#include <iostream>
#include <cmath>
#include <iomanip>
#include <cassert>

using namespace std;

int Factorial(int n, int result = 1)
{
assert(n >= 0);

if(n == 0 || n == 1){
return result;
} else{
return Factorial(n - 1, n * result);
}
}

int main()
{
double x, factor, numerator, denominator, item, sum = 0.0;
const double epsilon = 1e-8;
int n = 0;

cin >> x;
do{
factor = pow(-1, n + 1);
numerator = pow(x, n);
denominator = Factorial(n);

item = factor * numerator / denominator;
sum += item;

cout << "item = " << item << endl;
}while(fabs(item) >= epsilon && ++n);

cout.precision(10);
cout << "sum = " << sum << endl;
}``````
[/code] 。。。
• 打赏
• 举报

[quote=引用 9 楼 cfjtaishan 的回复:]
``````//1+x-x^2/2！+x^3/3!-…………(-1)^(n+1)x^n/n!

#include <iostream>
#include <cmath>
//#include <iomanip>

using namespace std;

/*
int fac(int n)
{
if(n < 0)
return 0;
if(n == 0 || n == 1)
return 1;
else
return n * fac(n - 1);
}
int main()
{
double x;
double factor = 1;
double numerator;
double denominator ;
double item, sum = 1;
const double error = 1e-8;
int n = 1;
cin >> x;
numerator = x;
item = x;

for(;abs(item) > error;n++)
{
sum += item;
item = factor*numerator/fac(n);
factor = pow(-1,n+1);
numerator = pow(x,n);
denominator = fac(n);
cout.precision(10);
cout << left;
}
//cout << sum << endl;

}
*/

#define MIN     1e-8

long fac(long n)
{
if (n == 0 || n == 1)
return 1;
else
return (n * fac(n-1));
}

int main(void)
{
double sum = 1.0, item;
int n, x, sign = 1;

while (cin>>x && x < 0)
;

n = 1;
item = pow(-1, n+1) * pow(x, n) / fac(n);

while (fabs(item) >= MIN) {
sum += item;
n++;
item = pow(-1, n+1) * pow(x, n) / fac(n);
cout.precision(9);
cout<<fabs(item)<<"\t";
}

cout.precision(9);
cout<<"sum = "<<sum<<endl;

return 0;
}``````

10^-8精度下 x=2出不来结果 无限往下循环 增加到10^-5才可以算出来 这样是我的电脑的问题 还是编译器的问题[/quote] 我测试的是1,2,3都能出结果，但是4可能精度位10^-8太小了，一直处于计算中。
• 打赏
• 举报

weixin_39393548 2017-12-07

``````//1+x-x^2/2！+x^3/3!-…………(-1)^(n+1)x^n/n!

#include <iostream>
#include <cmath>
//#include <iomanip>

using namespace std;

/*
int fac(int n)
{
if(n < 0)
return 0;
if(n == 0 || n == 1)
return 1;
else
return n * fac(n - 1);
}
int main()
{
double x;
double factor = 1;
double numerator;
double denominator ;
double item, sum = 1;
const double error = 1e-8;
int n = 1;
cin >> x;
numerator = x;
item = x;

for(;abs(item) > error;n++)
{
sum += item;
item = factor*numerator/fac(n);
factor = pow(-1,n+1);
numerator = pow(x,n);
denominator = fac(n);
cout.precision(10);
cout << left;
}
//cout << sum << endl;

}
*/

#define MIN     1e-8

long fac(long n)
{
if (n == 0 || n == 1)
return 1;
else
return (n * fac(n-1));
}

int main(void)
{
double sum = 1.0, item;
int n, x, sign = 1;

while (cin>>x && x < 0)
;

n = 1;
item = pow(-1, n+1) * pow(x, n) / fac(n);

while (fabs(item) >= MIN) {
sum += item;
n++;
item = pow(-1, n+1) * pow(x, n) / fac(n);
cout.precision(9);
cout<<fabs(item)<<"\t";
}

cout.precision(9);
cout<<"sum = "<<sum<<endl;

return 0;
}``````

10^-8精度下 x=2出不来结果 无限往下循环 增加到10^-5才可以算出来 这样是我的电脑的问题 还是编译器的问题
• 打赏
• 举报

weixin_39393548 2017-12-07

• 打赏
• 举报

weixin_39393548 2017-12-07

• 打赏
• 举报

• 打赏
• 举报

[quote=引用 8 楼 zhao4zhong1 的回复:] 把代码贴图是为了保护版权不让别人轻易复制粘贴吗？

• 打赏
• 举报

wodexiaojidan 2017-12-06

• 打赏
• 举报

weixin_39393548 2017-12-06

• 打赏
• 举报

wodexiaojidan 2017-12-06
• 打赏
• 举报

weixin_39393548 2017-12-06

f(x) = -1 + x - x^2/1 * 2 +...+ (-1)^(2n + 1) * x^n/n!楼主是想求这个级数？但是代码不对

• 打赏
• 举报

wodexiaojidan 2017-12-06
f(x) = -1 + x - x^2/1 * 2 +...+ (-1)^(2n + 1) * x^n/n!楼主是想求这个级数？但是代码不对
• 打赏
• 举报

6.0w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等

2017-12-06 09:50