roc_java 2008-11-20 15:51
浏览 227
已采纳

无路可走了,讨教一概率算法

0  50%

1  35%

2  15%

 

 

0 出现的概率为50%

1 出现的概率为35%

2 出现的概率为15%

 

在这3个数中按以上概率取其中的一个数。

 


问题补充:

0? 50%


1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?



谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。

public class RandomTest {
private static Random RANDOM = new Random();
public static void main(String[] args) {
    List list = new ArrayList();
    list.add(new Integer(25));
    list.add(new Integer(40));
    list.add(new Integer(35));
    Collections.sort(list);
    System.out.println(list);

    int a = ((Integer) list.get(0)).intValue();
    int b = ((Integer) list.get(1)).intValue();
    int c = ((Integer) list.get(2)).intValue();
    System.out.println("几率最大的数:" + c);
    System.out.println("中间数:" + (c + b));
    Map map = new HashMap();
    int x = 0;
    int y = 0;
    int z = 0;
    for (int i = 0; i < 1000000; i++) {
        int k = RANDOM.nextInt();
        int j = Math.abs(k % 100);
        if (j <= c) {
            map.put("0", new Integer(++x));
        } else if (j > c && j <= (c + b)) {
            map.put("1", new Integer(++y));
        } else if (j > (c + b) && j < (a+b+c)) {
            map.put("2", new Integer(++z));
        }

    }
    System.out.println(map);
}

}


问题补充:

0? 50%



1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?


问题补充:

0  50%




1  35%




2  15%




 




 




0 出现的概率为50%




1 出现的概率为35%




2 出现的概率为15%




 




在这3个数中按以上概率取其中的一个数。




 




谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。



public class RandomTest {

private static Random RANDOM = new Random();

public static void main(String[] args) {




List list = new ArrayList();

list.add(new Integer(25));

list.add(new Integer(40));

list.add(new Integer(35));

Collections.sort(list);

System.out.println(list);



int a = ((Integer) list.get(0)).intValue();

int b = ((Integer) list.get(1)).intValue();

int c = ((Integer) list.get(2)).intValue();

System.out.println("几率最大的数:" + c);

System.out.println("中间数:" + (c + b));

Map map = new HashMap();

int x = 0;

int y = 0;

int z = 0;

for (int i = 0; i < 1000000; i++) {

int k = RANDOM.nextInt();

int j = Math.abs(k % 100);

if (j <= c) {

map.put("0", new Integer(++x));

} else if (j > c && j <= (c + b)) {

map.put("1", new Integer(++y));

} else if (j > (c + b) && j < (a+b+c)) {

map.put("2", new Integer(++z));

}



}

System.out.println(map);

}



}


问题补充:

0? 50%



1? 35%



2? 15%



?



?



0 出现的概率为50%



1 出现的概率为35%



2 出现的概率为15%



?



在这3个数中按以上概率取其中的一个数。



?




谢谢bohemia,刚才我也弄了一个,呵呵,过来一看差不多。呵呵。



public class RandomTest {

private static Random RANDOM = new Random();

public static void main(String[] args) {




List list = new ArrayList();

list.add(new Integer(25));

list.add(new Integer(40));

list.add(new Integer(35));

Collections.sort(list);

System.out.println(list);



int a = ((Integer) list.get(0)).intValue();

int b = ((Integer) list.get(1)).intValue();

int c = ((Integer) list.get(2)).intValue();

System.out.println("几率最大的数:" + c);

System.out.println("中间数:" + (c + b));

Map map = new HashMap();

int x = 0;

int y = 0;

int z = 0;

for (int i = 0; i < 1000000; i++) {

int k = RANDOM.nextInt();

int j = Math.abs(k % 100);

if (j <= c) {

map.put("0", new Integer(++x));

} else if (j > c && j <= (c + b)) {

map.put("1", new Integer(++y));

} else if (j > (c + b) && j < (a+b+c)) {

map.put("2", new Integer(++z));

}



}

System.out.println(map);

}



}

  • 写回答

2条回答 默认 最新

  • bohemia 2008-11-20 16:23
    关注

    [code="java"]public class TestRandom{
    public static void main(String[] args){

        int a = 50;
        int b = 35;
        int c = 15;
    
        int a2 = 0;
        int b2 = 1;
        int c2 = 2;
    
        Map result = new LinkedHashMap();
    
        for(int i=0; i<a; i++){
            result.put(""+i,""+a2);
        } 
        for(int i=a; i<a+b; i++){
            result.put(""+i,""+b2);
        } 
        for(int i=a+b; i<a+b+c; i++){
            result.put(""+i,""+c2);
        } 
    
        System.out.println("Result:"+result);            
    
        System.out.println("随机数:"+result.get(""+(int)(Math.random()*100)));    
    
    }
    

    }[/code]

    不知道行不行;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败