😭为什么在输入3.4和10之后会输出1.#J

#include <stdio.h>
int main()
{
double x,a,b;
float i;
int n;
scanf("%lf %d",&x,&n);
a=1;
b=x;
for(i=2.0;i<=n;i++)
{
x=(2.0i-1)/ixb-(i-1)/ia;
a=b;
b=x;
}
printf("%.2f",x);
return 0;
}

img

这里的x是最开始输入的x值,但你的程序x值是一直在变化的

img

#include <stdio.h>
int main()
{
double x,a,b,c;
float i;
int n;
scanf("%lf %d",&x,&n);
a=1;
b=x;
for(i=2.0;i<=n;i++)
{
c=(2.0*i-1)/i*x*b-(i-1)/i*a;
a=b;
b=c;
}
    //四舍五入保留两位小数
printf("%.2lf",c+0.005);
return 0;
}

看起来程序方面没有问题,这里可能出问题就是你的算法问题了,或者注意除数不要为0.
我试着执行你的代码,提示是inf 输出结果不对,太大或者太小。。。

img

i不应使用float类型

仅供参考:

// https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2019
// https://docs.microsoft.com/zh-cn/cpp/porting/visual-cpp-change-history-2003-2015?view=vs-2019
//
// Starting in Visual Studio 2015, if the argument that corresponds to a floating-point conversion specifier (a, A, e, E, f, F, g, G) is infinite,
// indefinite, or NaN, the formatted output conforms to the C99 standard. This table lists the formatted output:
//   Value           Output
//   --------------  ---------
//   infinity        inf
//   Quiet NaN       nan
//   Signaling NaN   nan(snan)
//   Indefinite NaN  nan(ind)
// Any of these values may be prefixed by a sign. If a floating-point type conversion specifier character is a capital letter,
// then the output is also formatted in capital letters. For example, if the format specifier is %F instead of %f, an infinity
// is formatted as INF instead of inf. The scanf functions can also parse these strings, so these values can make a round trip
// through printf and scanf functions.
//
// Before Visual Studio 2015, the CRT used a different, non-standard format for output of infinite, indefinite, and NaN values:
//   Value                           Output
//   ------------------------------  -------------------------
//   + infinity                      1.#INF random-digits
//   - infinity                      -1.#INF random-digits
//   Indefinite (same as quiet NaN)  digit .#IND random-digits
//   NaN                             digit .#NAN random-digits
// Any of these may have been prefixed by a sign, and may have been formatted slightly differently depending on field width and precision,
// sometimes with unusual effects. For example, printf("%.2f\n", INFINITY) would print 1.#J because the #INF would be "rounded" to 2 digits of precision.
#include <stdio.h>
#include <float.h>
int main() {
    int r;
    double d1=-1.0,d2=-2.0;
    printf("input two double (d1 d2):");
    r=scanf("%lf%lf",&d1,&d2);
    printf("scanf return:%d d1:%lg d2:%lg\n",r,d1,d2);
    printf("_finite(d1):%d\n",_finite(d1));
    printf("_fpclass(d1):0x%04X\n",_fpclass(d1));
    return 0;
}
// input two double (d1 d2):nan 22
// scanf return:2 d1:nan d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):nan(snan) 22
// scanf return:2 d1:nan(snan) d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):nan(ind) 22
// scanf return:2 d1:-nan(ind) d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):-inf 22
// scanf return:2 d1:-inf d2:22
// _finite(d1):0
// _fpclass(d1):0x0004