我的VBA有问题。
在数据库中有一张教室桌子
id limit
-------- ---------
103 35
107 42
109 50
201 45
203 54
204 80
... ...
.. ..
. .
还有像下面这样的考试
exam_id participants
-------- ------------
1010 105
1011 320
1012 45
1014 283
... ...
.. ..
. .
我们需要确定每次考试最少需要多少教室有些考试只需要一个教室,有些则需要十个以上的教室因此,我必须交叉检查每个教室的容量,以选择最少的教室数量,而不浪费大教室给小团体。
我正在一个接一个地考试,试着找一个合适的有for循环的教室
'all exams
for a=1 to LastExam
'with all classrooms
for b=1 to LastClassRoom
'if it's the first classroom we're checked
if tempClass = "" then tempClass = b
'if this classroom capacity is enough for participants
if examList(a,1) < classRoomList(b,1) then
'declare it that we found
findFlag = 1
'check is it also better than the temp classroom. If it's then
'mark it as the new temp classroom
if classRoomList(b,1) < classRoomList(tempClass,1) then
tempClass = b
end if
end if
next
next
'if we haven't found an enough capacity for it try two nested loops
if findFlag = "" then
for a1=1 to lastExam-1
for a2=a1 to lastExam
...
..
.
'if two classrooms are not enough try three nested loop
if findFlag = "" then
for a1=1 to lastExam-2
for a2=a1 to lastExam-1
for a3=a2 to lastExam
...
..
.
still not found? try four nested loops
...
..
.
five nested loops
...
..
.
till 16 nested loops
嵌套for循环解决方案不适合,因为有16个教室,而且可能很快就会增加。
我试过创建递归函数,但我做不到。我不得不承认这超出了我的能力范围我接受所有的建议。谢谢您。
最佳答案
为什么需要一个递归的解决方案,可以通过对一个数组进行排序并运行一个for循环来完成???我想这可能有帮助->
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>
#include <list>
using namespace std;
int main() {
int n, k;
vector< int >hallroom;
cout<<"How many examHalls ?? : ";
cin>>n;
cout<<"Input ExamHall Capacities : ";
for(int i=0; i<n; i++)
{
int idx;
cin>>idx;
hallroom.push_back(idx);
}
sort(hallroom.begin(), hallroom.end());
cout<<"How many examinees ?? : ";
cin>>k;
for(int i=hallroom.size()-1; i>=0; i--)
{
k-=hallroom[i];
if(k<=0)
{
cout<<hallroom.size()-i<<endl;
break;
}
}
return 0;
}