ユーザーは1から19までの範囲の数値を入力しています...できるだけ多くを見つける必要があります。各番号は1回のみ使用できます。C#で合計まで加算した数値のリストを検索する
したがって、リストは次のとおりです: 19,17,11,13,8,9,7,5,10,16,14,8,7,3。
、出力は次のようになります。
19、11、10
16、14、7、3
17、8、8、13を7
、9および5は、リストに残さ。
私はいくつかの提案を見つけましたが、1つのマッチを探すように見えましたが、自分で試しましたが、それでもまだ洗練されていません。私はすぐに残していると、コードの実際のブロックのタイプすることはできませんが、ここでこれを行うだろうかだ
private void btnCalc_Click(object sender, RoutedEventArgs e)
{
// Copy the list to a new list:
List<int> lookInto = new List<int>();
foreach(int i in weaponList)
{
lookInto.Add(i);
}
int lookFor = 40;
while (lookFor > 0)
{
lookFor = Search(lookInto, lookFor);
}
if (lookFor != -1)
{
listSell.Items.Add(answer);
}
}
private int Search(List<int> hay, int needle)
{
int lowestValue = hay.Min();
int highestValue = hay.Max();
if (hay.BinarySearch(needle) > 0)
{
int index = hay.BinarySearch(needle);
if (answer == "")
{
answer += hay[index].ToString();
needle -= hay[index];
hay.Remove(needle);
}
else
{
answer += ", " + hay[index].ToString();
needle -= hay[index];
hay.Remove(needle);
}
}
if (needle - highestValue > lowestValue || needle - highestValue == 0)
{
if (answer == "")
{
answer += highestValue.ToString();
needle -= highestValue;
hay.Remove(highestValue);
}
else
{
answer += ", " + highestValue.ToString();
needle -= highestValue;
hay.Remove(highestValue);
}
}
else
{
for (int i = 0; i > hay.Count; i++)
{
if (needle - hay[i] == 0 || needle - hay[i] > lowestValue)
{
if (answer == "")
{
answer += hay[i].ToString();
needle -= hay[i];
hay.RemoveAt(i);
}
else
{
answer += ", " + hay[i].ToString();
needle -= hay[i];
hay.RemoveAt(i);
}
}
}
if (needle > 0)
{
needle = -1;
}
}
return needle;
}
質問は何ですか? – hatchet
また、この宿題はありますか? –
質問は基本的に、例のような値を見つけるために何をする必要がありますか?そして、いいえ、それは私が自分で作っているマイナーなプログラムのための宿題ではありません。 – Dalmar