找错误,找错误,单次输入对,多次就不对

img


#include
#include
#define PI 3.1415926
double fact(int n)
{
  if (n == 0)
  return 1;
  return fact(n - 1)*n;
}
int main()
{
  double x, sum = 0, term = 1,h;
  int n = 1, t = 1;
  int T;
    scanf("%d",&T);
    while(T--){
  scanf("%lf", &x);
  h=x*PI/180;
  while (term >= 0.000001) {
  term = pow(h, 2 * n - 1) / fact(2 * n - 1);
  sum += t * term;
  t = -t;
  n++;
 }
  printf("%.6lf\n", sum);
  }
}

修改如下,供参考:

#include <stdio.h>
#include <math.h>
#define PI 3.1415926
double fact(int n)
{
    if (n == 0)
        return 1;
    return fact(n - 1) * n;
}
int main()
{
    double x, sum = 0, term = 1, h;
    int n = 1, t = 1;
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%lf", &x);
        h = x * PI / 180;
        sum = 0; term = 1; t = 1; n = 1;
        while (fabs(term) >= 0.000001) {
            term = pow(h, 2 * n - 1) / fact(2 * n - 1);
            sum += t * term;
            t = -t;
            n++;
        }
        printf("%.6lf\n", sum);
    }
}

换种写法:

#include <stdio.h>
#include <math.h>
#define PI 3.1415926
int main()
{
    double x, sum = 0, term = 1, h;
    int n = 1, t = 1;
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%lf", &x);
        if (x <= 0 || x >= 180) continue;
        h = x * PI / 180.0;
        sum = h; term = h; t = -1; n = 1;
        while (fabs(term) >= 0.000001) {
            term *= h * h /((2 * n + 1) * 2 * n);
            sum += t * term;
            t = -t;
            n++;
        }
        printf("%.6lf", sum);
        if (T)  printf("\n");
    }
    return 0;
}

记住,如果再遇到类似的情况,一次执行正确,再执行就错误
那保证是有变量在循环过程中忘记初始化了,它保留了上次计算的结果当然错了
你的term 没初始化成1,while循环根本进不去啊,n也没初始化