![](/img/trans.png)
[英]I have a problem with “team” codeforces task, don't understand why my c++ code does work and doesn't
[英]I have a problem with C++ because it refuses to display the answer and I don't know where the problem is in code
#include <iostream>
#include <math.h>
#include <iomanip>
#include <tchar.h> using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
setlocale(LC_CTYPE, "Russian");
double x;
for (x = 2; x < 6.3; x + 0.4);
{
double Rez, Rez1;
Rez = pow(x, 4) + 2 * pow(x, 2) + 3;
Rez1 = cos(3 * x) + exp(-2 * x);
cout << Rez << Rez1;
}
system("Pause");
return 0;
它应该计算每个 X 值在 2 到 6.3 范围内的方程,步长为 0.4,即 2、2.4、2.8 等。
正如评论中的某人所建议的那样,问题出在最后的分号上。 正是由于那个分号,x 的值在第一次迭代中增加到 6.4 以上,并且只发生了 1 次迭代。 变量 x 也需要以适当的方式递增:这是工作代码:
#include <iostream>
#include <math.h>
#include <iomanip>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_CTYPE, "Russian");
double x=2;
for ( x = 2 ; x < 6.3; x = x + 0.4 )
{
double Rez, Rez1;
Rez = pow( x, 4) + 2 * pow( x, 2) + 3;
Rez1 = cos(3 * x) + exp(-2 * x);
cout <<"Result 1:" <<Rez <<" Result 2:"<< Rez1<< endl;
}
system("Pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.