用二分法求方程解
#include <stdio.h>#include <math.h>double f(double x){return x*x-2*x-1;}double bisection(double l,double r){double mid;if (f(l)*f(r)>0) {printf("error");return 0;}while (fabs(r-l)>0.0000001) {mid=(l+r)/2;if (f(l)*f(mid)<0) {r=mid;}else{l=mid;}}return l;}int main() {printf("%lf",bisection(2.0, 3.0));return 0;}
不完善之处:需预判解位于哪个区间
思路清晰!!!!!!
#include <stdio.h>#include <math.h>int isprime(int x){int i;if (x==0||x==1) {return 0;}for (i=2; i<sqrt(x); i++) {if (x%i==0) {return 0;}}return 1;}int main(){int x,i;scanf("%d",&x);while (x<6||x%2!=0){printf("Input an even num (>=6)");scanf("%d",&x);}if (x>6&&x%2==0){for (i=2; i<=(x/2); i++) {if (isprime(i)&&isprime(x-i)) {printf("%d+%d",i,x-i);break;}}}return 0;}