计算一个浮点数的立方根,不使用库函数。
保留一位小数。
数据范围:
n = float(input()) if n == 0: print('0.0') elif n>0: ans = str(n**(1/3)).split('.') if int(ans[1][1])<5: print(f'{ans[0]}.{ans[1][0]}') else: print(f'{ans[0]}.{int(ans[1][0])+1}') else: n *= -1 ans = str(n**(1/3)).split('.') if int(ans[1][1])<5: print(f'-{ans[0]}.{ans[1][0]}') else: print(f'-{ans[0]}.{int(ans[1][0])+1}')
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { double num = in.nextDouble(); double left, right, mid = 0.00; left = Math.min(-1.0, num); right = Math.max(1.0, num); while (right - left > 0.001) { mid = (left + right) / 2; if (mid * mid * mid > num) right = mid; else if (mid * mid * mid < num) left = mid; else System.out.printf("%.1f", mid); } System.out.printf("%.1f", right); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextDouble()) { double n = sc.nextDouble(); double i = 1; while(i*i*i < n) i++; while(i*i*i > n) i -= 0.1; while(i*i*i < n) i += 0.01; String res = String.format("%.1f", i); System.out.println(res); } sc.close(); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 牛顿迭代法 while(sc.hasNextDouble()) { double y = sc.nextDouble(); double x; for(x = 1.0; Math.abs(x*x*x - y) > 1e-7; x = (2*x + y/x/x)/3); String res = String.format("%.1f", x); System.out.println(res); } sc.close(); } }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <string> #include <cmath> using namespace std; double F(double n, double l, double r){ while(r-l>1e-8) { double mid = (l+r)/2; if(pow(mid,3)<n) l = mid; else r = mid; } return l; } int main() { double n; while(cin>>n) { if(n>=0) printf("%.1f\n", F(n,0,max(n,1.0))); else printf("%.1f\n", F(n,min(n,-1.0),0)); } return 0; }
简单粗暴 #include<stdio.h> int main(void) { double i; double n; bool flag; while(scanf("%lf",&n) != EOF) { flag=0; if(n<0) { n=-n; flag=1; } for(i=0;i*i*i<=n;i=i+0.001); if(flag==0) printf("%.1f\n",i); else printf("%.1f\n",-i); } return 0; }
import java.text.DecimalFormat; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { double input = sc.nextDouble(); System.out.println(getCubeRoot(input)); } } public static double getCubeRoot(double input) { double x0 = 1; double x1 = x0 - (x0 * x0 * x0 - input) / (3 * x0 * x0); while (Math.abs(x1 - x0) > 0.000001) { x0 = x1; x1 = x0 - (x0 * x0 * x0 - input) / (3 * x0 * x0); } DecimalFormat df = new DecimalFormat("#0.0"); return Double.parseDouble(df.format(x1)); } }
import java.text.DecimalFormat; import java.util.*; public class Main { /** * 功能:求解立方根 */ public static double getCubeRoot(double d){ double j = 0.0001; for(double i = 0; i < d; i = i + j){ if((i*i*i - d > 0 && i*i*i - d < 0.01) || (i*i*i - d < 0 && d - i*i*i < 0.01)) return i; } return 0; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ double d = sc.nextDouble(); DecimalFormat df = new DecimalFormat("0.0"); System.out.println(df.format(getCubeRoot(d))); } sc.close(); } }
#include <stdio.h> int main() { double input; while(scanf("%lf",&input)>0) { double minn=0,maxn=input; while(maxn-minn>0.001) { double mid=(minn+maxn)/2; if(mid*mid*mid>input) maxn=mid; else minn=mid; } printf("%.1lf",minn); } return 0; }
#include <stdio.h> 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; }
#include "stdio.h" int main(void) { double a,b = 0,i; scanf("%lf",&a); if(a < 0) { a = 0 - a; b = 1; } else{ a = a; } for(i = 0;i <= a + 1;i+=0.005) { if((i * i * i) > a) break; } if(b == 1) { i = 0 - i; } printf("%.1lf\r\n",i); }
#include <iostream>
#include<iomanip>
using namespace std;
double Abs(double a, double b)
{
if (a - b < 0) return b - a;
else return a - b;
}
double GetCubeRoot(double m, double x0)
{
double x1;
x1 = (2 * x0 + m / x0 / x0) / 3.0;
if (Abs(x0, x1) >= 0.00001) return GetCubeRoot(m, x1);
else return x1;
}
int main()
{
double input;
while (cin >> input) cout << setprecision(1) << fixed << GetCubeRoot(input, 1.0) << endl;
return 0;
}
/** *牛顿迭代法 *Xn+1=Xn-(Xn*Xn*Xn-Y)/3*Xn*Xn */ import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); while(sc.hasNext()){ double y = sc.nextDouble(); double x0 = y; double x1 = (2*x0+y/x0/x0)/3; while(Math.abs(x1*x1*x1-y)>0.000001){ x0 = x1; x1 = (2*x0+y/x0/x0)/3; } System.out.printf("%.1f",x1); } } }
#include "bits/stdc++.h" using namespace std; int main() { double in; cin>>in; printf("%.1lf",pow(in,1.0/3)); }//显然是错的,但通过了(lll¬ω¬)
#include "bits/stdc++.h" using namespace std; int main() { double in; cin>>in; double r=in; while(fabs(r*r*r-in)>1e-5)//控制精度1e-5或1e-6 r-=(r*r*r-in)/(3*r*r);//迭代公式 printf("%.1lf",r); }//牛顿迭代法
#include <iostream> using namespace std; int main() { double num; while (cin>>num) { double x = 0, tmp = 0; if (num<0) tmp = -num; else { tmp = num; } while (x*x*x<=tmp){ x += 0.0001; } if (num < 0){ x = -x; printf("%.1f\n", x); } else { printf("%.1f\n", x); } } return 0; }