Skip to content

Commit 71ac97d

Browse files
author
Carlos Leonard
committed
update
1 parent 4f07709 commit 71ac97d

File tree

4 files changed

+93
-36
lines changed

4 files changed

+93
-36
lines changed

Debug/Fun Practice.log

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
Build started 10/24/2016 5:28:26 PM.
1+
Build started 10/25/2016 1:55:12 PM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
33
1>ClCompile:
44
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt code50.cpp
55
code50.cpp
6+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\code50.cpp(23): warning C4018: '<' : signed/unsigned mismatch
7+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\code50.cpp(79): warning C4018: '<' : signed/unsigned mismatch
8+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\code50.cpp(92): warning C4018: '<' : signed/unsigned mismatch
69
Link:
710
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\code50.obj
811
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
912
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
1013

1114
Build succeeded.
1215

13-
Time Elapsed 00:00:01.67
16+
Time Elapsed 00:00:01.11
142 Bytes
Binary file not shown.

Debug/vc120.idb

0 Bytes
Binary file not shown.

code50.cpp

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,37 @@ Another example : if arr is[4, 5, 2, 3, 1, 0] then you can distribute the sandwi
77
*/
88

99
#include <iostream>
10-
#include <string>
10+
#include <string>
11+
#include <vector>
1112
using namespace std;
1213

14+
void bubbleSort(vector <int>& list, vector <int>& index)
15+
{
16+
int temp, temp2;
17+
bool swap;
18+
19+
do
20+
{
21+
swap = false;
22+
23+
for (int x = 1; x < list.size()-1; x++)
24+
{
25+
// Will move the elements around in the new list
26+
// Also keeps track of the index in relation to the original array
27+
if (list[x] < list[x + 1])
28+
{
29+
temp = list[x];
30+
temp2 = index[x];
31+
index[x] = index[x + 1];
32+
index[x + 1] = temp2;
33+
list[x] = list[x + 1];
34+
list[x + 1] = temp;
35+
swap = true;
36+
}
37+
}
38+
} while (swap);
39+
}
40+
1341
int FoodDistribution(int arr[], int size)
1442
{
1543
int total = arr[0]; // Number of Sandwiches
@@ -20,42 +48,52 @@ int FoodDistribution(int arr[], int size)
2048
// In some cases we might not need to do food distribution if the difference across is 0
2149
for (int x = 1; x < size-1; x++)
2250
{
23-
difference += (arr[x] - arr[x + 1]);
51+
int temp = arr[x] - arr[x + 1];
52+
if (temp < 0)
53+
{
54+
temp *= -1;
55+
}
56+
difference += temp;
2457
}
2558
if (difference == 0)
2659
{
2760
return 0;
2861
}
2962

30-
// Loop to distribute food to those that are hungry
31-
// The distribution will be based by comparing the hunger levels adjacent to each other
32-
// The goal is to get the hunger levels to be equal across all people if possible
63+
vector <int> newList, indexConect;
64+
newList.push_back(-1); // Garbage value just to keep the size parallel to original array
65+
indexConect.push_back(-1);
66+
67+
for (int x = 1; x < size; x++)
68+
{
69+
newList.push_back(arr[x]);
70+
indexConect.push_back(x);
71+
}
72+
73+
bubbleSort(newList, indexConect);
74+
75+
// Loop to distribute the food
3376
do
3477
{
3578
give = false;
36-
37-
for (int x = 1; x < size - 1; x++)
79+
for (int x = 1; x < newList.size() - 1 && total > 0; x++)
3880
{
39-
if (x == 1)
40-
{
41-
if (arr[x] > arr[x + 1] && total > 0)
42-
{
43-
give = true;
44-
arr[x]--;
45-
cout << "index " << x << " now is " << arr[x] << endl;
46-
total--;
47-
}
48-
}
49-
else if (arr[x] > arr[x + 1] && arr[x] > arr[x - 1] && total > 0)
81+
if (newList[x] > newList[x + 1])
5082
{
5183
give = true;
52-
arr[x]--;
53-
cout << "index " << x << " now is " << arr[x] << endl;
84+
newList[x]--;
5485
total--;
86+
break;
5587
}
5688
}
5789
} while (give);
5890

91+
// Loop to match the new list to the original order
92+
for (int x = 1; x < newList.size(); x++)
93+
{
94+
arr[x] = newList[indexConect[x]];
95+
}
96+
5997
// Checking for the difference after the distribution took place
6098
difference = 0;
6199
for (int x = 1; x < size - 1; x++)
@@ -78,28 +116,44 @@ int main()
78116
int C[] = { 5, 2, 2, 2, 2, 2 };
79117
int D[] = { 5, 2, 3, 4, 5 };
80118
int E[] = { 3, 2, 1, 0, 4, 1, 0 };
119+
int F[] = { 4, 5, 4, 5, 2, 3, 1, 2 };
120+
int G[] = { 7, 5, 4, 3, 4, 5, 2, 3, 1, 4, 5 };
81121
cout << FoodDistribution(A, sizeof(A)/sizeof(A[0])) << endl; // 0
82122
cout << FoodDistribution(B, sizeof(B) / sizeof(B[0])) << endl; // 2
83123
cout << FoodDistribution(C, sizeof(C) / sizeof(C[0])) << endl; // 0
84124
cout << FoodDistribution(D, sizeof(D) / sizeof(D[0])) << endl; // 1
85125
cout << FoodDistribution(E, sizeof(E) / sizeof(E[0])) << endl; // 4
86-
return 0;
126+
cout << FoodDistribution(F, sizeof(F) / sizeof(F[0])) << endl; // 3
127+
cout << FoodDistribution(G, sizeof(G) / sizeof(G[0])) << endl; // 6
87128

88129
/*
89-
2 3 4 5 5
90-
2 2 4 5 4
91-
2 2 3 5 3
92-
2 2 3 4 2
93-
2 2 2 4 1
94-
2 2 2 3 0
95-
96-
97-
2 1 0 4 1 0 3
98-
2 1 0 3 1 0 2
99-
2 1 0 2 1 0 1
100-
2 1 0 1 1 0 0
130+
5 4 5 2 3 1 2
131+
132+
4 4 4 2 3 1 2
133+
4 4 4 2 2 1 2
134+
4 4 4 2 2 1 1
135+
0 0 2 0 1 0
136+
137+
138+
5 5 4 3 2 2 1 4
139+
5 4 4 3 2 2 1 3
140+
4 4 4 3 2 2 1 2
141+
4 4 3 3 2 2 1 1
142+
4 3 3 3 2 2 1 0
143+
144+
4 3 3 2 3 1 2
145+
1 0 1 1 2 1
146+
147+
101148
102-
1 1 1 0 1
149+
5 4 3 4 5 2 3 1 4 5
150+
151+
103152
153+
104154
*/
155+
156+
157+
158+
return 0;
105159
}

0 commit comments

Comments
 (0)