Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
8
Answers
Display the minimum answer
Ms Lyana
7y
256
1
Reply
Hi, I have problem to display my minimum value from the answer that I got.
Here is my output when I run :
The problem is, at the minimum result it shows nothing. Supposely it will show like this
9 7 6 =2
because that is the minimum value (2).
Here is my code:
class
Program
{
static
void
Main(string[] args)
{
string input;
int
NoDisplay;
decimal goal;
decimal element;
do
{
Console.WriteLine(
"Please enter the target:"
);
input = Console.ReadLine();
}
while
(!decimal.TryParse(input, out goal));
Console.WriteLine(
"Please enter the numbers (separated by spaces)"
);
input = Console.ReadLine();
string[] elementsText = input.Split(
' '
);
List
elementsList =
new
List
();
foreach (string elementText in elementsText)
{
if
(decimal.TryParse(elementText, out element))
{
elementsList.Add(element);
}
}
int
i;
int
j;
decimal tmp;
for
(i = 0; i < elementsList.Count; i++)
{
for
(j = i + 1; j < elementsList.Count; j++)
{
if
(elementsList[i] < elementsList[j])
{
tmp = elementsList[i];
elementsList[i] = elementsList[j];
elementsList[j] = tmp;
}
}
}
Console.WriteLine(
"Please enter the maximum combination :"
);
NoDisplay = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(
"The results is :"
);
//the first result
Solver solver =
new
Solver();
List
> results = solver.Solve(goal, elementsList.ToArray());
//results.Reverse();
int
counter = 0;
Boolean recordexist =
false
;
foreach (List
result in results)
{
if
(counter == 3)
break
;
if
(result.Count == NoDisplay)
{
decimal sum = 0;
recordexist =
true
;
foreach (decimal value in result)
{
Console.Write(
"{0}\t"
, value);
sum = sum + value;
}
if
(recordexist ==
true
)
{
Console.WriteLine(
" = "
+ sum);
}
counter++;
}
}
if
(recordexist ==
false
)
{
Console.WriteLine(
"No record exist"
);
}
Console.WriteLine(
"The final results is :"
);
//the last result
Solver solvers =
new
Solver();
List
> resultss = solvers.Solve(goal, elementsList.ToArray());
//results.Reverse();
int
counters = 0;
Boolean recordexists =
false
;
decimal minSub = 0;
//to holding minimun total
List
minResult =
new
List
();
//to holding minimum Result
foreach (List
result in results)
{
if
(counters == 3)
break
;
if
(result.Count == NoDisplay)
{
decimal subtract = 0;
decimal sum = 0;
recordexists =
true
;
foreach (decimal value in result)
{
Console.Write(
"{0}\t"
, value);
sum = sum + value;
subtract = sum - goal;
}
if
(subtract < minSub)
{
minSub = subtract;
foreach (decimal value in result)
{
minResult.Add(value);
}
}
if
(recordexists ==
true
)
{
Console.WriteLine(
" = "
+ subtract);
}
counters++;
}
}
if
(recordexists ==
false
)
{
Console.WriteLine(
"No record exist"
);
}
Console.WriteLine(
"The minimum results is :"
);
//the minimum result line
foreach (decimal value in minResult)
{
Console.Write(
"{0}"
, minSub);
}
Console.ReadLine();
}
}
Thanks in advance
Post
Reset
Cancel
Answers (
8
)
Next Recommended Forum
How to convert the file into pdf while uploading it to the d
How to deployment asp.net Applicati on IIS Server with sql