题面
小王购房贷款m万元20年,最终还款n万元,求小王的贷款利率。n>m,利率大于0,小于100%
输入
输入m,n
输出
输出贷款利率,保留4位小数,四舍五入
这道题比较坑的地方在于没有样例输入输出
代码
import java.util.Scanner; public class Main { public static double check(double x, double m) { for(int i = 0; i < 20; ++i) { m = (1 + x) * m; } return m; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); double m = sc.nextDouble(); double n = sc.nextDouble(); double l = 0, r = 1.0; while((r - l) > 1e-8) { double mid = (l + r) / 2; if(check(mid, m) < n) { l = mid; } else { r = mid; } } System.out.printf("%.4f", r); } }
Comments NOTHING