Option Explicit Dim intMaxReply As Integer Dim intTotalPoint%, intRemain% Dim arrReply() As Integer Private Sub Command1_Click() intMaxReply = 17'问题总回答数 intTotalPoint = 40'问题点数 ReDim arrReply(1 To intMaxReply) If intTotalPoint > intMaxReply Then Dim intAvg% intAvg = Int(intTotalPoint / intMaxReply) intRemain = intTotalPoint - intAvg * intMaxReply Dim i% For i = 1 To intMaxReply If i <= intRemain Then arrReply(i) = intAvg + 1 Else arrReply(i) = intAvg End If Next Else For i = 1 To intTotalPoint arrReply(i) = 1 Next End If Print "Total: " & vbTab & intTotalPoint Print "Reply: " & vbTab & intMaxReply Print "============" For i = 1 To intMaxReply Print i & vbTab & arrReply(i) & "'" Next End Sub
其实对于问题数和点数的大小比较是不必要的,修改后第二版:
Option Explicit Dim intMaxReply As Integer Dim intTotalPoint%, intRemain% Dim arrReply() As Integer Private Sub Command1_Click() intMaxReply = 17 '回答总数 intTotalPoint = 40 '问题点数 ReDim arrReply(1 To intMaxReply) Dim intAvg% intAvg = Int(intTotalPoint / intMaxReply) intRemain = intTotalPoint - intAvg * intMaxReply Dim i% For i = 1 To intMaxReply If i <= intRemain Then arrReply(i) = intAvg + 1 Else arrReply(i) = intAvg End If Next Print "Total: " & vbTab & intTotalPoint Print "Reply: " & vbTab & intMaxReply Print "============" For i = 1 To intMaxReply Print i & vbTab & arrReply(i) & "'" Next End Sub
执行结果:
另外附上csdn自己的分配算法:
function GiveAvePointByUserName() { var tables = document.getElementsByTagName("table"); var replys = new Array(); var names = new Array(); var draws = new Array(); var pointCounter = 0; var topicPoint = -1; var drawing = 0; for (var i = 0; i < tables.length; i++) { if (tables[i].className == "item topic") topicPoint = GetTopicPoint(tables[i]); if (tables[i].className == "item reply") { var input = GetPointInput(tables[i]); if (!input) continue; if (parseInt(input.value) > 0) { drawing += parseInt(input.value); var username = GetUserName(tables[i]); var index = IndexOf(draws, username); if (index < 0) draws.push(username); var index = IndexOf(names, username); if (index < 0) names.push(username); } else { var username = GetUserName(tables[i]); var index = IndexOf(names, username); if (index < 0) { pointCounter++; names.push(username); } } } } if (pointCounter <= 0) return; topicPoint = topicPoint - drawing; if (topicPoint < 0) { alert("没有多余的分散了!"); return; } var average = Math.floor(topicPoint / pointCounter); var spare = topicPoint % pointCounter; names.length = 0; for (var i = 0; i < draws.length; i++) names.push(draws[i]); for (var i = 0; i < tables.length; i++) if (tables[i].className == "item reply") { var input = GetPointInput(tables[i]); if (!input) continue; if (parseInt(input.value) > 0) continue; var username = GetUserName(tables[i]); var index = IndexOf(names, username); if (index < 0) { if (spare > 0) { input.value = average + 1; spare--; } else input.value = average; names.push(username); } else input.value = 0; } window.alert("平均给分完成,点击\"确认结帖\"进行结帖"); } /* CSDN新版论坛帖子自动平均给分脚本 由网友ChrisAK提供 */ function averagePoint() { var points = document.getElementById("point").innerHTML; var validtxts = new Array(); var txts = document.getElementsByName("tb_score"); for (var i = 0; i < txts.length; ++i) { if (txts[i].readOnly)//跳过自己的回复 continue; var score = new Number(txts[i].value); if (score < 0)//负分的清零,跳过 { txts[i].value = 0; continue; } if (score > 0)//跳过已给分的 { points -= score; continue; } txts[i].value = 0; validtxts.push(i); } }
javascript:GiveAvePointByUserName();
javascript:averagePoint();