100c之44:求四个分数

Table of Contents

问题

求四个分数且满足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
posted @ 2013-07-01 17:04  emacsun  阅读(237)  评论(0编辑  收藏  举报