|
C语言趣味程序百例精解之java实现(44)分数之和:
程序:public class Test44{
public static void main(String args[]){
int s,count=0;
System.out.printf("The 4 fractions which sum is equal 1 are:\n");
for(int p=2;p<5;p++)
for(int q=p;q<7;q++)
for(int r=q;r<13;r++)
if(p*q*r-q*r-p*r-p*q!=0){
s=(p*q*r)/(p*q*r-q*r-p*r-p*q);
if(((p*q*r)%(p*q*r-q*r-p*r-p*q)==0)&&s>=r)
System.out.printf("[%2d] 1/%d+1/%d+1/%d+1/%d=1\n",++count,p,q,r,s);
}
}
}运行结果:
C:\java>java Test44
The 4 fractions which sum is equal 1 are:
[ 1] 1/2+1/3+1/7+1/42=1
[ 2] 1/2+1/3+1/8+1/24=1
[ 3] 1/2+1/3+1/9+1/18=1
[ 4] 1/2+1/3+1/10+1/15=1
[ 5] 1/2+1/3+1/12+1/12=1
[ 6] 1/2+1/4+1/5+1/20=1
[ 7] 1/2+1/4+1/6+1/12=1
[ 8] 1/2+1/4+1/8+1/8=1
[ 9] 1/2+1/5+1/5+1/10=1
[10] 1/2+1/6+1/6+1/6=1
[11] 1/3+1/3+1/4+1/12=1
[12] 1/3+1/3+1/6+1/6=1
[13] 1/3+1/4+1/4+1/6=1
[14] 1/4+1/4+1/4+1/4=1 |
|