JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:Quiz [Re:T55555]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-02-28 04:33 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
My first answer are like:

Mmmm...
I like brain exercice.

There are 9 casese to hold 9 digits.
So the total combination possible are
9! = 362880

The program consists of find all possible
combinations then check the equation.

BTW: It is better do not use floating division operation,
that may be will lose precision. (==> incorrect result)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Author: T55555 
//Date: 2002-12-31 
 
public class Test { 
 
  public static int COUNT = 0; 
 
  public static void check(int[] t) { 
    COUNT++; 
    int denominator1 = t[1]*10 + t[2]; 
    int denominator2 = t[4]*10 + t[5]; 
    int denominator3 = t[7]*10 + t[8]; 
    if ( (t[0] * denominator2 * denominator3 + 
          t[3] * denominator1 * denominator3 + 
          t[6] * denominator1 * denominator2) == 
         (denominator1 * denominator2 * denominator3) ) { 
      System.out.println(t[0] + "/" + t[1] + t[2] + " + " + 
                         t[3] + "/" + t[4] + t[5] + " + " + 
                         t[6] + "/" + t[7] + t[8] + " = 1"); 
    } 
  } 
 
  public static void findNext(int[] t, int position, int n) { 
    if (position == n) { 
      check(t); 
      return; 
    } 
 
    possibleValue: for (int v = 1; v <= n; v++) { 
      // check the value already used 
      for (int i = position-1; i >= 0; i--) { 
        if (t[ i ] == v) { 
          continue possibleValue; 
        } 
      } 
      t[position] = v; 
      findNext(t, position+1, n); 
    } 
  } 
  
  public static void main(String[] args) { 
    findNext(new int[9], 0, 9); 
    System.out.println("Total check: " + COUNT); 
  } 
} 


After compile and run the program, here is the result:

1
2
3
4
5
6
7
5/34 + 7/68 + 9/12 = 1  
5/34 + 9/12 + 7/68 = 1  
7/68 + 5/34 + 9/12 = 1  
7/68 + 9/12 + 5/34 = 1  
9/12 + 5/34 + 7/68 = 1  
9/12 + 7/68 + 5/34 = 1  
Total check: 362880 


Still not fast enough...
See my second answer ...


T55555 edited on 2004-02-28 07:40
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
11895 [精華] [教學]Quiz T55555 685 2004-02-28 04:26
10165 Re:Quiz T55555 1969 2004-02-28 04:33
10679 Re:Quiz T55555 2322 2004-02-28 04:41
10036 Re:Quiz sungo 26 2004-02-28 06:05
10074 Re:Quiz T55555 175 2004-02-28 06:14
10142 Re:Quiz sungo 295 2004-02-28 06:25
11049 Re:Quiz worookie 886 2004-02-28 08:36
12607 Re:Quiz jini 938 2004-02-28 12:41
» JWorld@TW »  Java 程式分享區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8