//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");
}
Другие языки программирования и технологии
Помогите пожалуйста перевести код с C++ в C#
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();
}
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();
}
Андрианов Роман
спасибо
Похожие вопросы
- Помогите пожалуйста перевести код программы с C++ на Си.
- Помогите пожалуйста исправить код программы на visual c++!!
- здравствуйте, помогите пожалуйста, что не так вот c C++ кодом:
- помогите пожалуйста кто смыслит в C# или в C++, очень нужна ваша помощь, нужен код программы, точнее двух
- Помогите пожалуйста разобрать задачу по програмированию на C++.
- HTML! помогите пожалуйста написать код для сайта простого сайта!
- Помогите пожалуйста решить на языке C++
- люди, помогите пожалуйста найти код от вируса
- Помогите пожалуйста оптимизировать решение задачи (Зайчик) на C++
- Помогите пожалуйста написать программу на C#