100c之44:求四个分数
问题
求四个分数且满足1/p + 1/q + 1/r + 1/s =1,其中p<=q<=r<=s.
分析
p的范围是[2,4], q的范围是[p,7],r的范围是[q,13].
程序
1: /** 2: * @file 044c.c 3: * @author Chaolong Zhang <emacsun@163.com> 4: * @date Mon Jul 1 16:33:22 2013 5: * 6: * @brief 求四个分数且满足1/p + 1/q + 1/r + 1/s =1,其中p<=q<=r<=s. 7: * 8: * 9: */ 10: 11: #include <stdio.h> 12: 13: int main(int argc, char *argv[]) 14: { 15: int p,q,r,s; 16: int count=0; 17: 18: for (p=2; p <= 4; ++p){ 19: for ( q= p; q <= 7; ++q){ 20: for (r=q; r <= 13; ++r){ 21: if (p*q*r - p*q - p*r- q*r != 0){ 22: s=p*q*r/( p*q*r - p*q - p*r- q*r ); 23: if (0==p*q*r% ( p*q*r - p*q - p*r- q*r ) && s>=r) 24: { 25: printf ("%-4d 1/%-2d + 1/%-2d + 1/%-2d + 1/%-2d = 1\n", ++count, p,q,r,s); 26: } 27: } 28: } 29: } 30: } 31: return 0; 32: }
结果
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
作者:emacsun
出处:http://www.cnblogs.com/chaolong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。