首页 > 其他分享 >限行列和随机加墙版_递归调用解决结果含负数问题

限行列和随机加墙版_递归调用解决结果含负数问题

时间:2022-11-29 09:11:06浏览次数:36  
标签:End 递归 nCols vArr Long 负数 tmpSum 加墙 Next

Sub 限行列和随机加墙版()
    Dim nRows As Long, nCols As Long, nLastRow As Long, tmpSum As Long, tmpRow As Long, r As Long, c As Long
    Dim sumRows() As Long, nRowSkipSum() As Long, nColSkipSum() As Long, nColsLast() As Long
    Dim bForceValue() As Boolean
    Dim vArr() As Variant
    Dim fOffset As Single
    vArr = Sheet1.Range("A2").CurrentRegion.Value '取数据
    nRows = UBound(vArr)
    nCols = UBound(vArr, 2)
    ReDim sumRows(3 To nRows)
    ReDim nRowSkipSum(3 To nRows)
    ReDim nColsLast(3 To nRows)
    ReDim nColSkipSum(2 To nCols - 1)
    ReDim nRowsLast(2 To nCols - 1)
    ReDim bForceValue(3 To nRows, 2 To nCols)
    With Sheet1.Range("A1")
        For c = 2 To nCols - 1
            For r = 3 To nRows
                If .Offset(r - 1, c - 1).Interior.Color = vbYellow Then '背景颜色为黄色的单元格固定原值不变(跳过)
                    bForceValue(r, c) = True
                    bForceValue(r, nCols) = True
                    nRowSkipSum(r) = nRowSkipSum(r) + vArr(r, c) '每行跳过值之和
                    nColSkipSum(c) = nColSkipSum(c) + vArr(r, c) '每列跳过值之和
                Else
                    sumRows(r) = sumRows(r) + vArr(2, c) '每行限制之和
                    If vArr(2, c) <> 0 Then nColsLast(r) = c '每行最后1个列限制和非0的非固定值的列号
                End If
            Next
        Next
    End With
    For c = 2 To nCols - 1
        tmpSum = tmpSum + vArr(2, c)
    Next
    For r = 3 To nRows
        tmpRow = tmpRow + vArr(r, nCols)
        If vArr(r, nCols) <> 0 And bForceValue(r, nCols) = False Then nLastRow = r
    Next
    If tmpRow <> tmpSum Then MsgBox "行与列限制之和不相等!": Exit Sub
    If nLastRow < 3 Then MsgBox "至少要有一行无任何固定值!": Exit Sub
    '        fOffset = 0.05! '随机值浮动百分比
    fOffset = 0.08! '随机值浮动百分比
    '    fOffset = 0.015! '随机值浮动百分比
    Randomize
    For r = 3 To nRows
        If r <> nLastRow Then
            tmpSum = 0
            tmpRow = vArr(r, nCols) - nRowSkipSum(r) '该行剩余可随机值之和
            For c = 2 To nCols - 1
                If c <> nColsLast(r) And bForceValue(r, c) = False Then
                    vArr(r, c) = Int(tmpRow / sumRows(r) * vArr(2, c) * (1! + Rnd * fOffset * 2 - fOffset))
                    tmpSum = tmpSum + vArr(r, c)
                End If
            Next
            vArr(r, nColsLast(r)) = tmpRow - tmpSum '该行剩余列的值
        End If
    Next
    For c = 2 To nCols - 1
        tmpSum = 0
        For r = 3 To nRows
            If r <> nLastRow And bForceValue(r, c) = False Then tmpSum = tmpSum + vArr(r, c)
        Next
        vArr(nLastRow, c) = vArr(2, c) - nColSkipSum(c) - tmpSum '剩余列的剩余值
    Next
    If 二维数组含负数(vArr) = False Then
        Call 限行列和随机加墙版
    Else
        Sheet1.Range("A1").Resize(UBound(vArr), UBound(vArr, 2)).Value = vArr
    End If
End Sub

Function 二维数组含负数(ar)
    flag = True
    For x = 3 To UBound(ar)
        For y = 2 To UBound(ar, 2)
            If ar(x, y) < 0 Then
                flag = False
            End If
        Next
    Next
    二维数组含负数 = flag
End Function


Function 检查二维数组是否合法(vArr)
    '    vArr = Sheet2.Range("A2").CurrentRegion.Value '取数据
    If 二维数组含负数(vArr) = False Then
        MsgBox "随机数不合理,请重试一次!"
    Else
        MsgBox "随机数取值合理,请运行主程序!"
    End If
End Function

Sub 单独检查二维数组是否包含负数()
    vArr = Sheet1.Range("A2").CurrentRegion.Value '取数据
    If 二维数组含负数(vArr) = False Then
        MsgBox "随机数不合理,请重试一次!"
    End If
End Sub

 

标签:End,递归,nCols,vArr,Long,负数,tmpSum,加墙,Next
From: https://www.cnblogs.com/eyunkeji/p/16934401.html

相关文章

  • C语言递归算法解决李白打酒问题
    一、概念递归算法(英语:recursionalgorithm)在计算机科学中是指一种通过重复将问题分解为同类的子问题而解决问题的方法。递归式方法可以被用于解决很多的计算机科学问题,因此......
  • Java实现递归查询树结构
        我们在实际开发中,肯定会用到树结构,如部门树、菜单树等等。Java后台利用递归思路进行构建树形结构数据,返回给前端,能以下拉菜单等形式进行展示。今天,咱们就来说......
  • 递归小笔记:
    @OverridepublicLong[]findCatelogPath(LongcatelogId){List<Long>paths=newArrayList<>();List<Long>parentPath=findParentPath(catelogId,paths);......
  • java利用递归实现扫雷
    package扫雷;importjava.math.*;importjava.util.Scanner;publicclass扫雷{//记录翻开次数staticintk=0;//两个数组......
  • C语言-求一个数的阶乘(递归法)
    1.函数调用#include<stdio.h>intFac(intn){inti,ret=1;for(i=1;i<=n;i++){ret*=i;}returnret;}intmain(void){intn=0,set;scanf("%d",......
  • c 递归,递推法
     #include<stdio.h>//递归longlongfunc(intn){if(n==1||n==2)return1;returnfunc(n-1)+func(n-2);}//数组longlongnum[100];intmain(){......
  • PC_机器数_定点负数的原码_补码_反码在结构上的关系
    文章目录​​预备知识​​​​模2范畴内的取反​​​​证明二进制补码规律​​​​模k同余​​​​......
  • 浅谈 CRTP:奇异递归模板模式
    浅谈CRTP:奇异递归模板模式前言建议先看一遍文末的参考资料!建议先看一遍文末的参考资料!建议先看一遍文末的参考资料!思维导图一、CRTP是什么CRTP全称:​​CuriouslyRec......
  • PHP基于非递归方式算法实现先序/中序/后序遍历二叉树操作
    /** *PHP基于非递归方式算法实现先序/中序/后序遍历二叉树操作 *     A *    B   C *  D  E F   G......
  • js 深度拷贝递归生成
    functiondeepClone(param){//判断时间if(paraminstanceofdate)returnnewDate(param);//如果普通类型直接返回if(typeofparam!=='object'......