每个输入包含一个测试用例。每个测试用例包含一行一个整数 h (1 <= h <= 10^18)。
输出一行一个整数表示结果。
10
2
1
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { long num = in.nextLong(); long x = (long)Math.pow(num, 0.5); if(x * (x + 1) > num) { System.out.println(x - 1); } else { System.out.println(x); } } } }
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { long long h; cin >> h; cout << (int)floor( ( sqrt((double)(1 + 4 * h)) - 1 ) / 2 ) << endl; return 0; }
// 直接解方程 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ long n = sc.nextLong(); long k = (int)Math.sqrt(n); if(k*(k+1)<=n){ System.out.println(k); }else{ System.out.println(k-1); } } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Long h = Long.parseLong(br.readLine()); long left = 0, right = (long)Math.sqrt(h) + 1, x = 0; while(left < right){ long mid = left + ((right - left) >> 1); if(mid * (mid + 1) > h){ right = mid - 1; }else{ x = mid; left = mid + 1; } } System.out.println(x); } }
import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { /** *这一题就是考察最接近h的sum (=X + X * X); *限制h最大为10^18,我们知道int约为2*10^9,long是9*10^18,所以我们的数据类型为long *使用sqrt就可以了,开方取整,如果取整后(X + X * X)恰等于h,那么就取X,否则我们就取小于X的一位就可以了, *因为 (a - 1)^2 = a^2 - 2*a + 1 < a^2 - a < a^2 < a^a + a *a减一已经能够让出一个a了 */ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long num = Long.parseLong(br.readLine().trim()); long numSqrt = (long)Math.sqrt(num); long tolerance = (numSqrt + 1) * numSqrt <= num ? numSqrt : (numSqrt -1); System.out.println(tolerance); } }
简单的n^2+n<=h 一直h求n的最大值 ---->n<=((4h+1)开根号-1)/2 public class Program4 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); long a=scanner.nextLong(); System.out.println(((long)Math.sqrt(4*a+1)-1)/2); } }
/*没有用库函数,二分查找,一个技巧就是通过位数快速压缩第一次查找的区间*/ import java.util.Scanner; public class Main{ // 寻找最大的x整数解使得x(x+1)<=h public static void main(String[] args) { // 考虑一个2位数,最大为99,则99*100=9900,最多也到不了5位数 // 因此,当h为k位数时,x的位数不会超过(k+1)/2,但是也不会低于(k+1)/2-1 Scanner scanner = new Scanner(System.in); long h = scanner.nextLong(); scanner.close(); if(h<2){ System.out.println(0); return; } String hString = String.valueOf(h); int bit = hString.length(); int maxbit = (bit + 1) / 2, minbit = maxbit - 1; long left = (long) Math.pow(10, minbit), right = (long) Math.pow(10, maxbit + 1); long mid=(left+right)/2; while(left<right){ long pivot=mid*(mid+1); if(pivot>h){ right=mid; mid=(left+right)/2; }else{ long newpivot=(mid+1)*(mid+2); if(newpivot>h) break; left=mid; mid=(left+right)/2; } } System.out.println(mid); } }
/* 直接对h求算术平方根向下取整 需要注意的是定义h变量要用long,如果使用int会超出范围 */ import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); long h=sc.nextLong(); test(h); } public static void test(long h){ long a=(long)Math.floor(Math.sqrt(h)); for(long i=a;i>=0;i--){ if((i*i+i)<=h){ System.out.println(i); break; } } } }
import java.util.Scanner;
public class Interstellar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
long m=sc.nextLong();
for(long i=(long) Math.floor(Math.sqrt(m))-1;i<=Math.pow(10, 9);i++) {
if(i*(i+1)>m) {
System.out.println(i-1);
break;
}
}
}
sc.close();
}
}