我是C#
的新手,我想製作一個程序,需要輸入並計算出移動:1移動或2移動和這些移動的所有組合(排列)。C#排列字符串n =((n - 1)+(n - 2))
我的公式是n = ((n - 1) + (n - 2))
我有正試圖輸出結果的問題 - 我試圖通過他們,如果再else邏輯循環,但它是一個爛攤子。我不知道如何構建一個數組來支持排列。
If I move n=1 the result is 1 (1)
If I move n=2 the results is 2 (11,2)
If I move n=3 the results is 3 (111,12,21)
If I move n=4 the results is 4 (1111,112,211,121,22)
任何幫助,將不勝感激:)
謝謝!
我的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleAppFrog
{
public class Program
{
public void Main(string[] args)
{
// Number of moves by using only 1 or 2 moves
Console.WriteLine("Enter a number of moves?");
int myNumber = Convert.ToInt32(Console.ReadLine());
int combenations = 0;
Console.WriteLine("You Chose " + myNumber + " moves :)");
// Math = ((myNumber - 1) + (myNumber - 2)) = combenations
combenations = ((myNumber - 1) + (myNumber - 2));
Console.WriteLine("There are " + combenations + " combinations of moves");
// Need to output the permutations of combenations
var moves = "";
// Gives the 1 moves
for (int i = 1; i <= myNumber; i++)
{
moves += "1 ";
}
Console.WriteLine(moves);
var moves2 = "";
// Get the 2 moves and check for remainder
for (int i = 1; i <= (myNumber/2); i++)
{
moves2 += "2 ";
}
var moves3 = "";
//need to add on 1 move if there is a remainder
if (myNumber % 2 != 0)
{
moves3 += "1 ";
}
Console.WriteLine(moves2 + moves3);
Console.WriteLine(moves3 + moves2);
// Stuck trying to figure out the rest of moves
//-------------------------------------------------------------------------
Console.WriteLine("Done...");
Console.Read();
}
}
}
'N =((N - 1)+(N - 2))'不會與你的輸出感。給定'n = 1',我們有'1 =(1 - 1)+(1 - 2)',或者:'1 = 0 - 1'。 – Rob
實際上你需要什麼?你給出的代碼是什麼? –
在上面添加了我的代碼。需要更好的方式輸出我的動作。 –