97
牛顿迭代法。设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
回复(24)
28
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)
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)
19
命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)
16
# 牛顿迭代
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
感觉这个小题确实不需要牛顿出山吧。用查找方式
#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)
2
#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
回复(1)
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
#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)
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
回复(1)
12
python one line: import math
print(round(math.pow(int(input()),1/3),1))
编辑于 2017-09-08 10:12:40
回复(5)
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)
3
#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
回复(2)
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)
1
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
try{
double dou = scanner.nextDouble();
System.out.printf("%.1f\n",getCubeRoot(dou,1.0));
//System.out.printf("%.1f\n",getCubeRoot2(dou,0,dou));
}catch(Exception e){
System.out.println("输入类型错误!");
}
}
scanner.close();
}
//方法一:牛顿迭代法
//命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)。
public static double getCubeRoot(double target, double Num){
if(Math.abs(Num*Num*Num-target)>1e-9){
Num = Num - (Num*Num*Num-target)/(3*Num*Num);
return getCubeRoot(target,Num);
}
return Num;
}
//方法二:二分查找法
public static double getCubeRoot2(double target, double min, double max){
if((max-min)>1e-9){
double mid = (max+min)/2;
if(mid*mid*mid>target){
return getCubeRoot2(target,min,mid);
}else if(mid*mid*mid
return getCubeRoot2(target,mid,max);
}else{
return mid;
}
}else{
return max;
}
}
}
发表于 2020-07-09 10:32:39
回复(0)
1
#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)