Другие языки программирования и технологии

Помогите пожалуйста перевести код с C++ в C#

//libraries
#include
#include

//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");
}
public static bool notEmpty(int[] arr, int arraySize) => arr.Take(arraySize).Sum() == 0;

static void Main(string[] args)
{
int[] resources = { 200, 180, 190 };
int[] needs = { 150, 130, 150, 140 };
int m = 3, n = 4;
int[,] outgoings = {
{ 7, 8, 1, 2 },
{ 4, 5, 9, 8 },
{ 9, 2, 3, 6 } };

int[,] result = {
{ 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);
Console.WriteLine("Path: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", result[i,j]);
Console.WriteLine();
}
double sum = 0; //path price
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
sum += result[i, j] * outgoings[i, j];
}

Console.WriteLine("Resources: {0}", sum);

Console.ReadKey();
}
Сергей Свазьян
Сергей Свазьян
61 656
Лучший ответ