JAVA权重计算(根据自己理解计算的,不喜勿喷)
/**
?* 權重實體類,用于接收需要計算權重的數據和每個數據的權重
?* @param <T>
?*/
public class Height<T> {
?? ?private int height;
?? ?private T t;
?? ?public int getHeight() {
?? ??? ?return height;
?? ?}
?? ?public void setHeight(int height) {
?? ??? ?this.height = height;
?? ?}
?? ?public T getT() {
?? ??? ?return t;
?? ?}
?? ?public void setT(T t) {
?? ??? ?this.t = t;
?? ?}
?? ?
}
?
?
?
?
?
?
?
?
?
?
/**
?* Demo用于作為測試類
?*
?*/
public class Demo {
?? ?private String id;
?? ?private Integer height;
?? ?public String getId() {
?? ??? ?return id;
?? ?}
?? ?public void setId(String id) {
?? ??? ?this.id = id;
?? ?}
?? ?public Integer getHeight() {
?? ??? ?return height;
?? ?}
?? ?public void setHeight(Integer height) {
?? ??? ?this.height = height;
?? ?}
?? ?
}
?
?
?
?
?
?
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
?* 權重計算實現類(根據自己的理解來計算權重的,不喜勿噴)
?*
?* @param <T> 最終需要返回的數據
?*/
public class WeightUtils<T> {
?? ?/**
?? ? * 計算權重后獲取的數據
?? ? * @param list 需要進行權重計算的數據
?? ? * @return
?? ? */
?? ?public static <T>T getT(List<Height<T>> list){
?? ??? ?/*定義一個數字,用于統計權重的和*/
?? ??? ?int num = 0;
?? ??? ?/*遍歷傳入的集合,計算總權重*/
?? ??? ?for (int i = 0; i < list.size(); i++) {
?? ??? ??? ?num += list.get(i).getHeight();
?? ??? ?}
?? ??? ?/*在總權重的區間獲取一個隨機數*/
?? ??? ?int random = getRandom(num);
?? ??? ?num = 0;
?? ??? ?for (int i = 0; i < list.size(); i++) {
?? ??? ??? ?num = num + list.get(i).getHeight();
?? ??? ??? ?if (num >= random) {
?? ??? ??? ??? ?return list.get(i).getT();
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return null;
?? ?}
?? ?/**
?? ? * 獲取隨機數
?? ? * @param num
?? ? * @return
?? ? */
?? ?public static int getRandom(int num){
?? ??? ?double random = Math.random()*num;
?? ??? ?return (int)random;
?? ?}
?? ?/**
?? ? * 測試的main方法計算權重
?? ? * @param args
?? ? */
?? ?public static void main(String[] args) {
?? ??? ?/*將Demo進行實例化,用于表示需要計算權重的數據*/
?? ??? ?Demo demo1 = new Demo();
?? ??? ?demo1.setId(UUID.randomUUID().toString());
?? ??? ?demo1.setHeight(100);
?? ??? ?
?? ??? ?Demo demo2 = new Demo();
?? ??? ?demo2.setId(UUID.randomUUID().toString());
?? ??? ?demo2.setHeight(200);
?? ??? ?
?? ??? ?Demo demo3 = new Demo();
?? ??? ?demo3.setId(UUID.randomUUID().toString());
?? ??? ?demo3.setHeight(300);
?? ??? ?
?? ??? ?Demo demo4 = new Demo();
?? ??? ?demo4.setId(UUID.randomUUID().toString());
?? ??? ?demo4.setHeight(400);
?? ??? ?
?? ??? ?Demo demo5 = new Demo();
?? ??? ?demo5.setId(UUID.randomUUID().toString());
?? ??? ?demo5.setHeight(500);
?? ??? ?
?? ??? ?Demo demo6 = new Demo();
?? ??? ?demo6.setId(UUID.randomUUID().toString());
?? ??? ?demo6.setHeight(600);
?? ??? ?List<Height<Demo>> list = new ArrayList<Height<Demo>>();
?? ??? ?/*將Height進行實例化,用于存儲前面的demo*/
?? ??? ?Height<Demo> w1 = new Height<Demo>();
?? ??? ?w1.setT(demo1);
?? ??? ?w1.setHeight(demo1.getHeight());
?? ??? ?
?? ??? ?Height<Demo> w2 = new Height<Demo>();
?? ??? ?w2.setT(demo2);
?? ??? ?w2.setHeight(demo2.getHeight());
?? ??? ?
?? ??? ?Height<Demo> w3 = new Height<Demo>();
?? ??? ?w3.setT(demo3);
?? ??? ?w3.setHeight(demo3.getHeight());
?? ??? ?
?? ??? ?Height<Demo> w4 = new Height<Demo>();
?? ??? ?w4.setT(demo4);
?? ??? ?w4.setHeight(demo4.getHeight());
?? ??? ?
?? ??? ?Height<Demo> w5 = new Height<Demo>();
?? ??? ?w5.setT(demo5);
?? ??? ?w5.setHeight(demo5.getHeight());
?? ??? ?
?? ??? ?Height<Demo> w6 = new Height<Demo>();
?? ??? ?w6.setT(demo6);
?? ??? ?w6.setHeight(demo6.getHeight());
?? ??? ?
?? ??? ?/*將權重實體類存放到集合中*/
?? ??? ?list.add(w1);
?? ??? ?list.add(w2);
?? ??? ?list.add(w3);
?? ??? ?list.add(w4);
?? ??? ?list.add(w5);
?? ??? ?list.add(w6);
?? ??? ?/*定義六個int類型的數字,用于最終*/
?? ??? ?int a1 = 0;
?? ??? ?int a2 = 0;
?? ??? ?int a3 = 0;
?? ??? ?int a4 = 0;
?? ??? ?int a5 = 0;
?? ??? ?int a6 = 0;
?? ??? ?int sum = 200000;
?? ??? ?for (int i = 1; i <= sum; i++) {
?? ??? ??? ?Demo demo = getT(list);
?? ??? ??? ?if (demo == demo1) {
?? ??? ??? ??? ?a1 ++;
?? ??? ??? ?}
?? ??? ??? ?if (demo == demo2) {
?? ??? ??? ??? ?a2 ++;
?? ??? ??? ?}
?? ??? ??? ?if (demo == demo3) {
?? ??? ??? ??? ?a3 ++;
?? ??? ??? ?}
?? ??? ??? ?if (demo == demo4) {
?? ??? ??? ??? ?a4 ++;
?? ??? ??? ?}
?? ??? ??? ?if (demo == demo5) {
?? ??? ??? ??? ?a5 ++;
?? ??? ??? ?}
?? ??? ??? ?if (demo == demo6) {
?? ??? ??? ??? ?a6 ++;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?/*輸出權重和除以每個demo出現的次數,計算出來的值越小權重越高*/
?? ??? ?System.out.println("a1========="+a1+"============"+(double)sum/(double)a1);
?? ??? ?System.out.println("a2========="+a2+"============"+(double)sum/(double)a2);
?? ??? ?System.out.println("a3========="+a3+"============"+(double)sum/(double)a3);
?? ??? ?System.out.println("a4========="+a4+"============"+(double)sum/(double)a4);
?? ??? ?System.out.println("a5========="+a5+"============"+(double)sum/(double)a5);
?? ??? ?System.out.println("a6========="+a6+"============"+(double)sum/(double)a6);
?? ??? ?/*計算所有權重出現的概率之和*/
?? ??? ?double z = (double)a1/(double)sum+(double)a2/(double)sum+(double)a3/(double)sum+(double)a4/(double)sum+(double)a5/(double)sum+(double)a6/(double)sum;
?? ??? ?System.out.println(z);
?? ??? ?/*計算每個demo出現的次數之和,用于判斷demo出現的次數之和是不是等于遍歷的次數,用于判斷是否有遺漏的情況*/
?? ??? ?System.out.println(a1+a2+a3+a4+a5+a6);
?? ?}
}
?
總結
以上是生活随笔為你收集整理的JAVA权重计算(根据自己理解计算的,不喜勿喷)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于TIdTCPClient的几种方法
- 下一篇: CSS实现圆点