Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write a c# program that does following:
Takes two parameters: a list of objects and a number
Iterates through the list and find the first set of objects that equals to the number.
Stop iteration if a set is found and the return the set.
So, I have a list of user defined object, say Person as an example. Say, Person object has two fields, Name and age. For example,
MyList
- Person1: John, 10
- Person2: Mary, 25
- Person3: Mike, 35
- Person4: Ann, 20
- Person5: Joe, 5
I want to find a set from the list that equals to the number that I am passing in. If I am passing in above list and 50, I want to return Person2, Person4, Person5 as a list.
This is subset sum problem.
Unfortunately, it is NP-Complete, so there is no known polynomial solution for it.
However, it does have a pseudo-polynomial solution, using Dyanamic Programming, that is using the next recursive function:
f(i,0) = true
f(0,k) = false (k != 0)
f(i,k) = f(i-1,k) or f(i-1,k-weight[i])
Running with f(n,W) yields true if such solution exists, and false otherwise.
The dynamic programming solution fills up a table, after the table is full, you need to "retrace" your steps from table[n][W] back in order to find which items should be included in the set.
More information on getting the actual elements from the table can be found in this thread
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm currently using Bellman Ford algorithm to find the shortest paths with negative value. Is there any faster algorithm that would outperform Bellman Ford for finding shortest paths with negative values?
A simple improvement is to only check for "active" nodes instead of iterating on all of them as the naive implementation does.
The reason is that if a node didn't lead to improvements on any of its neighbors and didn't change value in last iteration there is no need to redo the computation again (it will still produce no improvements).
Pseudocode (Python, actually):
A = set([seed])
steps = 0
while len(A) > 0 and steps < number_of_nodes:
steps += 1
NA = set()
for node in A:
for nh in neighbours(node):
x = solution[node] + weight(node, nh)
if x < solution[nh]:
# We found an improvement...
solution[nh] = x
pred[nh] = node
NA.add(nh)
A = NA
A is the "active" node set, where an improvement was found on last step and NA is the "next-active" node set that will need to be checked for improvements on next iteration.
Initially the solution is set to +Infinity for all nodes except the seed where the solution is 0. Initially only the seed is in the "active" set.
Note that in case of negative-sum loops reachable from the seed the problem has no "minimum path" because you can get the total as low as you want by simply looping; this is the reason for the limit on the "steps" value.
If when coming out of the loop A is not empty then there is no solution to the minimum cost problem (there is a negative-sum loop and you can lower the cost by simply looping).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently trying to get a label(lets name it lblMessage) to pull information based on what the user has picked threw radio buttons and check boxes. Lets make the theme a sundae maker form.. people can select there flavor, size, and addons.
I'm just trying to make it so the label displays once they click the confirm button, a message that identifies the number of sundaes, sundae flavor and size the person ordered. like.. (You ordered 2 small hot fudge sundaes which will cost you
$x.xx (where x.xx is the cost for all sundaes ordered). I tried looking this up but i couldn't seem to find what to put into the label code... =\
I am a beginner at C# ... If any other information is needed, I can provide.
What you want to do is set the text of the label. You can use the String.Format method to take a string and replace certain values with values from variables, for example:
var result = MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
Decimal total = int.Parse(textBox1.Text) * 1.25m;
// The {0} will be replaced with the first argument after the format string.
// The total.ToString("C") tells the decimal to format the string into a currency string (http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=vs.110).aspx)
label1.Text = String.Format("You ordered {0} small hot fudge sundaes, which will cost you {1}", textBox1.Text, total.ToString("C"));
}
Now this example still fails if the textbox does not contain an int, but that can easily be handled using the TryParse method.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't have much code to show but I am trying to remove a substring (individual number) from a longer string is a series of numbers.
Ex: Main String is "11,12,15,16,55,33,88,100,121,155,115"
Need to find number 16 and remove it from the string leaving...
11,12,15,55,33,88,100,121,155,115
They are a list of id's from a database so I can't just change them to strings. Also how do I remove it as if it wasn't there?
string numbers = "11,12,15,16,55,33,88,100,121,155,115";
numbers = string.Join(",", numbers.Split(',').Where(num => num != "16"));
But why don't you use a List<int> instead for database ID's?
In this specific use case I would split the string using "," as the separator, remove the element that matches, then join the elements again.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm working on an automated follow up system on highly antiquated techniques of registering the current status of a "Project" the GUI interaction in this system uses what are named "flags" that the user can "check" to designate the current status of a project. There are 11 possible boxes that can be checked and the system accepts multiple selections.
For example a user can select a check-box labeled "Confirmed" and or "Needs Follow Up" and or or "Is Scheduled" and or "Spoken with client" (There are 11 possible selections).
Here is the problem - whoever wrote this saved those selections to the database in a "bit sum" so the what you see is an int of the original bit[] for the check-box selections.
What I need to do is read the integer from the database and turn it back into a bit array of 11 values of 1 || 0 then from that bit array i need to determine which boxes of string value are checked in order to determine weather or not i need to perform an automated follow up.
So basically if "Confirmed" is checked i don't want to follow up
If "Needs-followup" is checked i need to follow up.
The problem here is that multiple selections can be present.
So after the int is turned into a bit[] we have for example
1,0,1,0,0,0,1,1,1,0,1 where each int represents a box checked.
i need to find a way to turn the above into an array of strings representing the box labels to determine which boxes are checked.
The usual way to do this in C# would be to use a flags enumeration - this is exactly what your bit field is.
[Flags]
enum ProjectStatus
{
Confirmed = 1,
NeedsFollowUp = 2,
SpokenWithClient = 4,
....
}
To test if a specific flag is set:
ProjectStatus status = (ProjectStatus)intFromDb;
if( ( status & ProjectStatus.Confirmed ) == ProjectStatus.Confirmed )
// the Confirmed flag is set
There is also a Enum.HasFlags extension method that simplifies this if you are in .NET 4 or higher.
If you do not wish to do it this way, you can find out if the bit at position x is set by doing this:
bool isSet = ( intFromDb & ( 1 << x ) ) != 0;
And use that to build your string.
Edit: I'd also suggest you read up a bit on bitwise operators and what they do. This might be a good start: http://blackwasp.co.uk/CSharpLogicalBitwiseOps.aspx