有50个数,他们的值的范围都在0到100之间,我想让50个数进行分组 ,要求:每一组的值少于等于100
写一段小程序

解决方案 »

  1.   

    a(50) 存放着原来的50个数
    b(n组)(m个)
    c(n)存放b每组的数的个数
    b(0)(0)=a(0)
    c(0)=1
    n=0
    for i=1 to 49           遍历所有数值
           bz=false
           for j=0 to n-1 遍历已经排好的组
    '求已分组好的和
            sum=0;
    for l=0 to c(j)
                sum=sum+b(j)(l);
            next l
                    
    if sum+a(j)<=100 then
    b(j)(c(j)+1)=a(j)
    c(j)=c(j)+1
    bz=true
                            break
                    endif
            next j
            if bz=false then
       n=n+1
               b(n)(0)=a(i)
       c(n)=1
            end if 
           
    next i
      

  2.   

    dim a(1 to 50)'要分的数值
    dim b()
    '排序
    'code here
    '排好的数组a(1 to 50)
    n=50
    for i=1 to 50
    if i>n then exit for
    b(i)=a(1)
    for j=n to i step -1
    if b(i)+a(j)<=100 then
    b(i)=b(i)+a(j)
    '记录分组情况 记录
    'code here
    n=j
    end if
    next j
    next i
    '数组b的个数就是得到的分组个数。具体分组情况可以在分组的时候记录
    '排序和记录分组情况的代码没有写
    '没有开发环境。若有bug请自己调试一下。主要的算法都在里面了