Я новичок в C#
, и я пытаюсь сделать программу, которая берет ввод и вычисляет ход: 1 ход или 2 ходов и всех комбинаций (перестановок) этих движений.C# Перестановки строки для n = ((n - 1) + (n - 2))
Моя формула n = ((n - 1) + (n - 2))
Проблема, которую я имею пытается выводить результаты - я попытался перекручивание через них, если потом еще логикой, но это беспорядок. Я не уверен, как построить массив для поддержки перестановок.
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();
}
}
}
'п = ((п - 1) + (п - 2))' не имеет смысла с вашим выходом. Учитывая 'n = 1', мы имеем 1 = (1 - 1) + (1 - 2)' или: '1 = 0 - 1'. – Rob
что вам нужно на самом деле? Каков код, который вы указали? –
Добавил мой код выше. Нужен лучший способ вывода моих ходов. –