[ create a new paste ] login | about

Link: http://codepad.org/gN46mr8L    [ raw code | fork ]

C++, pasted on Jan 17:
#include <stdio.h>
#include <math.h>
#include "stdafx.h"

//反復回数
const int count = 30;

double f(double x) { return exp(x) - 3*x; }

double solve() {
  double x_1 = 0;
  double x_2 = 1;
  int i;
  for (i = 0 ; i < count ; i++) {
    double center = (x_1+x_2)/2;
    if ( f(center) > 0 )
      x_1 = center;
    else
      x_2 = center;
  }
  return (x_1+x_2)/2;
}

int main(void) {
  printf("%f" , solve());
  return 0;
}


Create a new paste based on this one


Comments: