计算一个浮点数的立方根,不使用库函数。
保留一位小数。
数据范围:
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #define IsEqualZero(a) (fabs(a - 0.0) < 1e-6 ? 1 : 0) int JudgeInt(int x, double tar) { int a, b; a = x * x * x; b = (x+1) * (x+1) * (x+1); if( a < tar && b > tar){ return 1; } return 0; } int JudgeFloat(float x, double tar) { double a, b; a = (double)(x * x * x); b = (double)((x+0.01) * (x+0.01) * (x+0.01)); if((a - tar) < 0.0 && (b -tar) > 0.0){ return 1; } return 0; } float GetRes(double val) { int a; float b; // 计算整数部分 for(int i = 0; i < 3; i++){ if(JudgeInt(i, val)){ a = i; break; } } // 计算小数部分 for(int i = 0; i < 100; i++){ if(JudgeFloat(i*0.01+a, val)){ if(i % 10 >= 5){ i = (i / 10 + 1) * 10; }else{ i = (i / 10) * 10; } b = i*0.01+a; break; } } return b; } int main() { float val = 0.0; scanf("%f", &val); int flag = 0; float res = 0.0; if(val < 0.00){ val = -val; flag = 1; } if(!IsEqualZero(val)){ res = GetRes(val); if(flag){ res = -res; } } printf("%0.1f", res); return 0; }
#include <stdio.h> int main() { double a, b; scanf("%lf",&a); for(b=-20;b<=20;b=b+0.0001){ if(b*b*b>=a){ printf("%.1f",b); break; } } return 0; }
int main() { double y,x; scanf("%lf", &y); printf("%.1lf", x); return 0; }真心理解不了
//二分法求立方根 #include<math.h> #include<stdio.h> float func(float x, float y, float z) { float temp; temp = z; z = fabs(z); if (z == 0) return 0; else { float a, b; a = x * x * x - z; if (fabs(a) < 0.01) return x; b = y * y * y - z; if (fabs(b) < 0.01) return y; if (a * b < 0) { y = (x + y) / 2; } else { x = y; y = 2 * y; } if (temp > 0) return func(x, y, z); else return -func(x, y, z); } } int main() { float a, b; scanf("%f", &b); a = func(0, 20, b); printf("%.1f\n", a); return 0; }
#include <stdio.h> int main() { double val,err[11],base,i; char flag,k,min_err; while(scanf("%lf",&val)!=EOF) { flag=1; if(val==0) { printf("0.0"); continue; } else if(val<0) { flag=-1; val*=-1; } if(val<1) base=0; else if(val<8) base=1; else base=2; for(i=0,k=0;i<=1;i+=0.1,k++) { err[k]=val-(base+i)*(base+i)*(base+i); if(err[k]<0) err[k]*=-1; } min_err=0; for(k=1;k<11;k++) { if(err[min_err]>err[k]) min_err=k; } printf("%.1lf\n",flag*(base+min_err*0.1)); } 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); }
用例的坑有点多,-5,-0.07这样的,需要把数字先转化成正数,算出立方根再加上符号
#include<stdio.h> double LFG(double a) { int isFuShu = 0; if(a < 0) { isFuShu = 1; a = 0 - a; } if(a > 1) { int i = 1; for (i = 1; i < a / 3; i++) { if(i * i * i >= a) { break; } } double m = i - 1; for(m = i - 1; m < i; m += 0.1) { if(m*m*m > a) { break; } } double maxa = m*m*m - a > a- m*m*m ? m*m*m - a : a - m*m*m; double maxb = (m-0.1)*(m-0.1)*(m-0.1) - a > a- (m-0.1)*(m-0.1)*(m-0.1) ? (m-0.1)*(m-0.1)*(m-0.1) - a : a - (m-0.1)*(m-0.1)*(m-0.1); if(maxa > maxb) { if(isFuShu == 1) { return 0 - (m - 0.1); } else { return m - 0.1; } } else { if(isFuShu == 1) { return 0- m; } else { return m; } } } else { double i = 0.1; for(i = 0.1; i <= 0.9; i += 0.1) { if(i*i*i > a) { break; } } double maxa = i*i*i - a > a- i*i*i ? i*i*i - a : a - i*i*i; double maxb = (i-0.1)*(i-0.1)*(i-0.1) - a > a- (i-0.1)*(i-0.1)*(i-0.1) ? (i-0.1)*(i-0.1)*(i-0.1) - a : a - (i-0.1)*(i-0.1)*(i-0.1); if(maxa > maxb) { if(isFuShu == 1) { return 0 - (i - 0.1); } else { return i - 0.1; } } else { if(isFuShu == 1) { return 0 - i; } else { return i; } } } } int main() { double in; while(scanf("%lf", &in) != EOF) { printf("%.1lf", LFG(in)); } }