package xiangqi2;
public class Test3 {
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
int mark1=0;
int mark2=0;
int mark3=0;
int mark4=0;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (mark1==0&&s.length() >= 4 && s.substring(0, 4).equals("3.14")) {
mark1++;
System.out.println(time);
}
if (mark2==0&&s.length() >= 5 && s.substring(0, 5).equals("3.141")) {
mark2++;
System.out.println(time);
}
if (mark3==0&&s.length() >= 6 && s.substring(0, 6).equals("3.1415")) {
mark3++;
System.out.println(time);
}
if (mark4==0&&s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
mark4++;
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
==================
结果:
119
1696
10188
159784
public class Test3 {
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
=================
结果是:
159784