数学方法,解一元二次方程,o(1)复杂度。
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = (int) ((-1 + Math.sqrt(1 + 8 * n)) / 2); while (k * k + k - 2 * n < 0) { k++; } System.out.println(k); } }