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
.NET
.NET Assemblies
.NET Core
.NET Standard
Active Directory
ADO.NET
Agile Development
AJAX
Alexa Skills
Algorithms in C#
Android
Angular
Architecture
ArcObject
Artificial Intelligence
ASP.NET
ASP.NET Core
Augmented Reality
Aurelia
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blockchain
Bootstrap
Bot Framework
Business
C#
C# Corner
C, C++, MFC
Career Advice
Chapters
CIO
Cloud
COBOL.NET
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Cortana Development
Cryptocurrency
Cryptography
Crystal Reports
Current Affairs
Custom Controls
Cyber Security
Data Mining
Databases & DBA
Design Patterns & Practices
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
Expression Studio
F#
Files, Directory, IO
Games Programming
GDI+
General
Google Cloud
Google Development
Graphics Design
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Internet & Web
Internet of Things
Ionic
iOS
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
kotlin
Leadership
Learn .NET
LightSwitch
LINQ
Machine Learning
Microsoft 365
Microsoft Office
Microsoft Phone
Mobile Development
Multithreading
NetBeans
Networking
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Power BI
Printing in C#
Products
Progressive Web Apps
Project Management
Python
Q#
QlikView
R
React
Reports using C#
Robotics & Hardware
Ruby on Rails
Salesforce
Security
Servers
SharePoint
SignalR
Silverlight
Smart Devices
Software Testing
SQL Language
SQL Server
Startups
String in C#
Swift
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
WCF
Wearables
Web Development
Web Services
Web3
Windows 10
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML Standard
XML
XNA
XSharp
Register
Login
2
Answers
c# how to sum all output from combination that i get
Ms Lyana
7y
303
1
Reply
Please, I really need help. I have code that ask user to key-in the target, random number and how much combination that user want. Then, the output must bigger than the target .
This is the output :
So how can i change this output to be like this :
9 8 7 = 24
9 8 6 = 23
9 7 6 = 22
And then after sum the number, I need to subtract the answer with the target that user key-in. Example like this:
24 - 20 = 4
23 - 20 = 3
22 - 20 = 2
Here is my code that I do to get the output like the image:
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 elementsList =
new
List();
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());
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)
{
recordexist =
true
;
foreach (decimal value in result)
{
Console.Write(
"{0}\t"
, value);
}
if
(recordexist ==
true
)
{
Console.WriteLine();
}
counter++;
}
}
if
(recordexist ==
false
)
{
Console.WriteLine(
"No record exist"
);
}
Console.ReadLine();
}
}
public
class
Solver
{
private
List> mResults;
public
List> Solve(decimal goal, decimal[] elements)
{
mResults =
new
List>();
RecursiveSolve(goal, 0.0m,
new
List(),
new
List(elements), 0);
return
mResults;
}
private
void
RecursiveSolve(decimal goal, decimal currentSum,
List included, List notIncluded,
int
startIndex)
{
for
(
int
index = startIndex; index < notIncluded.Count; index++)
{
decimal nextValue = notIncluded[index];
if
(currentSum + nextValue > goal)
//bigger than target
{
List newResult =
new
List(included);
newResult.Add(nextValue);
mResults.Add(newResult);
}
else
if
(currentSum + nextValue < goal)
{
List nextIncluded =
new
List(included);
nextIncluded.Add(nextValue);
List nextNotIncluded =
new
List(notIncluded);
nextNotIncluded.Remove(nextValue);
RecursiveSolve(goal, currentSum + nextValue,
nextIncluded, nextNotIncluded, startIndex++);
}
}
}
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
How to set the A4 page size in c# code.
HOW TO PROTECT MY WEB APPLICATIONS FOLDER