运输理论(西北角法)。迭代问题。 [英] Transportation Theory (North-West Corner Method). Problem with iterations.

查看:146
本文介绍了运输理论(西北角法)。迭代问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看起来像while循环有问题。它只进行一次迭代。有没有什么建议?



我的代码:

<前lang =C#> // libraries
#include< stdafx.h>
#include< iostream>

// 标准命名空间
using namespace std;

// 检查数组是否为空的函数
bool notEmpty( int * array, int arraySize)
{
int sum = 0 ;
for int i = 0 ; i < arraySize; ++ i)
sum + = array [i];

return sum == 0 ; // 如果sum == 0,则返回true,否则为false
}


int main()
{
// 设置资源数组
int resources [] = { 200 180 190 };
// 设置需要数组
int 需要[] = { 150 130 150 140 };

int m = 3 ,n = 4 ; // m - 资源字段,n - 需要字段

// 设置支出
int 支出[ 3 ] [ 4 ] = {{ 7 8 1 2 } ,
{ 4 5 9 8 },
{ 9 2 3 6 }};

// 设置结果数组以写入进度
int 结果[ 3 ] [ 4 ] = { { 0 0 0 ,< span class =code-digit> 0
},
{ 0 0 0 0 },
{ 0 0 0 0 }};


int i = 0 ,j = 0 ;
bool resultBool;
执行
{
resultBool = false ; // 检查零相等元素

if (resources [i]>需要[j]) // 如果有更多资源然后需要
{
result [i] [j] = needs [j]; // 需要满足
resources [i] - = needs [j]; // 剩余资源


}
< span class =code-keyword> else
if (resources [i]< needs [j]) // 如果资源不足
{
result [i] [j] = resources [j]; // 提供尽可能多的资源
需要[j] - = resources [i] ; // 还需要多少?
}


else goto l; // 去检查归零
l:resultBool = notEmpty(needs, 0 ); // check nulling
}

// 输出数据
(!resultBool) ;
cout<< 路径:<< ENDL;
for (i = 0 ; i < ; m; i ++)
{
for (j = 0 ; j < n; j ++)
cout<<结果[i] [j]<< ;
cout<< ENDL;
}
double sum = 0 ; // 路径价格
(i = 0 ; i < m; i ++)
{
for (j = 0 ; j < n; j ++)
sum + = result [i] [j] * outgoings [i] [j];
}
cout<< 资源:<<和;
cout<< ENDL;
system( pause);
}





输出:

路径:
150 0 0 0
0 0 0 0
0 0 0 0
资源: 1050





但输出必须与图片链接(粗体文字)相似:

https://drive.google.com/file/ d / 0B2RsqtI-JTVxR3pMeTlzN2UyX28 / view?usp = sharing [ ^ ]



所以它确实是第一个t迭代好,然后什么都不做......



求助!

解决方案

你真的需要清理代码缩进。这将使您的代码更容易阅读和调试。



此外,使用调试器并逐步执行代码和检查变量可以极大地帮助解决这样的问题。这是你的逻辑,我们不知道你正在尝试用它做什么或它应该做什么。



我真的质疑为什么你甚至需要 goto 该代码中的语句,因为您没有。删除该语句(以及它上方的其他语句)根本不会改变行为或代码。



这似乎是作业。我们不打算为你做这件事。


I have problem with while loop, as it seems. It does just one iteration. Is there any suggetsions?

My code:

//libraries
#include <stdafx.h>
#include <iostream>

//standard namespace
using namespace std;

//function to check if array is not empty
bool notEmpty(int * array, int arraySize)
{
	int sum = 0;
	for (int i = 0; i < arraySize; ++i)
		sum += array[i];

	return sum == 0;	// if sum==0, true returned, else false
}


int main()
{
	//setting resources array
	int resources[] = { 200, 180, 190 };
	//setting needs array
	int needs[] = { 150, 130, 150, 140 };

	int m = 3, n = 4; //m - resources fields, n - needs fields

	//setting outgoings
	int outgoings[3][4] = { { 7, 8, 1, 2 },
	{ 4, 5, 9, 8 },
	{ 9, 2, 3, 6 } };

	//setting result array to write progress
	int result[3][4] = { { 0, 0, 0, 0 },
	{ 0, 0, 0, 0 },
	{ 0, 0, 0, 0 } };


	int i = 0, j = 0;
	bool resultBool;
	do
	{
		resultBool = false; //check for zero equal elements

		if (resources[i]>needs[j]) //if there's more resources then needss
		{
			result[i][j] = needs[j]; //needs satisfied
			resources[i] -= needs[j]; //resources left
			
			
		}
		else if (resources[i]<needs[j]) //if not enough resources
		{
			result[i][j] = resources[j]; //giving as many resources as possible
			needs[j] -= resources[i]; //how much is still needed?
		}
		
		
		else goto l; //go to check nulling
	l:resultBool = notEmpty(needs, 0);	//check nulling
	}

	//outputing data
	while (!resultBool);
		cout << "Path: " << endl;
		for (i = 0; i < m; i++)
		{
			for (j = 0; j < n; j++)
				cout << result[i][j] << " ";
			cout << endl;
		}
		double sum = 0; //path price
		for (i = 0; i < m; i++)
		{
			for (j = 0; j < n; j++)
				sum += result[i][j] * outgoings[i][j];
		}
		cout << "Resources: " << sum;
		cout << endl;
		system("pause");
}



Output:

Path:
150 0 0 0
0 0 0 0
0 0 0 0
Resources: 1050



But the output have to be like in the picture linked (bold text):
https://drive.google.com/file/d/0B2RsqtI-JTVxR3pMeTlzN2UyX28/view?usp=sharing[^]

So it does first iteration well, and then does nothing...

Help, please!

解决方案

You really need to clean up your code indentation. It'll make your code much easier to read and debug.

Also, using the debugger and stepping through the code and examining variables greatly helps to solve problems like this. It's your logic and we have no idea what you're trying to do with it or what it should be doing.

I really question why you even need the goto statement in that code, because you don't. Removing that statement (and the else right above it) doesn't change the behavior or the code at all.

This appears to be homework. We're not going to do it for you.


这篇关于运输理论(西北角法)。迭代问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆