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

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 益智程式 - Yoshi and Porcorny - 解一
worookie

Small Ship

版主

發文: 2092
積分: 21
於 2003-08-16 02:39 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
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.Vector;
 
/**
 * 2<=a,b<=100 <br>
 * S knows a+b <br>
 * P knows a*b <br>
 * S: I don't know a nor b, and I know you don't know either <br>
 * P: now I know <br>
 * S: now I know <br>
 * What are a and b?
 */
class SumProd {
 
  final int MAX = 100;
  final int MAX_SUM = MAX + MAX;
  final int MAX_PROD = MAX * MAX;
  Try tries [][];
  SPAbstract sums [];
  SPAbstract prods [];
 
  public static void main(String[] args) {
    new SumProd();
  }
 
  public SumProd() {
    sums = new SPAbstract[MAX_SUM];
    for (int i=3; i<MAX_SUM; i++) {
      sums[i] = new SPAbstract() {
        protected void unsetCandidateTries(boolean removeProdTry) {
          isCandidate = false;
          for (int i=0, total=this.tries.size(); i<total; i++) {
            Try tryIter = (Try) this.tries.elementAt(i);
            tryIter.isCandidate = false;
            if (removeProdTry)
                prods[tryIter.prod()-1].tries.remove(tryIter);
          }
        }};
      sums[i].value = i + 1;
    }
 
    prods = new SPAbstract[MAX_PROD];
    for (int i=3; i<MAX_PROD; i++) {
      prods[i] = new SPAbstract() {
         protected void unsetCandidateTries(boolean removeSumTry) {
            isCandidate = false;
            for (int i=0, total=this.tries.size(); i<total; i++) {
              Try tryIter = (Try) this.tries.elementAt(i);
              tryIter.isCandidate = false;
              if (removeSumTry)
                sums[tryIter.sum()-1].tries.remove(tryIter);
            }
          }
      };
      prods[i].value = i + 1;
    }
 
    tries = new Try[MAX][MAX];
    for (int a=1; a<MAX; a++)
        for (int b=a; b<MAX; b++)
          tries[a][b] = new Try(a+1, b+1);
 
    for (int i=3; i<MAX_SUM; i++)
      sums[i].validateHasMoreThanOneTryCandidate();
    for (int i=3; i<MAX_PROD; i++)
      prods[i].validateHasMoreThanOneTryCandidate();
    for (int i=3; i<MAX_SUM; i++)
      sums[i].validateAllTriesCandidate();
    for (int i=3; i<MAX_PROD; i++)
      prods[i].validateOnlyOneTryCandidate();
    for (int i=3; i<MAX_SUM; i++)
      sums[i].validateOnlyOneTryCandidate();
    for (int i=3; i<MAX_PROD; i++)
      prods[i].validateAtLeastOneTryCandidate();
 
    System.out.println(debugString());
  }
 
  public String debugString() {
    StringBuffer sb = new StringBuffer();
 
    int totalSumCandidates = 0;
      for (int i=3; i<MAX_SUM; i++)
        if (sums[i].isCandidate) {
          totalSumCandidates++;
            sb.append("sum: ").append(sums[i]);
        }
    sb.append("total sum candidates: ").append(totalSumCandidates).append("\n\n");
 
    int totalProdCandidates = 0;
      for (int i=3; i<MAX_PROD; i++)
        if (prods[i].isCandidate) {
          totalProdCandidates++;
            sb.append("prod: ").append(prods[i]);
        }
    sb.append("total prod candidates: ").append(totalProdCandidates).append("\n\n");
 
    int totalTryCandidates = 0;
    for (int i=1; i<MAX; i++)
      for (int j=i; j<MAX; j++)
        if (tries[i][j].isCandidate) {
          totalTryCandidates++;
            sb.append("tries(").append(i+1).append(")(").append(j+1).append(")");
        }
    sb.append("\ntotal try candidates: ").append(totalTryCandidates).append("\n");
 
    return sb.toString();
  }
 
  private abstract class SPAbstract {
    int value;
    Vector tries = new Vector();
    boolean isCandidate = true;
 
    private boolean hasTryCandidates() {
      return isCandidate && (isCandidate = tries != null && tries.size() > 0);
    }
 
    private int calcTriesCandidates() {
      int totalCandidateTries = 0;
      for (int i=0, total=tries.size(); i<total; i++) {
        Try tryIter = (Try) tries.elementAt(i);
        if (tryIter.isCandidate)
            totalCandidateTries++;
      }
      return totalCandidateTries;
    }
 
    protected abstract void unsetCandidateTries(boolean b);
 
    private void validateHasMoreThanOneTryCandidate() {
        if (hasTryCandidates() && (calcTriesCandidates() <= 1))
            unsetCandidateTries(false);
    }
 
    private void validateOnlyOneTryCandidate() {
      if (hasTryCandidates() && (calcTriesCandidates() != 1))
          unsetCandidateTries(true);
    }
 
    private void validateAtLeastOneTryCandidate() {
      if (hasTryCandidates() && (calcTriesCandidates() == 0))
          unsetCandidateTries(true);
    }
 
    private void validateAllTriesCandidate() {
      if (hasTryCandidates() && (calcTriesCandidates() != tries.size()))
          unsetCandidateTries(true);
    }
    public String toString() {
      return "value: " + value + "\ttries.size: " + tries.size() + "\tisCandidate: " + isCandidate + "\n";
    }
  }
 
  private class Try {
    boolean isCandidate = true;
    int a;
    int b;
    public Try(int a, int b) {
      this.a = a;
      this.b = b;
      int sum = a+b;
      sums[sum-1].tries.add(this);
      int prod = a*b;
      prods[prod-1].tries.add(this);
    }
    public int sum() { return a+b; }
    public int prod() { return a*b; }
    public String toString() {
      return "a: " + a + " b: " + b + "\n";
    }
  }
}


worookie edited on 2003-08-21 05:11
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1877 益智程式 - Yoshi and Porcorny - 解一 worookie 5249 2003-08-16 02:39
1562 Re:益智程式 - Yoshi and Porcorny - 解二 worookie 2399 2003-08-16 02:42
» JWorld@TW »  Java SE 討論區

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