Java練習(xí)——編寫(xiě)一個(gè)應(yīng)用程序求滿足1+2!+3!+……+n!<=9995的最大整數(shù)n。


package program;
public class Integer {
public static void main(String[] args) {
? ? ? ? int res = 0;
? ? ? ? int i = 1;
? ? ? ? while (res <= 9995) {
? ? ? ? ? ? res += t(i++);
? ? ? ? }
? ? ? ? System.out.printf("n的值:%d",i=i-2);
? ? }
? ? public static int t(int n) {
? ? ? ? int ans = 1;
? ? ? ? for (int i = 1; i <= n; i++) {
? ? ? ? ? ? ans *= i;
? ? ? ? }
? ? ? ? return ans;
? ? }
}


標(biāo)簽: