A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© touch_world 中级黑马   /  2014-11-9 07:34  /  1368 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

如何生成m个数让其和等于n,求编程
假设生成26个非负随机数,要求其和是301,求生成此列数字!?

5 个回复

正序浏览
a8851625 发表于 2014-11-10 14:41
其实是有不少空子可以钻,我感觉我这个例子就是,钻了个空子

是的啊,感觉要限制随机数重复的次数才行
回复 使用道具 举报
本帖最后由 hanxing 于 2014-11-10 15:42 编辑


分别是26次随机数和为301
5次和为20
5次和为40的运算结果,当然,就像我上面说的,如果可以重复,看上去就不是我们想要的结果
  1. import java.util.*;
  2. /*
  3. 如何生成m个数让其和等于n,求编程
  4. 假设生成26个非负随机数,要求其和是301,求生成此列数字!?
  5. */
  6. class  TestDemo3
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 getRandomNum(301,26);
  11.                 getRandomNum(20,5);
  12.                 getRandomNum(40,5);
  13.         }

  14.         public static void getRandomNum(int sum,int n)//sum为m个随机数的和
  15.         {        Random random=new Random();
  16.                 int temp=0,count=0,a=0;
  17.                
  18.                 if(sum>0&&n>0&&sum>=n)
  19.                 {       
  20.                         while(sum-temp>0)
  21.                         {       
  22.                                 //如果还差一次,且和还没有sum大最后一次随机数就是sum-temp
  23.                                 if(count==n-1&&temp<sum)
  24.                                         a=sum-temp;
  25.                                 else
  26.                                 {       
  27.                                 //本次随机数的范围随着和的增大而减小,n-count-1是后面次数和的最小值(每次都为1)
  28.                                 //sum-temp-(n-count-1)为当前这个随机数能取到的最大值
  29.                                         a =random.nextInt(sum-temp-(n-count-1))+1;
  30.                                 }                                                                                               
  31.                                 temp+=a;
  32.                                 count++;

  33.                                 if(count%10!=0)
  34.                                         System.out.print(a+"\t");
  35.                                 else
  36.                                         System.out.println(a);//输出格式       
  37.                         }
  38.                 }

  39.                 else
  40.                 {
  41.                         if(n<0) System.out.println("n不能为负数");
  42.                         if(sum<0&&sum<n) System.out.println("sum不能为负数,且必须不小于n");
  43.                 }
  44.                         System.out.println();
  45.                  System.out.println("--------------------------------------------------");       
  46.         }
  47. }
复制代码




回复 使用道具 举报
hanxing 发表于 2014-11-10 14:24
26个非负随机数,这是个bug,如果20个随机数的和就到了301,那后面6个数完全可以随机到0,如果第一个就是301,那 ...

其实是有不少空子可以钻,我感觉我这个例子就是,钻了个空子
回复 使用道具 举报
26个非负随机数,这是个bug,如果20个随机数的和就到了301,那后面6个数完全可以随机到0,如果第一个就是301,那后面都可以为0了,好像出这题就没意义了
回复 使用道具 举报

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Test {
       
       
       
        public static void main(String[] args)throws Exception
        {
               
                int y=24;//取6个随机数,有一个数根据取的5个数来做改变
                int sum=301;
                ArrayList<Integer> al=new ArrayList<Integer>();
                int m=0;
                boolean flag1=true;
                boolean flag2=true;
                while(flag2)
                {
                       
                        while(flag1)
                        {
                                m=0;
                                al.clear();
                                Random r=new Random();
                               
                                for(int x=0;x<=y;x++)
                                {       
                                        int num=r.nextInt(50);//取50行了,才50里面随机出数据。不过到100了就很难找。毕竟你要26个数
                                        al.add(num);
                                }                                       
                                Iterator<Integer> it=al.iterator();

                                while(it.hasNext())
                                {
                                        m=it.next()+m;
                                       
                                        if(m<sum)
                                        {
                                               
                                                flag1=false;
                                                flag2=false;
                                               
                                        }
                                        else
                                        {
                                                flag1=true;
                                                flag2=false;
                                               
                                        }       
                                       
                                }
                        }                                                                                       
                }
                int a=sum-m;
                al.add(a);
                for(int s: al)
                {
                        System.out.print("随机数:"+s+"        ");       
                }
        }       
       
}
下面是结果
随机数:2        随机数:2        随机数:15       随机数:6        随机数:8
随机数:7        随机数:30       随机数:11       随机数:9        随机数:13
随机数:9        随机数:16       随机数:4        随机数:3        随机数:18
随机数:22       随机数:36       随机数:3        随机数:12       随机数:1
随机数:31       随机数:20       随机数:12       随机数:2        随机数:5
随机数:4
哈哈,觉得不对可以自己加一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马