gpt4 book ai didi

C# 首次拟合算法输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:27:50 25 4
gpt4 key购买 nike

我被分配了一个家庭作业来编写 first-fit 算法,它的意思是根据 first-fit 算法将一定数量的作业分配给一定数量的内存块。到目前为止,我的算法可以正常工作并正确分配工作;但是,我的问题是,当我排队的作业输出到控制台时,我的循环不会遍历所有发送到队列的作业,它只显示发送到队列的初始作业及其大小,直到所有要发送的作业对队列进行了迭代。

这可能非常简单,但我似乎无法弄明白...。任何其他需要的都可以提出,我可以提供。谢谢。

public void firstFit(int counter, int JobIndex)
{

Counter = counter;
jobIndex = JobIndex;

do {
if (jobSize[jobIndex] > memorySize[Counter-1])
{
Counter += 1;
}
else
{
Console.Text += Environment.NewLine + "Job " + (jobIndex+1) + " of size "
+ jobSize[jobIndex] +
" has been loaded into memory block:"
+ Counter;

memorySize[Counter-1] = memorySize[Counter-1]-jobSize[jobIndex];

Console.Text += Environment.NewLine + "The size of memory block " + Counter + " is now " + memorySize[Counter-1];

Counter = 1;
jobIndex += 1;
}

} while (Counter <= blocks && jobIndex < jobs);

for (int i = 0; i < jobs-jobIndex; i++ ){

jobQ = jobIndex;

if (jobQ < jobs)
{
Console.Text += Environment.NewLine + "Job " + (jobQ+1) + " of size " + jobSize[jobQ] + " is sent to waiting queue!";


}
}

Job 1 of size 45 has been loaded into memory block:1 The size of memory block 1 is now 16

Job 2 of size 1230 has been loaded into memory block:2 The size of memory block 2 is now 410

Job 3 of size 325 has been loaded into memory block:2 The size of memory block 2 is now 85

Job 4 of size 303 has been loaded into memory block:3 The size of memory block 3 is now 131

Job 5 of size 1178 has been loaded into memory block:5 The size of memory block 5 is now 393

Job 6 of size 1276 has been loaded into memory block:6 The size of memory block 6 is now 426

Job 7 of size 965 has been loaded into memory block:7 The size of memory block 7 is now 322

Job 8 of size 537 is sent to waiting queue!

Job 8 of size 537 is sent to waiting queue!

Job 8 of size 537 is sent to waiting queue!

最佳答案

最后有这段代码:

for (int i = 0; i < jobs-jobIndex; i++ )
{
jobQ = jobIndex;
if (jobQ < jobs)
{
Console.Text += Environment.NewLine + "Job " + (jobQ+1) + " of size " +
jobSize[jobQ] + " is sent to waiting queue!";
}
}

jobQ 的值在每次循环中都是相同的,所以当然每次都会打印相同的内容。有没有可能你的意思是循环中的第一行是:

jobQ = jobIndex + i;

??

关于C# 首次拟合算法输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22303235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com