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
How to display the smallest answer
May 2 2017 9:53 PM
Hi currently I have some code that will display several answer. My problem is how can I display the smallest answer only.
Here is my output:
For the final result, I just want display the smallest answer. Like this:
9 7 6 = 2
Only that answer I want to display. How can I do that?
Here is my code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
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<decimal> elementsList =
new
List<decimal>();
foreach (string elementText in elementsText)
{
if
(decimal.TryParse(elementText, out element))
{
elementsList.Add(element);
}
}
int
i;
int
j;
decimal tmp;
int
[] arr1 =
new
int
[10];
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 :"
);
Solver solver =
new
Solver();
List<List<decimal>> results = solver.Solve(goal, elementsList.ToArray());
//results.Reverse();
int
counter = 0;
Boolean recordexist =
false
;
foreach (List<decimal> result in results)
{
if
(counter == 3)
break
;
if
(result.Count == NoDisplay)
{
decimal subtract = 0;
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 :"
);
Solver solvers =
new
Solver();
List<List<decimal>> resultss = solvers.Solve(goal, elementsList.ToArray());
//results.Reverse();
int
counters = 0;
Boolean recordexists =
false
;
foreach (List<decimal> 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
(recordexists ==
true
)
{
//decimal max = result.Max();
//Console.WriteLine(max);
Console.WriteLine(
" = "
+ subtract);
}
counters++;
}
}
if
(recordexists ==
false
)
{
Console.WriteLine(
"No record exist"
);
}
Console.ReadLine();
}
}
Reply
Answers (
9
)
real time example
How to remove space in XML template, If there is no value p