K-means算法的java实现,聚类分析681个三国武将

来源:互联网 发布:lpl韩国外援知乎 编辑:程序博客网 时间:2024/04/29 05:21

转载地址:http://blog.csdn.net/lufeng20/article/details/7542955

目录(?)[+]

一,k-means算法介绍:  

k-means算法接受输入量 k ;然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高;而不同聚类中的对象相似度较小。聚类相似度是利用各聚类中对象的均值所获得一个“中心对象”(引力中心)来进行计算的。k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。 k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。

k-means算法的工作过程说明如下:首先从n个数据对象任意选择 k 个对象作为初始聚类中心;而对于所剩下其它对象,则根据它们与这些聚类中心的相似度(距离),分别将它们分配给与其最相似的(聚类中心所代表的)聚类;然后再计算每个所获新聚类的聚类中心(该聚类中所有对象的均值);不断重复这一过程直到标准测度函数开始收敛为止。一般都采用均方差作为标准测度函数。k个聚类具有以下特点:各聚类本身尽可能的紧凑,而各聚类之间尽可能的分开。

二,k-means算法基本步骤:

  (1) 从 n个数据对象任意选择 k 个对象作为初始聚类中心;
  (2) 根据每个聚类对象的均值(中心对象),计算每个对象与这些中心对象的距离;并根据最小距离重新对相应对象进行划分;
  (3) 重新计算每个(有变化)聚类的均值(中心对象);
  (4) 计算标准测度函数,当满足一定条件,如函数收敛时,则算法终止;如果条件不满足则回到步骤(2),不断重复直到标准测度函数开始收敛为止。(一般都采用均方差作为标准测度函数。)

三,k-means算法的java实现:

  一共有七个类,General.java代表武将对象, Distance.java距离类计算各个武将到中心武将之间的距离, Cluster.java聚类对象包含一个中心武将和该聚类中所有武将, Kmeans.java核心的聚类算法类, Tool.java工具类用于转换武将的星级为数字等操作, TestKmeans.java测试类即入口文件, DomParser.java用于读取xml中的681个武将。

具体思路:先从general.xml文件中读取681个武将,然后随机选取初始类中心,计算各个武将到中心武将的距离,根据最小的距离进行聚类,然后重新根据平均值新的聚类的类中心,重新计算各个武将到新的中心武将的距离,直到更新后的聚类与原来的聚类包含的武将不再改变,即收敛时结束。

具体代码如下:

1,General.java

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. public class General {  
  4.       
  5.     private String name; // 姓名  
  6.     private int render; // 星级  
  7.     private int tongshai; // 统帅  
  8.     private int wuli; // 武力  
  9.     private int zhili; // 智力  
  10.     private int polic; // 政治  
  11.     private int qiangbin; // 枪兵  
  12.     private int jibin; // 戟兵  
  13.     private int nubin; // 弩兵  
  14.     private int qibin; // 骑兵  
  15.     private int binqi; // 兵器  
  16.     private int tongwu; // 统武  
  17.     private int tongzhi; // 统智  
  18.     private int tongwuzhi; // 统武智  
  19.     private int tongwuzhizheng; // 统武智政  
  20.     private int salary; // 50级工资  
  21.   
  22.     public General(int render, String name, int tongshai, int wuli, int zhili,  
  23.             int polic, int qiangbin, int jibin, int nubin, int qibin,  
  24.             int binqi, int tongwu, int tongzhi, int tongwuzhi,  
  25.             int tongwuzhizheng, int salary) {  
  26.         super();  
  27.         this.name = name;  
  28.         this.render = render;  
  29.         this.tongshai = tongshai;  
  30.         this.wuli = wuli;  
  31.         this.zhili = zhili;  
  32.         this.polic = polic;  
  33.         this.qiangbin = qiangbin;  
  34.         this.jibin = jibin;  
  35.         this.nubin = nubin;  
  36.         this.qibin = qibin;  
  37.         this.binqi = binqi;  
  38.         this.tongwu = tongwu;  
  39.         this.tongzhi = tongzhi;  
  40.         this.tongwuzhi = tongwuzhi;  
  41.         this.tongwuzhizheng = tongwuzhizheng;  
  42.         this.salary = salary;  
  43.     }  
  44.   
  45.     public General(int render, int tongshai, int wuli, int zhili, int polic,  
  46.             int qiangbin, int jibin, int nubin, int qibin, int binqi,  
  47.             int tongwu, int tongzhi, int tongwuzhi, int tongwuzhizheng,  
  48.             int salary) {  
  49.         super();  
  50.         this.name = "聚类中心";  
  51.         this.render = render;   
  52.         this.tongshai = tongshai;   
  53.         this.wuli = wuli;  
  54.         this.zhili = zhili;  
  55.         this.polic = polic;  
  56.         this.qiangbin = qiangbin;  
  57.         this.jibin = jibin;  
  58.         this.nubin = nubin;  
  59.         this.qibin = qibin;  
  60.         this.binqi = binqi;  
  61.         this.tongwu = tongwu;  
  62.         this.tongzhi = tongzhi;  
  63.         this.tongwuzhi = tongwuzhi;  
  64.         this.tongwuzhizheng = tongwuzhizheng;  
  65.         this.salary = salary;  
  66.     }  
  67.   
  68.     public General() {  
  69.     }  
  70.   
  71.     @Override  
  72.     public String toString() {  
  73.         return "武将 [name=" + name + ", render=" + Tool.dxingji(render)  
  74.                 + ", tongshai=" + tongshai + ", wuli=" + wuli + ", zhili="  
  75.                 + zhili + ", polic=" + polic + ", qiangbin="  
  76.                 + Tool.dchange(qiangbin) + ", jibin=" + Tool.dchange(jibin)  
  77.                 + ", nubin=" + Tool.dchange(nubin) + ", qibin="  
  78.                 + Tool.dchange(qibin) + ", binqi=" + Tool.dchange(binqi)  
  79.                 + ", tongwu=" + tongwu + ", tongzhi=" + tongzhi  
  80.                 + ", tongwuzhi=" + tongwuzhi + ", tongwuzhizheng="  
  81.                 + tongwuzhizheng + ", salary=" + salary + "]";  
  82.     }  
  83.   
  84.     public String getName() {  
  85.         return name;  
  86.     }  
  87.   
  88.     public void setName(String name) {  
  89.         this.name = name;  
  90.     }  
  91.   
  92.     public int getRender() {  
  93.         return render;  
  94.     }  
  95.   
  96.     public void setRender(int render) {  
  97.         this.render = render;  
  98.     }  
  99.   
  100.     public int getTongshai() {  
  101.         return tongshai;  
  102.     }  
  103.   
  104.     public void setTongshai(int tongshai) {  
  105.         this.tongshai = tongshai;  
  106.     }  
  107.   
  108.     public int getWuli() {  
  109.         return wuli;  
  110.     }  
  111.   
  112.     public void setWuli(int wuli) {  
  113.         this.wuli = wuli;  
  114.     }  
  115.   
  116.     public int getZhili() {  
  117.         return zhili;  
  118.     }  
  119.   
  120.     public void setZhili(int zhili) {  
  121.         this.zhili = zhili;  
  122.     }  
  123.   
  124.     public int getPolic() {  
  125.         return polic;  
  126.     }  
  127.   
  128.     public void setPolic(int polic) {  
  129.         this.polic = polic;  
  130.     }  
  131.   
  132.     public int getQiangbin() {  
  133.         return qiangbin;  
  134.     }  
  135.   
  136.     public void setQiangbin(int qiangbin) {  
  137.         this.qiangbin = qiangbin;  
  138.     }  
  139.   
  140.     public int getJibin() {  
  141.         return jibin;  
  142.     }  
  143.   
  144.     public void setJibin(int jibin) {  
  145.         this.jibin = jibin;  
  146.     }  
  147.   
  148.     public int getNubin() {  
  149.         return nubin;  
  150.     }  
  151.   
  152.     public void setNubin(int nubin) {  
  153.         this.nubin = nubin;  
  154.     }  
  155.   
  156.     public int getQibin() {  
  157.         return qibin;  
  158.     }  
  159.   
  160.     public void setQibin(int qibin) {  
  161.         this.qibin = qibin;  
  162.     }  
  163.   
  164.     public int getBinqi() {  
  165.         return binqi;  
  166.     }  
  167.   
  168.     public void setBinqi(int binqi) {  
  169.         this.binqi = binqi;  
  170.     }  
  171.   
  172.     public int getTongwu() {  
  173.         return tongwu;  
  174.     }  
  175.   
  176.     public void setTongwu(int tongwu) {  
  177.         this.tongwu = tongwu;  
  178.     }  
  179.   
  180.     public int getTongzhi() {  
  181.         return tongzhi;  
  182.     }  
  183.   
  184.     public void setTongzhi(int tongzhi) {  
  185.         this.tongzhi = tongzhi;  
  186.     }  
  187.   
  188.     public int getTongwuzhi() {  
  189.         return tongwuzhi;  
  190.     }  
  191.   
  192.     public void setTongwuzhi(int tongwuzhi) {  
  193.         this.tongwuzhi = tongwuzhi;  
  194.     }  
  195.   
  196.     public int getTongwuzhizheng() {  
  197.         return tongwuzhizheng;  
  198.     }  
  199.   
  200.     public void setTongwuzhizheng(int tongwuzhizheng) {  
  201.         this.tongwuzhizheng = tongwuzhizheng;  
  202.     }  
  203.   
  204.     public int getSalary() {  
  205.         return salary;  
  206.     }  
  207.   
  208.     public void setSalary(int salary) {  
  209.         this.salary = salary;  
  210.     }  
  211.   
  212. }  


2,Distance.java 

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2. /** 
  3.  * 这个类用于计算距离的。。 
  4.  * 
  5.  */  
  6. public class Distance {  
  7.     int dest;// 目的  
  8.     int source;// 源  
  9.     double dist;// 欧式距离  
  10.   
  11.     public int getDest() {  
  12.         return dest;  
  13.     }  
  14.   
  15.     public void setDest(int dest) {  
  16.         this.dest = dest;  
  17.     }  
  18.   
  19.     public int getSource() {  
  20.         return source;  
  21.     }  
  22.   
  23.     public void setSource(int source) {  
  24.         this.source = source;  
  25.     }  
  26.   
  27.     public double getDist() {  
  28.         return dist;  
  29.     }  
  30.   
  31.     public void setDist(double dist) {  
  32.         this.dist = dist;  
  33.     }  
  34.     /** 
  35.      * 计算源和目的的距离 
  36.      * @param dest 目的武将 
  37.      * @param source 源武将 
  38.      * @param dist 两者间的距离 
  39.      */  
  40.     public Distance(int dest, int source, double dist) {  
  41.         this.dest = dest;  
  42.         this.source = source;  
  43.         this.dist = dist;  
  44.     }  
  45.   
  46.     public Distance() {  
  47.     }  
  48.   
  49. }  


3,Cluster.java

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class Cluster {  
  6.     private int center;// 聚类中心武将的id  
  7.     private ArrayList<General> ofCluster = new ArrayList<General>();// 属于这个聚类的武将的集合  
  8.   
  9.     public int getCenter() {  
  10.         return center;  
  11.     }  
  12.   
  13.     public void setCenter(int center) {  
  14.         this.center = center;  
  15.     }  
  16.   
  17.     public ArrayList<General> getOfCluster() {  
  18.         return ofCluster;  
  19.     }  
  20.   
  21.     public void setOfCluster(ArrayList<General> ofCluster) {  
  22.         this.ofCluster = ofCluster;  
  23.     }  
  24.   
  25.     public void addGeneral(General general) {  
  26.         if (!(this.ofCluster.contains(general)))  
  27.             this.ofCluster.add(general);  
  28.     }  
  29. }  



4,Kmeans.java

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. import java.util.*;  
  4.   
  5. public class Kmeans {  
  6.     public ArrayList<General> allGenerals = null;  
  7.     public int totalNumber = 0;// 得到所有的武将数目  
  8.     public int K = 0;// 假设K=10  
  9.   
  10.     public Kmeans() {  
  11.         allGenerals = new DomParser().prepare();  
  12.         totalNumber = allGenerals.size();  
  13.         K = 3;  
  14.     }  
  15.   
  16.     // 第一次随机选取聚类中心  
  17.     public Set<Integer> firstRandom() {  
  18.         Set<Integer> center = new HashSet<Integer>();// 聚类中心的点的id,采用set保证不会有重复id  
  19.         Random ran = new Random();  
  20.         int roll = ran.nextInt(totalNumber);  
  21.         while (center.size() < K) {  
  22.             roll = ran.nextInt(totalNumber);  
  23.             center.add(roll);  
  24.         }  
  25.         return center;  
  26.     }  
  27.   
  28.     // 根据聚类中心初始化聚类信息  
  29.     public ArrayList<Cluster> init(Set<Integer> center) {  
  30.         ArrayList<Cluster> cluster = new ArrayList<Cluster>();// 聚类 的数组  
  31.         Iterator<Integer> it = center.iterator();  
  32.         while (it.hasNext()) {  
  33.             Cluster c = new Cluster();// 代表一个聚类  
  34.             c.setCenter(it.next());  
  35.             cluster.add(c);  
  36.         }  
  37.         return cluster;  
  38.     }  
  39.   
  40.     /** 
  41.      * 计算各个武将到各个聚类中心的距离,重新聚类 
  42.      *  
  43.      * @param cluster 
  44.      *            聚类数组,用来聚类的,根据最近原则把武将聚类 
  45.      * @param center 
  46.      *            中心点id,用于计算各个武将到中心点的距离 return cluster 聚类后的所有聚类组成的数组 
  47.      */  
  48.     public ArrayList<Cluster> juLei(Set<Integer> center,  
  49.             ArrayList<Cluster> cluster) {  
  50.         ArrayList<Distance> distence = new ArrayList<Distance>();// 存放距离信息,表示每个点到各个中心点的距离组成的数组  
  51.         General source = null;  
  52.         General dest = null;  
  53.         int id = 0;// 目的节点id  
  54.         int id2 = 0;// 源节点id  
  55.         Object[] p = center.toArray();// p 为聚类中心点id数组  
  56.         boolean flag = false;  
  57.         // 分别计算各个点到各个中心点的距离,并将距离最小的加入到各个聚类中,进行聚类  
  58.         for (int i = 0; i < totalNumber; i++) {  
  59.             // 每个点计算完,并聚类到距离最小的聚类中就清空距离数组  
  60.             distence.clear();  
  61.             // 计算到j个类中心点的距离,便利各个中心点  
  62.             for (int j = 0; j < center.size(); j++) {  
  63.                 // 如果该点不在中心点内 则计算距离  
  64.                 if (!(center.contains(i))) {  
  65.                     flag = true;  
  66.                     // 计算距离  
  67.                     source = allGenerals.get(i);// 某个点  
  68.                     dest = allGenerals.get((Integer) p[j]);// 各个 中心点  
  69.                     // 计算距离并存入数组  
  70.                     distence.add(new Distance((Integer) p[j], i, Tool.juli(  
  71.                             source, dest)));  
  72.                 } else {  
  73.                     flag = false;  
  74.                 }  
  75.             }  
  76.             // 说明计算完某个武将到类中心的距离,开始比较  
  77.             if (flag == true) {  
  78.                 // 排序比较一个点到各个中心的距离的大小,找到距离最小的武将的 目的id,和源id,  
  79.                 // 目的id即类中心点id,这个就归到这个中心点所在聚类中  
  80.                 double min = distence.get(0).getDist();// 默认第一个distance距离是最小的  
  81.                 // 从1开始遍历distance数组  
  82.                 int minid = 0;  
  83.                 for (int k = 1; k < distence.size(); k++) {  
  84.                     if (min > distence.get(k).getDist()) {  
  85.                         min = distence.get(k).getDist();  
  86.                         id = distence.get(k).getDest();// 目的,即类中心点  
  87.                         id2 = distence.get(k).getSource();// 某个武将  
  88.                         minid = k;  
  89.                     } else {  
  90.                         id = distence.get(minid).getDest();  
  91.                         id2 = distence.get(minid).getSource();  
  92.                     }  
  93.                 }  
  94.                 // 遍历cluster聚类数组,找到类中心点id与最小距离目的武将id相同的聚类  
  95.                 for (int n = 0; n < cluster.size(); n++) {  
  96.                     // 如果和中心点的id相同 则setError  
  97.                     if (cluster.get(n).getCenter() == id) {  
  98.                         cluster.get(n).addGeneral(allGenerals.get(id2));// 将与该聚类中心距离最小的武将加入该聚类  
  99.                         break;  
  100.                     }  
  101.                 }  
  102.             }  
  103.         }  
  104.         return cluster;  
  105.     }  
  106.   
  107.     // 产生新的聚类中心点数组  
  108.     public Set<Integer> updateCenter() {  
  109.         Set<Integer> center = new HashSet<Integer>();  
  110.         for (int i = 0; i < K; i++) {  
  111.             center.add(i);  
  112.         }  
  113.         return center;  
  114.     }  
  115.   
  116.     // 更新聚类中心, 求平均值  
  117.     public ArrayList<Cluster> updateCluster(ArrayList<Cluster> cluster) {  
  118.         ArrayList<Cluster> result = new ArrayList<Cluster>();  
  119.         // 重新产生的新的聚类中心组成的数组  
  120.         // k个聚类进行更新聚类中心  
  121.         for (int j = 0; j < K; j++) {  
  122.             ArrayList<General> ps = cluster.get(j).getOfCluster();// 该聚类的所有 武将  
  123.                                                                     // 组成的数组  
  124.             ps.add(allGenerals.get(cluster.get(j).getCenter()));// 同时将该类中心对应的武将加入该武将数组  
  125.             int size = ps.size();// 该聚类的长度大小  
  126.             // 计算和,然后在计算平均值  
  127.             int sumrender = 0, sumtongshai = 0, sumwuli = 0, sumzhili = 0, sumjibin = 0, sumnubin = 0, sumqibin = 0, sumpolic = 0, sumqiangbin = 0, sumbinqi = 0, sumtongwu = 0, sumtongzhi = 0, sumtongwuzhi = 0, sumtongwuzhizheng = 0, sumsalary = 0;  
  128.             for (int k1 = 0; k1 < size; k1++) {  
  129.                 sumrender += ps.get(k1).getRender();  
  130.                 sumtongshai += ps.get(k1).getRender();  
  131.                 sumwuli += ps.get(k1).getWuli();  
  132.                 sumzhili += ps.get(k1).getZhili();  
  133.                 sumjibin += ps.get(k1).getJibin();  
  134.                 sumnubin += ps.get(k1).getNubin();  
  135.                 sumqibin += ps.get(k1).getQibin();  
  136.                 sumpolic += ps.get(k1).getPolic();  
  137.                 sumqiangbin += ps.get(k1).getQiangbin();  
  138.                 sumbinqi += ps.get(k1).getBinqi();  
  139.                 sumtongwu += ps.get(k1).getTongwu();  
  140.                 sumtongzhi += ps.get(k1).getTongzhi();  
  141.                 sumtongwuzhi += ps.get(k1).getTongwuzhi();  
  142.                 sumtongwuzhizheng += ps.get(k1).getTongwuzhizheng();  
  143.                 sumsalary += ps.get(k1).getSalary();  
  144.             }  
  145.             // 产生新的聚类,然后加入到聚类数组中  
  146.             Cluster newCluster = new Cluster();  
  147.             newCluster.setCenter(j);  
  148.             // 计算平均值并构造新的武将对象  
  149.             newCluster.addGeneral(new General(sumrender / size, sumtongshai  
  150.                     / size, sumwuli / size, sumzhili / size, sumjibin / size,  
  151.                     sumnubin / size, sumqibin / size, sumpolic = 0,  
  152.                     sumqiangbin = 0, sumbinqi / size, sumtongwu / size,  
  153.                     sumtongzhi / size, sumtongwuzhi / size, sumtongwuzhizheng  
  154.                             / size, sumsalary / size));  
  155.             result.add(newCluster);  
  156.         }  
  157.         return result;  
  158.   
  159.     }  
  160.   
  161.     /** 
  162.      * 计算各个武将到各个更新后的聚类中心的距离,重新聚类 
  163.      * @param update 更新后的聚类中心 
  164.      * @param cluster 要存储的聚类中心 
  165.      */  
  166.     public ArrayList<Cluster> updateJuLei(ArrayList<Cluster> update,  
  167.             ArrayList<Cluster> cluster) {  
  168.         ArrayList<Distance> distence = new ArrayList<Distance>();// 存放距离信息,表示每个点到各个中心点的距离组成的数组  
  169.         General source = null;  
  170.         General dest = null;  
  171.         int id = 0;// 目的节点id  
  172.         int id2 = 0;// 源节点id  
  173.         //Object[] p = center.toArray();// p 为聚类中心点id数组  
  174.         boolean flag = false;  
  175.         // 分别计算各个点到各个中心点的距离,并将距离最小的加入到各个聚类中,进行聚类  
  176.         for (int i = 0; i < totalNumber; i++) {  
  177.             // 每个点计算完,并聚类到距离最小的聚类中就清空距离数组  
  178.             distence.clear();  
  179.             // 计算到j个类中心点的距离,便利各个中心点  
  180.             //for (int j = 0; j < center.size(); j++) {  
  181.             for (int j = 0; j < update.size(); j++) {  
  182.                 // 如果该点不在中心点内 则计算距离  
  183.                 //if (!(center.contains(i))) {  
  184.                     flag = true;  
  185.                     // 计算距离  
  186.                     source = allGenerals.get(i);// 某个点  
  187.                     // dest = allGenerals.get((Integer) p[j]);// 各个 中心点  
  188.                     dest = update.get(j).getOfCluster().get(0);// 各个 中心点  
  189.                     // 计算距离并存入数组  
  190.                     //distence.add(new Distance((Integer) p[j], i, Tool.juli(  
  191.                     distence.add(new Distance(update.get(j).getCenter(), i, Tool.juli(  
  192.                             source, dest)));  
  193.                     /*} else { 
  194.                     flag = false; 
  195.                 }*/  
  196.             }  
  197.             // 说明计算完某个武将到类中心的距离,开始比较  
  198.             if (flag == true) {  
  199.                 // 排序比较一个点到各个中心的距离的大小,找到距离最小的武将的 目的id,和源id,  
  200.                 // 目的id即类中心点id,这个就归到这个中心点所在聚类中  
  201.                 double min = distence.get(0).getDist();// 默认第一个distance距离是最小的  
  202.                 // 从1开始遍历distance数组  
  203.                 int mid = 0;  
  204.                 for (int k = 1; k < distence.size(); k++) {  
  205.                     if (min > distence.get(k).getDist()) {  
  206.                         min = distence.get(k).getDist();  
  207.                         id = distence.get(k).getDest();// 目的,即类中心点  
  208.                         id2 = distence.get(k).getSource();// 某个武将  
  209.                         mid = k;  
  210.                     } else {  
  211.                         id = distence.get(mid).getDest();  
  212.                         id2 = distence.get(mid).getSource();  
  213.                     }  
  214.                 }  
  215.                 // 遍历cluster聚类数组,找到类中心点id与最小距离目的武将id相同的聚类  
  216.                 for (int n = 0; n < cluster.size(); n++) {  
  217.                     // 如果和中心点的id相同 则setError  
  218.                     if (cluster.get(n).getCenter() == id) {  
  219.                         cluster.get(n).addGeneral(allGenerals.get(id2));// 将与该聚类中心距离最小的武将加入该聚类  
  220.                     }  
  221.                 }  
  222.             }  
  223.         }  
  224.         return cluster;  
  225.     }  
  226.   
  227.     // 不断循环聚类直到各个聚类没有重新分配  
  228.     public ArrayList<Cluster> getResult() {  
  229.         ArrayList<Cluster> result = new ArrayList<Cluster>();  
  230.         ArrayList<Cluster> temp = new ArrayList<Cluster>();  
  231.         boolean flag = false;  
  232.         // 得到随机中心点然后进行聚类  
  233.         Set<Integer> center = firstRandom();  
  234.         result = juLei(center, init(center));  
  235.         print(result);  
  236.         do {  
  237.             // 重新聚类  
  238.             ArrayList<Cluster> up = updateCluster(result);//新的聚类中心  
  239.             ArrayList<Cluster> cluster = init(updateCenter()); // 得到更新后的中心点对应的聚类数组  
  240.             temp = updateJuLei(up, cluster);  
  241.             //print(temp);  
  242.             flag = isEquals(temp, result);  
  243.             result = temp;  
  244.         } while (!flag);  
  245.         return result;  
  246.     }  
  247.       
  248.     public boolean isEquals(ArrayList<Cluster> temp, ArrayList<Cluster> result){  
  249.         boolean flag = false;  
  250.         if(temp.size() != result.size()){  
  251.             return flag;  
  252.         }  
  253.         for(Cluster tem : temp){  
  254.             for(Cluster res : result){  
  255.                 if(tem.getCenter() == res.getCenter()){  
  256.                     flag = true;  
  257.                 }  
  258.             }  
  259.             // 如果找了一轮没找到 则说明两个聚类  
  260.             if(flag == false){  
  261.                 return false;  
  262.             }else{// 如果找了一轮找到了,那么接着找  
  263.                 flag = false;  
  264.             }  
  265.         }  
  266.         //如果代码能进行到这边,说明是true  
  267.         flag = true;  
  268.         return flag;  
  269.     }  
  270.       
  271.     //输出所有的聚类  
  272.     public void print(ArrayList<Cluster> cs) {  
  273.         System.out.println("***************************************");  
  274.         for (int i = 0; i < cs.size(); i++) {  
  275.             Cluster c = cs.get(i);  
  276.             System.out.println("-----------------------------------------------------");  
  277.             System.out.println("center: " + allGenerals.get(c.getCenter()));  
  278.             ArrayList<General> p = c.getOfCluster();  
  279.             for (int j = 0; j < p.size(); j++) {  
  280.                 System.out.println("general:"+p.get(j)+"\n");  
  281.             }  
  282.         }  
  283.     }  
  284.   
  285. }  


5,Tool.java

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. public class Tool {  
  4.     //将各种武器的精通程度转为数字  
  5.     public static int change(String str) {  
  6.         int result = str.equals("精") ? 4 : (str.equals("神") ? 3 : (str  
  7.                 .equals("通") ? 2 : 1));  
  8.         return result;  
  9.     }  
  10.     //将星级转为数字  
  11.     public static int xingji(String str) {  
  12.         int result = str.equals("★★★★★") ? 5 : (str.equals("★★★★") ? 4 : (str  
  13.                 .equals("★★★") ? 3 : (str.equals("★★") ? 2 : 1)));  
  14.         return result;  
  15.     }  
  16.     //反转 将各种武器的数字转为精通程度  
  17.     public static String dchange(int str) {  
  18.         String result = str== 4 ? "精" : (str== 3 ? "神" : (str== 2 ? "通" : "疏"));  
  19.         return result;  
  20.     }  
  21.     //反转 将数字转为星级  
  22.     public static String dxingji(int str) {  
  23.         String result = str== 5 ? "★★★★★" : (str== 4 ? "★★★★" : (str== 3 ? "★★★" : (str == 2 ? "★★" : "★")));  
  24.         return result;  
  25.     }  
  26.     //计算欧式距离 传入两个将军对象。。  
  27.     public static double juli(General g1, General g2) {  
  28.         double result = (Double) Math.sqrt(StrictMath.pow(g1.getRender() - g2.getRender(), 2)  
  29.                 + StrictMath.pow(g1.getTongshai() - g2.getTongshai(), 2)  
  30.                 + StrictMath.pow(g1.getWuli() - g2.getWuli(), 2)  
  31.                 + StrictMath.pow(g1.getZhili() - g2.getZhili(), 2)  
  32.                 + StrictMath.pow(g1.getPolic() - g2.getPolic(), 2)  
  33.                 + StrictMath.pow(g1.getQiangbin() - g2.getQiangbin(), 2)  
  34.                 + StrictMath.pow(g1.getQibin() - g2.getQibin(), 2)  
  35.                 + StrictMath.pow(g1.getJibin() - g2.getJibin(), 2)  
  36.                 + StrictMath.pow(g1.getNubin() - g2.getNubin(), 2)  
  37.                 + StrictMath.pow(g1.getBinqi() - g2.getBinqi(), 2)  
  38.                 + StrictMath.pow(g1.getTongwu() - g2.getTongwu(), 2)  
  39.                 + StrictMath.pow(g1.getTongzhi() - g2.getTongzhi(), 2)  
  40.                 + StrictMath.pow(g1.getTongwuzhizheng() - g2.getTongwuzhizheng(), 2)  
  41.                 + StrictMath.pow(g1.getTongwuzhi() - g2.getTongwuzhi(), 2)  
  42.                 + StrictMath.pow(g1.getSalary() - g2.getSalary(), 2)  
  43.                 );  
  44.         return result;  
  45.     }  
  46. }  




6,DomParser.java

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. import javax.xml.parsers.*;  
  4.   
  5. import java.io.*;  
  6. import java.util.ArrayList;  
  7.   
  8. import org.w3c.dom.*;  
  9. import org.xml.sax.SAXException;  
  10.   
  11. public class DomParser {  
  12.   
  13.     private ArrayList<General> generals = new ArrayList<General>();  
  14.   
  15.     public ArrayList<General> prepare(){  
  16.         // get dom解析器工厂  
  17.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  18.         factory.setIgnoringElementContentWhitespace(true);  
  19.         // get dom 解析器  
  20.         DocumentBuilder builder = null;  
  21.         try {  
  22.             builder = factory.newDocumentBuilder();  
  23.         } catch (ParserConfigurationException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         // 解析文档  
  27.         Document doc = null;  
  28.         try {  
  29.             doc = builder.parse(new File("general.xml"));  
  30.         } catch (SAXException e) {  
  31.             e.printStackTrace();  
  32.         } catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         // 取得根节点  
  36.         Element generalList = doc.getDocumentElement();  
  37.         // 得到所有row节点  
  38.         NodeList nodeList = generalList.getElementsByTagName("Row");  
  39.         // 便利所有row节点  
  40.         for (int i = 1; i < nodeList.getLength(); i++) {  
  41.               
  42.             System.out.println("------------the " + i  
  43.                     + " element--------------");  
  44.               
  45.             Node row = nodeList.item(i);  
  46.             // 取得所有Data数据  
  47.             NodeList attList = row.getChildNodes();  
  48.             // 取得数据中的各个部分, 并加入ArrayList中  
  49.             generals.add(new General(Tool.xingji(attList.item(1)  
  50.                     .getTextContent()), attList.item(3).getTextContent(),  
  51.                     Integer.parseInt(attList.item(5).getTextContent()),   
  52.                     Integer.parseInt(attList.item(7).getTextContent()),  
  53.                     Integer.parseInt(attList.item(9).getTextContent()),   
  54.                     Integer.parseInt(attList.item(11).getTextContent()),   
  55.                     Tool.change(attList.item(13).getTextContent()),  
  56.                     Tool.change(attList.item(15).getTextContent()),   
  57.                     Tool.change(attList.item(17).getTextContent()),   
  58.                     Tool.change(attList.item(19).getTextContent()),   
  59.                     Tool.change(attList.item(21).getTextContent()),   
  60.                     Integer.parseInt(attList.item(23).getTextContent()),  
  61.                     Integer.parseInt(attList.item(25).getTextContent()),  
  62.                     Integer.parseInt(attList.item(27).getTextContent()),  
  63.                     Integer.parseInt(attList.item(29).getTextContent()),  
  64.                     Integer.parseInt(attList.item(31).getTextContent())));  
  65.               
  66.             System.out.println(" 星级:"  
  67.                     + Tool.xingji(attList.item(1).getTextContent()) + " 姓名:"  
  68.                     + attList.item(3).getTextContent() + " 统率:"  
  69.                     + attList.item(5).getTextContent() + " 武力:"  
  70.                     + attList.item(7).getTextContent() + " 智力:"  
  71.                     + attList.item(9).getTextContent() + " 政治:"  
  72.                     + attList.item(11).getTextContent() + "枪兵:"  
  73.                     + Tool.change(attList.item(13).getTextContent()) + " 戟兵:"  
  74.                     + Tool.change(attList.item(15).getTextContent()) + " 弩兵:"  
  75.                     + Tool.change(attList.item(17).getTextContent()) + " 骑兵:"  
  76.                     + Tool.change(attList.item(19).getTextContent()) + " 兵器:"  
  77.                     + Tool.change(attList.item(21).getTextContent()) + " 统武:"  
  78.                     + attList.item(23).getTextContent() + " 统智:"  
  79.                     + attList.item(25).getTextContent() + " 统武智:"  
  80.                     + attList.item(27).getTextContent() + " 统武智政:"  
  81.                     + attList.item(29).getTextContent() + " 50级工资:"  
  82.                     + attList.item(31).getTextContent() + " ");  
  83.             /* 
  84.              * for (int j = 0; j < attList.getLength(); j++) { 
  85.              * System.out.println(attList.item(j).getTextContent()); } 
  86.              */  
  87.         }  
  88.         return generals;  
  89.   
  90.     }  
  91. }  



7,TestKmeans.java 

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package kmeans;  
  2.   
  3. public class TestKmeans {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Kmeans kmeans = new Kmeans();  
  7.         kmeans.print(kmeans.getResult());  
  8.     }  
  9.   
  10. }  

五、附件

附部分general.xml(已上传到百度云盘,点此下载,完整下载链接:http://pan.baidu.com/s/1qW6SWOkf):

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <?mso-application progid="Excel.Sheet"?>  
  3. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"  
  4.  xmlns:o="urn:schemas-microsoft-com:office:office"  
  5.  xmlns:x="urn:schemas-microsoft-com:office:excel"  
  6.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"  
  7.  xmlns:html="http://www.w3.org/TR/REC-html40">  
  8.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">  
  9.   <Created>2006-09-13T11:21:51Z</Created>  
  10.   <LastSaved>2012-04-26T13:39:57Z</LastSaved>  
  11.   <Version>14.00</Version>  
  12.  </DocumentProperties>  
  13.  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">  
  14.   <AllowPNG/>  
  15.   <RemovePersonalInformation/>  
  16.  </OfficeDocumentSettings>  
  17.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">  
  18.   <WindowHeight>5850</WindowHeight>  
  19.   <WindowWidth>11070</WindowWidth>  
  20.   <WindowTopX>0</WindowTopX>  
  21.   <WindowTopY>90</WindowTopY>  
  22.   <ProtectStructure>False</ProtectStructure>  
  23.   <ProtectWindows>False</ProtectWindows>  
  24.  </ExcelWorkbook>  
  25.  <Styles>  
  26.   <Style ss:ID="Default" ss:Name="Normal">  
  27.    <Alignment ss:Vertical="Center"/>  
  28.    <Borders/>  
  29.    <Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>  
  30.    <Interior/>  
  31.    <NumberFormat/>  
  32.    <Protection/>  
  33.   </Style>  
  34.   <Style ss:ID="s16">  
  35.    <Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:WrapText="1"/>  
  36.    <Font ss:FontName="宋体" x:CharSet="134" ss:Bold="1"/>  
  37.   </Style>  
  38.   <Style ss:ID="s17">  
  39.    <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>  
  40.    <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12" ss:Bold="1"/>  
  41.   </Style>  
  42.   <Style ss:ID="s18">  
  43.    <Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:WrapText="1"/>  
  44.    <Font ss:FontName="宋体" x:CharSet="134"/>  
  45.   </Style>  
  46.   <Style ss:ID="s19">  
  47.    <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>  
  48.   </Style>  
  49.  </Styles>  
  50.  <Worksheet ss:Name="三国数据">  
  51.   <Table ss:ExpandedColumnCount="16" ss:ExpandedRowCount="682" x:FullColumns="1"  
  52.    x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">  
  53.    <Row ss:Height="28.5">  
  54.     <Cell ss:StyleID="s16"><Data ss:Type="String">星级</Data></Cell>  
  55.     <Cell ss:StyleID="s17"><Data ss:Type="String">姓名</Data></Cell>  
  56.     <Cell ss:StyleID="s17"><Data ss:Type="String">统率</Data></Cell>  
  57.     <Cell ss:StyleID="s17"><Data ss:Type="String">武力</Data></Cell>  
  58.     <Cell ss:StyleID="s17"><Data ss:Type="String">智力</Data></Cell>  
  59.     <Cell ss:StyleID="s17"><Data ss:Type="String">政治</Data></Cell>  
  60.     <Cell ss:StyleID="s17"><Data ss:Type="String">枪兵</Data></Cell>  
  61.     <Cell ss:StyleID="s17"><Data ss:Type="String">戟兵</Data></Cell>  
  62.     <Cell ss:StyleID="s17"><Data ss:Type="String">弩兵</Data></Cell>  
  63.     <Cell ss:StyleID="s17"><Data ss:Type="String">骑兵</Data></Cell>  
  64.     <Cell ss:StyleID="s17"><Data ss:Type="String">兵器</Data></Cell>  
  65.     <Cell ss:StyleID="s17"><Data ss:Type="String">统武</Data></Cell>  
  66.     <Cell ss:StyleID="s17"><Data ss:Type="String">统智</Data></Cell>  
  67.     <Cell ss:StyleID="s17"><Data ss:Type="String">统武智 </Data></Cell>  
  68.     <Cell ss:StyleID="s17"><Data ss:Type="String">统武智政</Data></Cell>  
  69.     <Cell ss:StyleID="s17"><Data ss:Type="String">50级工资 </Data></Cell>  
  70.    </Row>  
  71.    <Row>  
  72.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  73.     <Cell ss:StyleID="s19"><Data ss:Type="String">吕布</Data></Cell>  
  74.     <Cell ss:StyleID="s19"><Data ss:Type="Number">87</Data></Cell>  
  75.     <Cell ss:StyleID="s19"><Data ss:Type="Number">100</Data></Cell>  
  76.     <Cell ss:StyleID="s19"><Data ss:Type="Number">26</Data></Cell>  
  77.     <Cell ss:StyleID="s19"><Data ss:Type="Number">13</Data></Cell>  
  78.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  79.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  80.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  81.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  82.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  83.     <Cell ss:StyleID="s19"><Data ss:Type="Number">161</Data></Cell>  
  84.     <Cell ss:StyleID="s19"><Data ss:Type="Number">194</Data></Cell>  
  85.     <Cell ss:StyleID="s19"><Data ss:Type="Number">257</Data></Cell>  
  86.     <Cell ss:StyleID="s19"><Data ss:Type="Number">350</Data></Cell>  
  87.     <Cell ss:StyleID="s19"><Data ss:Type="Number">19250</Data></Cell>  
  88.    </Row>  
  89.    <Row>  
  90.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  91.     <Cell ss:StyleID="s19"><Data ss:Type="String">张飞</Data></Cell>  
  92.     <Cell ss:StyleID="s19"><Data ss:Type="Number">85</Data></Cell>  
  93.     <Cell ss:StyleID="s19"><Data ss:Type="Number">98</Data></Cell>  
  94.     <Cell ss:StyleID="s19"><Data ss:Type="Number">30</Data></Cell>  
  95.     <Cell ss:StyleID="s19"><Data ss:Type="Number">22</Data></Cell>  
  96.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  97.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  98.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  99.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  100.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  101.     <Cell ss:StyleID="s19"><Data ss:Type="Number">168</Data></Cell>  
  102.     <Cell ss:StyleID="s19"><Data ss:Type="Number">193</Data></Cell>  
  103.     <Cell ss:StyleID="s19"><Data ss:Type="Number">264</Data></Cell>  
  104.     <Cell ss:StyleID="s19"><Data ss:Type="Number">350</Data></Cell>  
  105.     <Cell ss:StyleID="s19"><Data ss:Type="Number">19250</Data></Cell>  
  106.    </Row>  
  107.    <Row>  
  108.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  109.     <Cell ss:StyleID="s19"><Data ss:Type="String">関羽</Data></Cell>  
  110.     <Cell ss:StyleID="s19"><Data ss:Type="Number">95</Data></Cell>  
  111.     <Cell ss:StyleID="s19"><Data ss:Type="Number">97</Data></Cell>  
  112.     <Cell ss:StyleID="s19"><Data ss:Type="Number">75</Data></Cell>  
  113.     <Cell ss:StyleID="s19"><Data ss:Type="Number">62</Data></Cell>  
  114.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  115.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  116.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  117.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  118.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  119.     <Cell ss:StyleID="s19"><Data ss:Type="Number">165</Data></Cell>  
  120.     <Cell ss:StyleID="s19"><Data ss:Type="Number">191</Data></Cell>  
  121.     <Cell ss:StyleID="s19"><Data ss:Type="Number">260</Data></Cell>  
  122.     <Cell ss:StyleID="s19"><Data ss:Type="Number">347</Data></Cell>  
  123.     <Cell ss:StyleID="s19"><Data ss:Type="Number">19085</Data></Cell>  
  124.    </Row>  
  125.    <Row>  
  126.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  127.     <Cell ss:StyleID="s19"><Data ss:Type="String">马超</Data></Cell>  
  128.     <Cell ss:StyleID="s19"><Data ss:Type="Number">88</Data></Cell>  
  129.     <Cell ss:StyleID="s19"><Data ss:Type="Number">97</Data></Cell>  
  130.     <Cell ss:StyleID="s19"><Data ss:Type="Number">44</Data></Cell>  
  131.     <Cell ss:StyleID="s19"><Data ss:Type="Number">26</Data></Cell>  
  132.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  133.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  134.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  135.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  136.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  137.     <Cell ss:StyleID="s19"><Data ss:Type="Number">168</Data></Cell>  
  138.     <Cell ss:StyleID="s19"><Data ss:Type="Number">187</Data></Cell>  
  139.     <Cell ss:StyleID="s19"><Data ss:Type="Number">259</Data></Cell>  
  140.     <Cell ss:StyleID="s19"><Data ss:Type="Number">353</Data></Cell>  
  141.     <Cell ss:StyleID="s19"><Data ss:Type="Number">19415</Data></Cell>  
  142.    </Row>  
  143.    <Row>  
  144.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  145.     <Cell ss:StyleID="s19"><Data ss:Type="String">赵雲</Data></Cell>  
  146.     <Cell ss:StyleID="s19"><Data ss:Type="Number">91</Data></Cell>  
  147.     <Cell ss:StyleID="s19"><Data ss:Type="Number">96</Data></Cell>  
  148.     <Cell ss:StyleID="s19"><Data ss:Type="Number">76</Data></Cell>  
  149.     <Cell ss:StyleID="s19"><Data ss:Type="Number">65</Data></Cell>  
  150.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  151.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  152.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  153.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  154.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  155.     <Cell ss:StyleID="s19"><Data ss:Type="Number">192</Data></Cell>  
  156.     <Cell ss:StyleID="s19"><Data ss:Type="Number">170</Data></Cell>  
  157.     <Cell ss:StyleID="s19"><Data ss:Type="Number">267</Data></Cell>  
  158.     <Cell ss:StyleID="s19"><Data ss:Type="Number">329</Data></Cell>  
  159.     <Cell ss:StyleID="s19"><Data ss:Type="Number">18095</Data></Cell>  
  160.    </Row>  
  161.    <Row>  
  162.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  163.     <Cell ss:StyleID="s19"><Data ss:Type="String">许褚</Data></Cell>  
  164.     <Cell ss:StyleID="s19"><Data ss:Type="Number">65</Data></Cell>  
  165.     <Cell ss:StyleID="s19"><Data ss:Type="Number">96</Data></Cell>  
  166.     <Cell ss:StyleID="s19"><Data ss:Type="Number">36</Data></Cell>  
  167.     <Cell ss:StyleID="s19"><Data ss:Type="Number">20</Data></Cell>  
  168.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  169.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  170.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  171.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  172.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  173.     <Cell ss:StyleID="s19"><Data ss:Type="Number">161</Data></Cell>  
  174.     <Cell ss:StyleID="s19"><Data ss:Type="Number">101</Data></Cell>  
  175.     <Cell ss:StyleID="s19"><Data ss:Type="Number">197</Data></Cell>  
  176.     <Cell ss:StyleID="s19"><Data ss:Type="Number">217</Data></Cell>  
  177.     <Cell ss:StyleID="s19"><Data ss:Type="Number">11935</Data></Cell>  
  178.    </Row>  
  179.    <Row>  
  180.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  181.     <Cell ss:StyleID="s19"><Data ss:Type="String">典韦</Data></Cell>  
  182.     <Cell ss:StyleID="s19"><Data ss:Type="Number">56</Data></Cell>  
  183.     <Cell ss:StyleID="s19"><Data ss:Type="Number">95</Data></Cell>  
  184.     <Cell ss:StyleID="s19"><Data ss:Type="Number">35</Data></Cell>  
  185.     <Cell ss:StyleID="s19"><Data ss:Type="Number">29</Data></Cell>  
  186.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  187.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  188.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  189.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  190.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  191.     <Cell ss:StyleID="s19"><Data ss:Type="Number">151</Data></Cell>  
  192.     <Cell ss:StyleID="s19"><Data ss:Type="Number">91</Data></Cell>  
  193.     <Cell ss:StyleID="s19"><Data ss:Type="Number">186</Data></Cell>  
  194.     <Cell ss:StyleID="s19"><Data ss:Type="Number">215</Data></Cell>  
  195.     <Cell ss:StyleID="s19"><Data ss:Type="Number">11825</Data></Cell>  
  196.    </Row>  
  197.    <Row>  
  198.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  199.     <Cell ss:StyleID="s19"><Data ss:Type="String">甘宁</Data></Cell>  
  200.     <Cell ss:StyleID="s19"><Data ss:Type="Number">86</Data></Cell>  
  201.     <Cell ss:StyleID="s19"><Data ss:Type="Number">94</Data></Cell>  
  202.     <Cell ss:StyleID="s19"><Data ss:Type="Number">76</Data></Cell>  
  203.     <Cell ss:StyleID="s19"><Data ss:Type="Number">18</Data></Cell>  
  204.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  205.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  206.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  207.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  208.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  209.     <Cell ss:StyleID="s19"><Data ss:Type="Number">181</Data></Cell>  
  210.     <Cell ss:StyleID="s19"><Data ss:Type="Number">183</Data></Cell>  
  211.     <Cell ss:StyleID="s19"><Data ss:Type="Number">270</Data></Cell>  
  212.     <Cell ss:StyleID="s19"><Data ss:Type="Number">351</Data></Cell>  
  213.     <Cell ss:StyleID="s19"><Data ss:Type="Number">19305</Data></Cell>  
  214.    </Row>  
  215.    <Row>  
  216.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  217.     <Cell ss:StyleID="s19"><Data ss:Type="String">庞德</Data></Cell>  
  218.     <Cell ss:StyleID="s19"><Data ss:Type="Number">80</Data></Cell>  
  219.     <Cell ss:StyleID="s19"><Data ss:Type="Number">94</Data></Cell>  
  220.     <Cell ss:StyleID="s19"><Data ss:Type="Number">70</Data></Cell>  
  221.     <Cell ss:StyleID="s19"><Data ss:Type="Number">44</Data></Cell>  
  222.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  223.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  224.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  225.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  226.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  227.     <Cell ss:StyleID="s19"><Data ss:Type="Number">174</Data></Cell>  
  228.     <Cell ss:StyleID="s19"><Data ss:Type="Number">150</Data></Cell>  
  229.     <Cell ss:StyleID="s19"><Data ss:Type="Number">244</Data></Cell>  
  230.     <Cell ss:StyleID="s19"><Data ss:Type="Number">288</Data></Cell>  
  231.     <Cell ss:StyleID="s19"><Data ss:Type="Number">15840</Data></Cell>  
  232.    </Row>  
  233.    <Row>  
  234.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  235.     <Cell ss:StyleID="s19"><Data ss:Type="String">文醜</Data></Cell>  
  236.     <Cell ss:StyleID="s19"><Data ss:Type="Number">78</Data></Cell>  
  237.     <Cell ss:StyleID="s19"><Data ss:Type="Number">94</Data></Cell>  
  238.     <Cell ss:StyleID="s19"><Data ss:Type="Number">25</Data></Cell>  
  239.     <Cell ss:StyleID="s19"><Data ss:Type="Number">25</Data></Cell>  
  240.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  241.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  242.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  243.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  244.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  245.     <Cell ss:StyleID="s19"><Data ss:Type="Number">172</Data></Cell>  
  246.     <Cell ss:StyleID="s19"><Data ss:Type="Number">103</Data></Cell>  
  247.     <Cell ss:StyleID="s19"><Data ss:Type="Number">197</Data></Cell>  
  248.     <Cell ss:StyleID="s19"><Data ss:Type="Number">222</Data></Cell>  
  249.     <Cell ss:StyleID="s19"><Data ss:Type="Number">12210</Data></Cell>  
  250.    </Row>  
  251.    <Row>  
  252.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  253.     <Cell ss:StyleID="s19"><Data ss:Type="String">黄忠</Data></Cell>  
  254.     <Cell ss:StyleID="s19"><Data ss:Type="Number">86</Data></Cell>  
  255.     <Cell ss:StyleID="s19"><Data ss:Type="Number">93</Data></Cell>  
  256.     <Cell ss:StyleID="s19"><Data ss:Type="Number">60</Data></Cell>  
  257.     <Cell ss:StyleID="s19"><Data ss:Type="Number">52</Data></Cell>  
  258.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  259.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  260.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  261.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  262.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  263.     <Cell ss:StyleID="s19"><Data ss:Type="Number">185</Data></Cell>  
  264.     <Cell ss:StyleID="s19"><Data ss:Type="Number">171</Data></Cell>  
  265.     <Cell ss:StyleID="s19"><Data ss:Type="Number">263</Data></Cell>  
  266.     <Cell ss:StyleID="s19"><Data ss:Type="Number">321</Data></Cell>  
  267.     <Cell ss:StyleID="s19"><Data ss:Type="Number">17655</Data></Cell>  
  268.    </Row>  
  269.    <Row>  
  270.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  271.     <Cell ss:StyleID="s19"><Data ss:Type="String">太史慈</Data></Cell>  
  272.     <Cell ss:StyleID="s19"><Data ss:Type="Number">82</Data></Cell>  
  273.     <Cell ss:StyleID="s19"><Data ss:Type="Number">93</Data></Cell>  
  274.     <Cell ss:StyleID="s19"><Data ss:Type="Number">66</Data></Cell>  
  275.     <Cell ss:StyleID="s19"><Data ss:Type="Number">58</Data></Cell>  
  276.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  277.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  278.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  279.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  280.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  281.     <Cell ss:StyleID="s19"><Data ss:Type="Number">175</Data></Cell>  
  282.     <Cell ss:StyleID="s19"><Data ss:Type="Number">148</Data></Cell>  
  283.     <Cell ss:StyleID="s19"><Data ss:Type="Number">241</Data></Cell>  
  284.     <Cell ss:StyleID="s19"><Data ss:Type="Number">299</Data></Cell>  
  285.     <Cell ss:StyleID="s19"><Data ss:Type="Number">16445</Data></Cell>  
  286.    </Row>  
  287.    <Row>  
  288.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  289.     <Cell ss:StyleID="s19"><Data ss:Type="String">颜良</Data></Cell>  
  290.     <Cell ss:StyleID="s19"><Data ss:Type="Number">79</Data></Cell>  
  291.     <Cell ss:StyleID="s19"><Data ss:Type="Number">93</Data></Cell>  
  292.     <Cell ss:StyleID="s19"><Data ss:Type="Number">42</Data></Cell>  
  293.     <Cell ss:StyleID="s19"><Data ss:Type="Number">32</Data></Cell>  
  294.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  295.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  296.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  297.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  298.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  299.     <Cell ss:StyleID="s19"><Data ss:Type="Number">172</Data></Cell>  
  300.     <Cell ss:StyleID="s19"><Data ss:Type="Number">121</Data></Cell>  
  301.     <Cell ss:StyleID="s19"><Data ss:Type="Number">214</Data></Cell>  
  302.     <Cell ss:StyleID="s19"><Data ss:Type="Number">246</Data></Cell>  
  303.     <Cell ss:StyleID="s19"><Data ss:Type="Number">13530</Data></Cell>  
  304.    </Row>  
  305.    <Row>  
  306.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  307.     <Cell ss:StyleID="s19"><Data ss:Type="String">张辽</Data></Cell>  
  308.     <Cell ss:StyleID="s19"><Data ss:Type="Number">93</Data></Cell>  
  309.     <Cell ss:StyleID="s19"><Data ss:Type="Number">92</Data></Cell>  
  310.     <Cell ss:StyleID="s19"><Data ss:Type="Number">78</Data></Cell>  
  311.     <Cell ss:StyleID="s19"><Data ss:Type="Number">58</Data></Cell>  
  312.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  313.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  314.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  315.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  316.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  317.     <Cell ss:StyleID="s19"><Data ss:Type="Number">183</Data></Cell>  
  318.     <Cell ss:StyleID="s19"><Data ss:Type="Number">167</Data></Cell>  
  319.     <Cell ss:StyleID="s19"><Data ss:Type="Number">257</Data></Cell>  
  320.     <Cell ss:StyleID="s19"><Data ss:Type="Number">330</Data></Cell>  
  321.     <Cell ss:StyleID="s19"><Data ss:Type="Number">18150</Data></Cell>  
  322.    </Row>  
  323.    <Row>  
  324.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  325.     <Cell ss:StyleID="s19"><Data ss:Type="String">孙策</Data></Cell>  
  326.     <Cell ss:StyleID="s19"><Data ss:Type="Number">92</Data></Cell>  
  327.     <Cell ss:StyleID="s19"><Data ss:Type="Number">92</Data></Cell>  
  328.     <Cell ss:StyleID="s19"><Data ss:Type="Number">69</Data></Cell>  
  329.     <Cell ss:StyleID="s19"><Data ss:Type="Number">70</Data></Cell>  
  330.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  331.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  332.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  333.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  334.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  335.     <Cell ss:StyleID="s19"><Data ss:Type="Number">130</Data></Cell>  
  336.     <Cell ss:StyleID="s19"><Data ss:Type="Number">192</Data></Cell>  
  337.     <Cell ss:StyleID="s19"><Data ss:Type="Number">230</Data></Cell>  
  338.     <Cell ss:StyleID="s19"><Data ss:Type="Number">325</Data></Cell>  
  339.     <Cell ss:StyleID="s19"><Data ss:Type="Number">17875</Data></Cell>  
  340.    </Row>  
  341.    <Row>  
  342.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  343.     <Cell ss:StyleID="s19"><Data ss:Type="String">魏延</Data></Cell>  
  344.     <Cell ss:StyleID="s19"><Data ss:Type="Number">81</Data></Cell>  
  345.     <Cell ss:StyleID="s19"><Data ss:Type="Number">92</Data></Cell>  
  346.     <Cell ss:StyleID="s19"><Data ss:Type="Number">69</Data></Cell>  
  347.     <Cell ss:StyleID="s19"><Data ss:Type="Number">49</Data></Cell>  
  348.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  349.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  350.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  351.     <Cell ss:StyleID="s19"><Data ss:Type="String">精</Data></Cell>  
  352.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  353.     <Cell ss:StyleID="s19"><Data ss:Type="Number">173</Data></Cell>  
  354.     <Cell ss:StyleID="s19"><Data ss:Type="Number">150</Data></Cell>  
  355.     <Cell ss:StyleID="s19"><Data ss:Type="Number">242</Data></Cell>  
  356.     <Cell ss:StyleID="s19"><Data ss:Type="Number">291</Data></Cell>  
  357.     <Cell ss:StyleID="s19"><Data ss:Type="Number">16005</Data></Cell>  
  358.    </Row>  
  359.    <Row>  
  360.     <Cell ss:StyleID="s18"><Data ss:Type="String">★★★★★</Data></Cell>  
  361.     <Cell ss:StyleID="s19"><Data ss:Type="String">华雄</Data></Cell>  
  362.     <Cell ss:StyleID="s19"><Data ss:Type="Number">81</Data></Cell>  
  363.     <Cell ss:StyleID="s19"><Data ss:Type="Number">92</Data></Cell>  
  364.     <Cell ss:StyleID="s19"><Data ss:Type="Number">56</Data></Cell>  
  365.     <Cell ss:StyleID="s19"><Data ss:Type="Number">40</Data></Cell>  
  366.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  367.     <Cell ss:StyleID="s19"><Data ss:Type="String">通</Data></Cell>  
  368.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  369.     <Cell ss:StyleID="s19"><Data ss:Type="String">神</Data></Cell>  
  370.     <Cell ss:StyleID="s19"><Data ss:Type="String">疏</Data></Cell>  
  371.     <Cell ss:StyleID="s19"><Data ss:Type="Number">173</Data></Cell>  
  372.     <Cell ss:StyleID="s19"><Data ss:Type="Number">137</Data></Cell>  
  373.     <Cell ss:StyleID="s19"><Data ss:Type="Number">229</Data></Cell>  
  374.     <Cell ss:StyleID="s19"><Data ss:Type="Number">269</Data></Cell>  
  375.     <Cell ss:StyleID="s19"><Data ss:Type="Number">14795</Data></Cell>  
  376.    </Row>  


六、结果截图

最终运行结果截图如下:

读取到得武将数据如图,


运行后的部分截图:


0 0
原创粉丝点击