As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have array of int[10000].
I need to sum each int with other one, and show result, for only those, where sum are > N.
Sum, can be any with any element of array, also sum of 5,6,7...10000 elements of array which > N.
I can write down(all combinations, but it`s insane) it like a[1] + a[2] + a[3]... But may be there are other resolution?
I need result all combinations, that gives me sum which is >N
Okey. If it is array of int[10]?
Your problem is similar to Subset Sum problem. Here you can find two solutions for this algorithm. The only change is you have to track your numbers whose sum is greater that N and you need to repeat it for all possibilities instead of just finding true/false result.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Everytime I have to access arrays via index, it bugs me that I have to check that I am not creating IndexOutOfRange Exceptions.
I get why arrays throw an exception instead of just returning null. That would be cool because I could use the ?? operator, but ok these are legacy types and I am fine with it.
But why did the BCL Team not implement TryGet(int index, out T value)
like they did with, for example, List<T>?
From my understanding, they could have put this into the Linq Extensions as Array already implements IEnumerable
You need to turn your thinking around - it is throwing IndexOutOfRangeException because you should not be accessing those indexes in the first place.
Instead, you should check the array length before you access the index:
string value = null;
if (i < array.Length) value = array[i];
or
for (int i=0; i<array.Length; i++) {
...
}
IndexOutOfRangeException is classed as a boneheaded exception - if it is being thrown, you are doing something wrong.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
In c#, how can I find a specefic character in a string. this "2222+2222" is my string and I want to find the character "+" in it? I want a function that returns a bool if it finds it.
string.Contains("+") returns a bool.
yourString.IndexOf("+") will return 0 or a positive number if the character is found
Since you prefer something that returns bool, you can use Contains instead but notice that Contains does not provide an overload to perform a case-insensitive search. In scenarios where this is important (finding a string without caring for case), it's best to use IndexOf(stringToSearch,StringComparison.InvariantCultureIgnoreCase) to determine whether the string is found or not.
String s = "2222+2222";
if (s.Contains("+")) {
// dosomething...
}
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I would like to know which way is better and faster for the following scenarios.
string dateStart = ((DateTime)dtRow["StartDate"]).ToShortDateString();
or
string dateStart = DateTime.Parse(dtRow["StartDate"].ToString()).ToString("dd/MM/yyyy")
If the type of value stored in the StartDate column of data-table is already DateTime, the first one is faster than the second. Otherwise we can't compare them, because the first one crashes.
Cast is doubtfully better because is only appropriate way if underlying data is type of DateTime or compatible.
Second way converts DateTime to String and then back to DateTime what is pointless.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have a List of Lists, call it listholder. I want to count how many lists listholder contains. listholder.count() does not seem to work (no definition).
I declared it this way:
List<List<T>> listholder = new List<List<T>>();
and tried
listholder.count()
listholder.Count() will return the number of elements in that list, i.e. the number of lists it contains.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
string a = "sea";
string b = "SEA"
if (a == b)...
How could I say that the two strings are the same, regardless of character casing ?
Use string.compare:
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
if (string.Compare(a, b, true) == 0)
{
...
}
if (0 == String.Compare(a, b, true))...
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
use String.Equals() and use the correct StringComparison-Value:
if(a.Equals(b, StringComparison.CurrentCultureIgnoreCase))
{
...//strings are equal
}
#OP: Please follow the guidelines of whathaveyoutried.com and read the docs... ...this way you would bwe able to answer things like that for yourself and you'll end up learn much more about the language and technique... :)