牛客网在线编程:星际穿越

题目描述:

航天飞行器是一项复杂而又精密的仪器,飞行器的损耗主要集中在发射和降落的过程,科学家根据实验数据估计,如果在发射过程中,
产生了 x 程度的损耗,那么在降落的过程中就会产生 x2 程度的损耗,如果飞船的总损耗超过了它的耐久度,飞行器就会爆炸坠毁。
问一艘耐久度为 h 的飞行器,假设在飞行过程中不产生损耗,那么为了保证其可以安全的到达目的地,只考虑整数解,至多发射过程中可以承受多少程度的损耗?
输入描述:
每个输入包含一个测试用例。每个测试用例包含一行一个整数 h (1 <= h <= 10^18)。
输出描述:
输出一行一个整数表示结果。
示例1
输入

10
输出

2

思路:

解方程题。使用Math.pow(h,0.5)求h的开方。然后比较x*(x-1)与开方数即可,如果大则返回x-1,否则返回x

 1 import java.util.*;
 2 public class Xingjichuanyue {
 3     public static long mosun(long h){
 4         long ans = 0;
 5         long x = (long)Math.pow(h, 0.5);
 6         
 7         if(x*(x+1)>h) return x-1;
 8         else return x;
 9     }
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner sc = new Scanner(System.in);
13         long h = sc.nextLong();
14         System.out.println(mosun(h));
15         //System.out.println(Math.sqrt(h));
16         //System.out.println(Math.pow(h, 0.5));
17     }
18 
19 }
原文地址:https://www.cnblogs.com/zlz099/p/8523383.html