Data_Structure/作業/unit2/main.c

60 lines
1.2 KiB
C
Raw Permalink Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#define f(x) (x*x*x-2*x*x-x+2)
//#define f(x) (x*x-100)
#define f(x) (3*x*x*x*x+9*x*x*x-198*x*x+156*x+360)
void binary_search ( int *ans){
int count = 1000 , i=0;
double mid , lower=-15 , upper=20;
while ((f(mid)) != 0 && i < count ) {
mid = (lower + upper) / 2.0;
printf("#%d\t[%f,%f]\t<EFBFBD><EFBFBD>=%f\n",i+1,lower,upper,mid);
if (fabs(f(mid)) == 0) {
break;
}
i++;
if ( f(mid) * f(lower) < 0) {
upper = mid;
} else {
lower = mid;
}
}*ans = mid;
}
void Exhaustive(int a, int b, int *ans, int *count) {
*count = 0;
int i;
for (i = a; i <= b; i++) {
if (f(i) == 0) {
ans[*count] = i;
(*count)++;
}
}
}
int main() {
int i , x , lower=-200 , upper=200 ;
int ans[10];
int *count;
Exhaustive(lower,upper,ans,&count);
if (count > 0) {
printf("*** <20>Ѧh<D1A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD> ***\n");
printf("\n<EFBFBD><EFBFBD><EFBFBD>{<7B><>p(x)= 3^4+9x^3-198x^2+156+360\n");
printf("\n<EFBFBD>ѱo<EFBFBD><EFBFBD><EFBFBD>Ʈڬ<EFBFBD>");
for (i = 0; i < count; i++) {
printf(" %d ", ans[i]);
}
} else {
printf("<EFBFBD>b<EFBFBD><EFBFBD><EFBFBD>w<EFBFBD>d<EFBFBD>򤺨S<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƸѡC\n");
}
return 0;
}