java中求立方根_求解立方根
0
夾逼法==二分法
import?java.util.Scanner;
public?class?Main{
public?static?void?main(String[]?args){
Scanner?in?=?new?Scanner(System.in);
double?input?=?in.nextDouble();
double?a?=(double)Math.round(getCubeRoot(input)*10)/10;
System.out.println(a);
}
public?static?double?getCubeRoot(double?input){
double?left?=?0;
double?right?=?input;
double?mid=0;
if(input==0){
return?0;
}else?if(input<0){
return?-getCubeRoot(-input);
}else{
while(right?-left?>0.01){
mid?=?(right+left)/2;
if(mid*mid*mid>input){
right?=?mid;
}else{
left?=?mid;
}
}
return?mid;
}
}
}
發表于 2020-03-10 16:59:50
回復(0)
更多回答
102
牛頓迭代法。設f(x)=x3-y, 求f(x)=0時的解x,即為y的立方根。
根據牛頓迭代思想,xn+1=xn-f(xn)/f'(xn)即x=x-(x3-y)/(3*x2)=(2*x+y/x/x)/3;
#include
inline double abs(double x){return (x>0?x:-x);}
double cubert(const double y){
double x;
for(x=1.0;abs(x*x*x-y)>1e-7;x=(2*x+y/x/x)/3);
return x;
}
int main(){
for(double y;~scanf("%lf",&y);printf("%.1lf\n",cubert(y)));
return 0;
}
編輯于 2016-08-12 13:43:08
回復(25)
29
import java.util.*;
public class Main
{
// 使用二分查找算法
public static double getCubeRoot(double input)
{
double min = 0;
double max = input;
double mid = 0;
// 注意,這里的精度要提高一點,否則某些測試用例無法通過
while ((max - min) > 0.001)
{
mid = (max + min) / 2;
if (mid * mid * mid > input)
max = mid;
else if (mid * mid * mid < input)
min = mid;
else
return mid;
}
return max;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
double input = sc.nextDouble();
double result = getCubeRoot(input);
System.out.printf("%.1f\n", result);
}
sc.close();
}
}
發表于 2016-08-12 15:20:51
回復(17)
21
命f(x) = x^3 - a,求解f(x) = x^3 - a = 0。
利用泰勒公式展開,即f(x)在xo處的函數值為:
f(x) = f(xo) +f'(xo)(x-xo) = xo^3-a+3xo^2(x-x0) = 0,
解之得:x = xo - (xo^3 - a) / (3xo^2)。 #include
#include
double fun(double n) {
double x = 1.0;
while(fabs(x*x*x - n) > 1e-9)
x = x - ((x*x*x - n) / (3*x*x));
return x;
}
int main() {
int number;
scanf("%d", &number);
double ans = fun(number*1.0);
printf("%.1f", ans);
return 0;
}
求平方根用一個套路@_@:
命f(x) = x^2?- a,求解f(x) = x^2?- a = 0。
利用泰勒公式展開,即f(x)在xo處的函數值為:
f(x) = f(xo) +f'(xo)(x-xo) = xo^2-a+2xo(x-x0) = 0,
解之得:x = (x+a/xo) / 2。
編輯于 2019-03-07 21:48:46
回復(8)
8
采用二分l=1,r=輸入數,結束條件是l-r<0.001即可。 #include
using?namespace?std;
int?main(){
int?a;
cin>>a;
double?l=1,r=a;
double?temp;
while((r-l)>0.001){
temp=(l+r)/2;
if(temp*temp*temp>a)r=temp;
else?l=temp;
}
printf("%.1lf",temp);
return?0;
}
編輯于 2020-03-20 23:45:29
回復(5)
17
# 牛頓迭代
a = float(raw_input())
e = 0.0001
t = a
while abs(t*t*t - a) > e:
# x(i+1) = x(i) - f(xi)/f(xi)'
t = t - ( t*t*t - a )* 1.0 / (3 * t*t)
print "%.1f" %t
發表于 2016-08-05 23:44:14
回復(1)
4
#?牛頓迭代法求解立方根的思路:
#?令f(x)?=?x^3?-?a,求解f(x)?=?x^3?-?a?=?0。
#?利用泰勒公式展開,即f(x)在x0處的函數值為:
#?f(x)?=?f(x0)?+f'(x0)(x-x0)?=?(x0^3-a)?+?(3x0^2)(x-x0)?=?0,
#?解之得:x?=?x0?-?(x0^3?-?a)?/?(3x0^2)。
#?????即?x?=?x?-?((x*x*x?-?n)?/?(3*x*x));
#?拓展:求平方根用一個套路:
#?令f(x)?=?x^2?-?a,求解f(x)?=?x^2?-?a?=?0。
#?利用泰勒公式展開,即f(x)在x0處的函數值為:
#?f(x)?=?f(x0)?+f'(x0)(x-x0)?=?(x0^2-a)?+?2x0(x-x0)?=?0,
#?解之得:x?=?x0?-?(x0^2?-?a)?/?2x0
#?????即?x?=?x?-?(x*x-a)/2x?可進一步化簡為:=(x+a/x)?/?2。
#?總結:
#?平方根與立方根的求解迭代公式:
#?新x?=?舊x?-?f(x)/f'(x)
#?新x?=?舊x?-?(x平方或者立方與輸入數a的差)/f(x)求導數
#?法一:牛頓迭代法
a?=?float(input().strip())??#?獲取輸入的實數a
e?=?0.0001??#?設定一個精度值
t?=?a??#?初始化立方根t的值為輸入的值a
while?abs(t*t*t?-?a)?>?e:??#?差值沒有達到精度,便一直更新立方根
#?x(i+1)?=?x(i)?-?f(xi)/f'(xi)
#?更新后的x?=?原x?-?(原x的立方-a)/f(原x)導數
t?=?t?-?(t*t*t?-?a)?*?1.0?/?(3?*?t*t)
print("%.1f"?%?t)??#?當精度達到要求時,此時的立方根t便為輸入實數的立方根解。
# 法二:二分法
a = float(input().strip())
epsilon = 0.0001
low = min(-1.0, a)
high = max(1.0, a)
ans = (low + high)/2
while abs(ans**3 - a) >= epsilon:
if ans**3 < a:
low = ans
else:
high = ans
ans = (low + high)/2.0
print('%.1f' % ans)
編輯于 2020-12-25 11:36:13
回復(0)
4
感覺這個小題確實不需要牛頓出山吧。用查找方式
#include? using?namespace?std;
//查找方式
int?main()
{
double?dv;
while(cin>>dv){
for(double?i=0;?i!=dv;?i+=1)?{
if(i*i*i?==?dv)?{
printf("%0.1f\n",?i);
break;
}?else?if(i*i*i?>?dv)?{
for(double?j=i-1;?j
if(j*j*j?>?dv)?{
if((j-0.05)*(j-0.05)*(j-0.05)?>?dv)
printf("%0.1f\n",?j-0.1);
else
printf("%0.1f\n",?j);
goto?_END;
}
}
}
}
_END:
dv=0;
}
}
編輯于 2019-08-09 22:32:17
回復(1)
3
#include
#include
using?namespace?std;
double?gCR(double?num);
int?main()
{
double?num;
cin>>num;
cout<
return?0;
}
double?gCR(double?num)
{
double?x=0;?//定義最終要返回的結果
double?step?=?1;?//步長
while(1)
{
//如果將要大于輸入值,改變步長
if((x+step)*(x+step)*(x+step)>num)
{
step?/=?10;
//不知道為什么至少要有三位小數才能正確四舍五入到一位
//所以這里循環多了一點
if(step?==?0.0001)
{
break;
}
continue;
}
x?+=?step;
}
return?x;
} 3ms還可以
編輯于 2020-07-16 12:59:41
回復(2)
3
#include
#include
//牛頓法
using namespace std;
double newton(double a){
double x = 1;
while (((x*x*x - a) >= 1e-7) || ((a - x*x*x) >= 1e-7)){
x = (x - x / 3 + a / (3 * x*x));
}
return x;
}
int main(){
double num;
while (cin >> num){
cout << setprecision(1) << fixed << newton(num) << endl;
}
return 0;
}
發表于 2017-06-05 09:37:03
回復(0)
4
#include
#include
#include
using namespace std;
int main()
{
double input;
cout << fixed; //小數點后一位
cout.precision(1);
while (cin >> input) //只考慮正數的情況
cout << (double)exp(1.0 / 3 * log(input)) << endl; //利用指數和對數相結合的思想
return 0;
}
發表于 2016-08-22 22:09:42
回復(3)
2
#include
(831)#include
#include
(802)#include
using?namespace?std;
#define?eps?1e-9
int?main(){
double?num,l=0,r,mid;
while(cin>>num)
{
r?=?max(1.0,num);
while(fabs(r-l)>eps)??//和一般的二分法不同的是r和l的區間逼近得足夠小的時候才符合題意,因為r也不可能
{
mid?=?(r?+?l)/2;
if(mid?*?mid?
l?=?mid;
else
r?=?mid;
}
printf("%.1f",mid);
return?0;
}
}
發表于 2020-04-13 14:11:17
回復(0)
2
//二分法
#include
#include
using?namespace?std;
double?getCubeRoot(double?start,?double?end,?double?input){
double?mid?=?(end+start)/2;
if(mid*mid*mid-input<0.0000001?&&?mid*mid*mid-input>-0.00000001)
return?mid;
if(mid*mid*mid-input>0)
return?getCubeRoot(start,mid,input);
return?getCubeRoot(mid,end,input);
}
int?main(){
double?data;
while(cin>>data){
double?res?=?getCubeRoot(0,data,data);
printf("%.1f",res);
}
}
編輯于 2020-02-28 22:58:03
回復(3)
2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
double input = cin.nextDouble();
double min = 0;
double max = input;
while(max - min > 0.00001) {
double temp = (min + max) / 2;
if(temp*temp*temp > input) {
max = temp;
} else {
min = temp;
}
}
min*=10;
double small = min - (int)min;
if(small >= 0.5) {
min++;
}
int n = (int)min;
min=(double)n/10;
System.out.println(min);
}
}
}
發表于 2016-03-28 10:24:47
回復(4)
3
求解給定值的立方根:
1、利用Scanner接收鍵入值。
2、利用牛頓迭代法求解立方根,牛頓迭代求解公式(1)所示,令鍵入值為y,定義函數
,則本題的迭代公式如(2),直至等式(3)成立停止迭代。
tips: 四舍五入保留1位小數位的做法可以利用String的靜態方法format(“%.1f”, x),其中%表示小數點前的位數,1表示保留小數點后1位,f表示轉換位float型(找過一下好像沒有可以轉換為double的)
(1)
(2)
(3)
import?java.util.Scanner;
public?class?Main{
public?static?void?main(String[]?args){
Scanner?input?=?new?Scanner(System.in);
while?(input.hasNextDouble()){
double?num?=?input.nextDouble();
double?x?=?1.0;
for?(;?Math.abs(Math.pow(x,3)-num)>1e-3;?x=x-((Math.pow(x,3)-num)/(3*Math.pow(x,2))));
System.out.println(String.format("%.1f",?x));
}
}
}
發表于 2020-02-23 16:45:26
回復(1)
12
python one line: import math
print(round(math.pow(int(input()),1/3),1))
編輯于 2017-09-08 10:12:40
回復(5)
3
#include
using namespace std;
int main()
{
double n,m;
cin>>n;
m=pow(n,1.0/3);
printf("%.1f",m);
return 0;
}
發表于 2017-08-17 11:23:37
回復(5)
2
#include
#include
using?namespace?std;
int?main()
{
double?d;
double?x=10000.0;
cin>>d;
while(abs(x*x*x-d)>0.000001)
{
x=x-(x*x*x-d)/(3*x*x);
}
printf("%.1lf\n",x);
return?0;
} 可以拓展為,求一元3次方程ax^3+bx^2+cx+d=0的解:↓
比如 x^3-27=0,我們就可以輸入1 0 0 -27,這樣我們就可以得到一個解 #include
#include
using?namespace?std;
int?main()
{
double?diedai(double?a,double?b,double?c,double?d,double?x);
double?a,b,c,d;
double?x=10000.0;
cout<
cin>>a>>b>>c>>d;
x=diedai(a,b,c,d,x);
cout<
return?0;
}
double?diedai(double?a,double?b,double?c,double?d,double?x)
{
while(abs(a*x*x*x+b*x*x+c*x+d)>0.000001)
{
x=x-(a*x*x*x+b*x*x+c*x+d)/(3*a*x*x+2*b*x+c);
}
return?x;
}
編輯于 2020-02-27 19:11:52
回復(0)
1
java二分查找
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double target = sc.nextDouble();
double left = 0, right = target, mid;
while(right - left > 0.01){
mid = left + (right - left) / 2;
if(mid * mid * mid < target)
left = mid;
else
right = mid;
}
System.out.printf("%.1f", right);
}
}
發表于 2020-07-28 16:59:33
回復(0)
1
#include
#include
int main()
{
double input;
scanf("%lf",&input);
double low=0,high=input;
double mid = (low+high)/2.0;
while(fabs(input-mid*mid*mid)>0.001)
{
if(mid*mid*mid > input)
{
high = mid;
}else
{
low = mid;
}
mid = (low+high)/2.0;
}
printf("%.1f",mid);
return 0;
}
發表于 2020-07-22 00:20:45
回復(0)
1
工程代碼寫多了,變笨了,我首先想的是為了穩定,先把特殊情況的處理邏輯寫了,然后在根據需求,劃分出四種情況,(-∞,-1),(-1,0),(0,1),(1,+∞),然后先用二分法寫1到+∞的情況,然后其他情況就是改一下左右邊界與更新中間點的邏輯了。
double getCubeRoot(double input)
{
if(input == 0)
return 0;
if(input == 1)
return 1;
if(input == -1)
return -1;
if(input > 1)
{
return getCubeRootGreaterThan1(input);
}
if(input < -1)
{
return getCubeRootSmallerThan_1(input);
}
if((input > 0) && (input < 1))
{
return getCubeRoot01(input);
}
if((input > -1) && (input < 0))
{
return getCubeRoot_10(input);
}
}
#define NUMS (0.0001)
double getCubeRootGreaterThan1(double input)
{
double left,right,middle;
left = 1;
right = input;
start:;
middle = (left + right) / 2;
double temp = middle * middle * middle;
temp = temp - input;
if((temp < NUMS) && (temp > (-NUMS)))
{
return middle;
}
if(temp < 0)
{
left = middle;
goto start;
}
if(temp > 0)
{
right = middle;
goto start;
}
}
發表于 2020-07-20 10:48:44
回復(1)
總結
以上是生活随笔為你收集整理的java中求立方根_求解立方根的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 降低Java占用_如何减少JAVA应用程
- 下一篇: java对象复制到另一个对象中_spri