java 板


LINE

這是作業 一直卡住好煩啊 拜託大家給點意見QQ 題目: Your program (Clustering.java) must acquire the input file name from the command line (args[0]) and then open it. The first line of the input file specifies the number of points (N), followed by the 2-dimensional coordinates of the N points. The clustering procedures are described as follows: Step 0: Treat each point as a cluster; Step 1: Find the nearest pair of clusters (a, b) among the remaining N clusters Step 2: Create a new cluster, c, of which the coordinates are the centroid of all the points it contains after merging the clusters a and b; Step 3: Delete the two old clusters: a and b; Step 4: N = N - 1; Step 5: Re-calculate the distance of the new cluster, c, to other remaining clusters; Step 6: Go to Step 1 unless N = 3; Step 7: For each point in each cluster, find the nearest point in different cluster. e.g cluster A has 2 points a1, a2. cluster B has 2 points b1, b2, cluster C has 2 points c1, c2. compare the distance (a1, b1), (a1, b2), (a1, c1), (a1, c2), (a2, b1), (a2, b2), (a2, c1), (a2, c2), (b1, c1) and (b1, c2). print the smallest distance. 簡易的翻譯一下 就是題目會給N個點,要找出其中有最短距離的兩點,把那兩點以兩點的重心取代 這樣就會變成N-1個點,一直做下去,直到N=3,並輸出那三個點 以下是程式碼 import static java.awt.geom.Point2D.distance; import java.util.*; class CV implements Comparable<CV> { public Point2D[] p1 = new Point2D[1]; public Point2D[] p2 = new Point2D[1]; public double cc; public CV(Point2D p1, Point2D p2, double cc) { this.p1[0] = p1; this.p2[0] = p2; this.cc = cc; } public Point2D[] getp1() { return this.p1; } public Point2D[] getp2() { return this.p2; } public int compareTo(CV that) { if (this.cc > that.cc) { return 1; } if (this.cc < that.cc) { return -1; } return 0; } } public class Clustering { /** * @param args the command line arguments */ public static Point2D[] shortist(Point2D[] oh) { MinPQ<CV> distence = new MinPQ(); MinPQ f = new MinPQ(); for (int i = 0; i < oh.length; i++) { for (int j = i + 1; j < oh.length; j++) { double dd = oh[i].distanceTo(oh[j]); CV a = new CV(oh[i], oh[j], dd); distence.insert(a); } } Point2D[] output = new Point2D[oh.length - 1]; CV result = distence.delMin(); StdDraw.setPenColor(StdDraw.RED); StdDraw.setPenRadius(.02); result.p1[0].drawTo(result.p2[0]); double new_x = (result.p1[0].x() + result.p2[0].x()) / 2; double new_y = (result.p1[0].y() + result.p2[0].y()) / 2; output[0] = new Point2D(new_x, new_y); int output_count = 1; for (int i = 0; i < oh.length; i++) { if ((oh[i].equals(result.p1[0])) | (oh[i].equals(result.p2[0]))) { } else { output[output_count] = oh[i]; output_count++; } } int r = (int) (Math.random() * 255 + 1); int g = (int) (Math.random() * 255 + 1); int b = (int) (Math.random() * 255 + 1); System.out.println(r); StdDraw.setPenColor(r, g, b); StdDraw.setPenRadius(0.05); for (int i = 0; i < output.length; i++) { System.out.println(output[i]); output[i].draw(); } return output; } public static void main(String[] args) { In in = new In(args[0]); int N = Integer.valueOf(in.readLine()); Point2D[] cluster = new Point2D[N]; int N_3 = N; int input_count = 0; String line; MinPQ show1 = new MinPQ(); while ((line = in.readLine()) != null) { cluster[input_count] = new Point2D(Double.valueOf(line.split(" ")[0]), Double.valueOf(line.split(" ")[1])); show1.insert(cluster[input_count]); input_count++; } while (N_3 > 3) { cluster = shortist(cluster); N_3--; } } } 可是我算出來的就是怪怪的 http://imgur.com/xf3CexM 這是正確的圖 http://imgur.com/QMJ5WIi 這是我的圖 有點像可是又不對QQ --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.94.192
※ 文章網址: https://webptt.com/m.aspx?n=bbs/java/M.1463289499.A.D57.html
1F:推 galois: 測資給一下好嗎 05/16 07:51
2F:推 steven11329: 你要不要先看還沒有跟其它cluster連結的時候圖長什 05/16 08:49
3F:→ steven11329: 麼樣子? 05/16 08:49
4F:→ steven11329: 感覺好像有漏資料。 05/16 08:49
5F:→ Souseasou3: 原來作業可以來這問 這根本伸手牌 05/21 16:33
6F:→ arethusa99: 不負責任猜測 題目意思是要考慮權重的 05/23 01:17
7F:→ arethusa99: 假設C 是融合A+B之後 那他的權重是2 D是1 05/23 01:18
8F:→ arethusa99: C跟D做融合後 新生的E點不應該在正中間 05/23 01:18
9F:→ arethusa99: 應該是要考慮權重 稍微偏向C才對 05/23 01:19
10F:→ arethusa99: 這件事情可以從圖中右上角觀察到 第二小的點 05/23 01:19
11F:→ arethusa99: 你的在中間 他的在偏向兩個點的位置 05/23 01:19
12F:→ arethusa99: 然後另一個端倪在Step 2 05/23 01:20
13F:→ arethusa99: 很特地跟你說all the points 感覺上是cluster內 05/23 01:20
14F:→ arethusa99: 可能不只擁有一個point不然直接跟你說a,b中心就好 05/23 01:20







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP