``````class fun{
// TODO Auto-generated method stub
public static void Bisektion()
{
float ymiddle = (float)0.0;
float yleft = (float)0.0;
float yright = (float)0.0;

float a = 0;
float b = (float)3;

for(int i = 0;i <=10000;i++)
{

float c = (float)((a+b)/2);

float x = c;
ymiddle =(float) Math.pow(x, 3)-3*x-3;    // 带入C， C 居于方程的中间
yleft =(float) Math.pow(a, 3)-3*a-3;
yright =(float) Math.pow(b, 3)-3*b-3;

if (ymiddle*yleft>0) {
a = c;
if(i==10000)
{
System.out.println("Die Resultat:"+c);
}
}else {
b = c;
if(i==10000)
{
System.out.println("Die Resultat:"+c);
}
}
}

}
}
public class Bisektionsverfahren {

public static void main(String[] args) {
fun.Bisektion();
}

}
``````

1个回答

while (Math.Abs(yleft - yright) > 0.00001) //精度为0.00001
{
....
}

Pfeffer 回复caozhy: 一万次只是我自己设置的
2 年多之前 回复
Pfeffer 回复caozhy: 一万次只是我自己设置的
2 年多之前 回复

C语言怎么用二分法求一元三次方程的根。啊 能不能把程序打出来一下谢了！！！

Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers. Output For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO". Sample Input 3 3 3 1 2 3 1 2 3 1 2 3 3 1 4 10 Sample Output Case 1: NO YES NO

1、本人小白，刚接触c语言（用的是vs2019）。练习题：用二分法求方程在（-10，10）范围内的根：y=x^3+6x^2+3x-6，收敛精度为10^-5。二分法好像只能计算出一个零点，就想着用循环的方式，把第一次循环得到的x=0.7465代替b的位置，再进行二分法。但是好像不太行，请问我这问题出在什么地方，要怎么改，或者有没有更简单的方法？注：已知这个方程在（-10，10）内有三个根。 ![图片说明](https://img-ask.csdn.net/upload/202004/25/1587827162_467967.png) #include <stdio.h> #include<math.h> int main() { double a, b, x, fa, fb, fx; a = -10, b = 10, x = 0; fa = pow(a, 3) + 6 * pow(a, 2) + 3 * a - 6; fb = pow(b, 3) + 6 * pow(b, 2) + 3 * b - 6; do { x = (x > 0) ? b : a; do { x = (a + b) / 2; fx = pow(x, 3) + 6 * pow(x, 2) + 3 * x - 6; if (fx * fa < 0) { b = x; fb = pow(b, 3) + 6 * pow(b, 2) + 3 * b - 6; } else { a = x; fa = pow(a, 3) + 6 * pow(a, 2) + 3 * a - 6; } } while (fabs(fx) >= 1e-5); printf("x:%f\n", x); } while (x > -10 && x < 10); return 0; }

C语言二分法求值（请帮忙找下问题）

#include<stdio.h> #include<math.h> int main() { double x1,x2,x,y1,y2,y; x1=10; x2=-10; y1=2*x1*x1*x1-4*x1*x1+3*x1-6; y2=2*x2*x2*x2-4*x2*x2+3*x2-6; do { x=(x1+x2)*0.5; y=2*x*x*x-4*x*x+3*x-6; if(y>=0) x2=x, x1=x1; if(y<0) x1=x,x2=x2; } while(fabs(x1-x2)>=0.0001); printf("%f",x2); return 0; } 这样输出之后结果是-10，求大神指点，我想知道的是我为什么错，只要纠正就好，谢谢各位大神

C语言 二分法求多项式单根 程序通不过oj的测试

![图片说明](https://img-ask.csdn.net/upload/201908/11/1565533167_367875.png) 以下面的程序运行，oj的4个测试点只能通过两个，程序有什么没考虑到的情况吗？ #include <stdio.h> #include <math.h> int a3,a2,a1,a0; double f(double x) { double func; func = a3*pow(x,3.0)+a2*pow(x,2.0)+a1*x+a0; return func; } int main() { double a,b,mid; scanf("%d %d %d %d\n%lf %lf",&a3,&a2,&a1,&a0,&a,&b); while (b-a>=0.01) { mid =(a+b)/2; if (f(mid)==0.0) { goto out; } else if (f(mid)*f(a)>0) { a=mid; } else if (f(mid)*f(b)>0) { b=mid; } } mid = (a+b)/2; out: printf("%.2f", mid); return 0; }

java的二分法怎么写？？

Java 二分法查找最值问题。

python二分法，请大神帮忙看看这个输出，求解释。非常捉急~

def search2(a,m): low = 0 high = len(a) - 1 while low<=high: mid = (low + high)/2 midval = a[mid] if midval<m: low = mid + 1 elif midval>m: high = mid-1 else: print mid return mid print -1 return -1 这是我在网上粘的别人写的，我不懂为什么最后两行会print和return的是-1？？？不管if,elif,else,讨论完了都该返回查找结果啊，就是这个数在哪不是吗

Matlab二分法，总是出现错误，各位大神请赐教！

``` %二分法 function [xstar,index,it]=bisect(fun,a,b,ep) if nargin<4 ep=0.00001; end fa=feval(fun,a);fb=feval(fun,b); if fa*fb>0 xstar=[fa,fb];index=0;it=0; return end k=0; while abs(b-a)/2>=ep x=(a+b)/2; fx=feval(fun,x); if fx*fa<0 b=x;fb=fx; else a=x;fa=fx; end k=k+1; end xstar=(a+b)/2;index=1;it=k; end function f=fun2(x) f=exp(x)+10*x-2; [xstar,index,it]=bisect('fun2',0,1,0.001); end ``` 出错：>> fun2(1) Maximum recursion limit of 1000 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer. Error in bisect

#include <stdio.h> int binary_search_recursive(int arr[], int left, int right, int query); int main () { int arr[10]; int left, right, query; int i , res_r; printf ("请输入数据："); for (i = 0; i <= 9; i++) scanf ("%d" , &arr[i]); printf ("输入区间："); scanf ("%d%d" , &left , &right); printf ("输入要查找的数据："); scanf ("%d" , &query); res_r = binary_search_recursive(arr , left , right , query); printf ("%d\n" , res_r); return 0; } int binary_search_recursive(int arr[], int left, int right, int query) { int low = left , high = right , mid; int flag = 0; mid = (low + high) / 2; if (low > high) flag = -1; if (query == arr[mid]) flag = 1; if (arr[mid] > query) binary_search_recursive(arr , low , mid - 1 , query); else binary_search_recursive(arr , mid + 1 , high , query); if (flag == 1) return mid; if (flag == -1) return -1; }

c++编写求解一元二次方程组根问题

def half_seek(point, seqence): a = len(readsx) b = a/2 if point <= readsx[b]: c = readsx[:b + 1] a = len(c) b = a/2 if point <= c[b]: c = c[:b + 1] a = len(c) b = a/2 else: c = c[b:] a = len(c) b = a/2 if point <= c[b]: c = c[:b + 1] a = len(c) b = a/2 else: c = c[b:] a = len(c) b = a/2 if point <= c[b]: else: c = readsx[b:] a = len(c) b = a/2 if point <= c[b]: c = c[:b + 1] a = len(c) b = a/2 else: c = c[b:] a = len(c) b = a/2 if point <= c[b]: c = c[:b + 1] a = len(c) b = a/2 格式不对。。。。。我就不粘贴了。。。。怎么写个循环呢？ 我是想写个函数以后用，目的是给一个值，给了一个由小到大的数列list形式，确定这个给的数能排哪个位置？

MySQL数据库面试题（2020最新版）

HashMap底层实现原理，红黑树，B+树，B树的结构原理 Spring的AOP和IOC是什么？它们常见的使用场景有哪些？Spring事务，事务的属性，传播行为，数据库隔离级别 Spring和SpringMVC，MyBatis以及SpringBoot的注解分别有哪些？SpringMVC的工作原理，SpringBoot框架的优点，MyBatis框架的优点 SpringCould组件有哪些，他们...

Python爬虫，高清美图我全都要（彼岸桌面壁纸）

6年开发经验女程序员，面试京东Java岗要求薪资28K

Java岗开发3年，公司临时抽查算法，离职后这几题我记一辈子