![](/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.