TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ms Lyana
NA
68
2.7k
Display the minimum answer
May 4 2017 12:13 AM
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
Reply
Answers (
8
)
How to convert the file into pdf while uploading it to the d
How to deployment asp.net Applicati on IIS Server with sql