【发布时间】:2020-04-21 07:19:51
【问题描述】:
谁能帮我解决这个问题?
它应该是一个要求输入名字和姓氏的 ConsoleApp。 从所有名称中,它应该生成 4 个字符。 我必须提供字符串的不同可能组合(例如 1. 和 3. char of fn 和 2. 3. char of ln ...) 该算法应该能够提供 4 个字符的所有可能组合,其中 2 个来自名字,2 个来自姓氏。
到目前为止,我只对名字和姓氏进行了 2 的组合。
static void Main(string[] args)
{
Console.WriteLine("Enter your first name");
string firstName = Console.ReadLine();
Console.WriteLine("Enter your last name");
string lastName = Console.ReadLine();
string[] result = GetAllCombinations(firstName);
string[] code = GetAllCombinations(lastName);
PrintTheCombinations(result);
PrintTheCombinations(code);
}
private static void PrintTheCombinations(string[] list)
{
foreach (var results in list)
{
Console.WriteLine(results);
}
}
private static string[] GetAllCombinations(string word)
{
int arraylength = word.Length * word.Length;
var ret = new string[arraylength];
for (int i = 0; i < word.Length; i++)
{
for (int j = 0; j < word.Length; j++)
{
ret[i * word.Length + j] = string.Concat(word[i], word[j]);
}
}
return ret;
}
现在我需要打印 4 个字符,2 个 fn 和 2 个 ln,但我卡住了。 希望大家明白我的意思
【问题讨论】:
-
您应该能够在采用 2
string[]的新方法中使用几乎完全相同的逻辑。刚才结果的长度将是数组 1 的长度 * 数组 2 的长度。i将循环数组 1 的长度,j循环数组 2 的长度。 -
字符顺序重要吗?是否允许多次使用同一字符,例如
word[1]和word[1]? -
你能给我一些输入和输出的例子吗?
-
嗯,是的,我希望没有重复 :)
标签: c#