搜索内容

有一个问题?

如果您有任何疑问,可以在下面询问或输入您要寻找的!

c#编程题,有大佬会的吗,请留下代码

技术聚客 未结 1 332
逐至
逐至 2021-03-08 00:18

Write a program that reads a string from standard input. The string will appear on a single line, and will contain no whitespace characters. You may assume that all characters in the string are unique.

Your program should print out all non-empty subsequences of characters that can be formed from the characters in the input string. Write each subsequence on a separate line. The characters in each subsequence must appear in the same order as in the original string. You may write the subsequences in any order.

Important: All of your code must appear in Main(); you may not write any other functions/methods (nested or otherwise). After all, we haven't learned about writing methods in C# yet. :) And so you may not use recursion to solve this problem as we did in Programming 1.

Sample input:

jump

Possible output:

j u ju m jm um jum p jp up jup mp jmp ump jump

Hint: If N is the length of the input string, there are 2^N possible subsequences (including the empty sequence). That's because each of the N input letters either is or is not in the output, so there are 2 possibilities for that letter.

There also happen to be 2^N numbers that can be written using N digits in binary representation (i.e. base 2). If you can generate those binary numbers, you can use each one to generate an output string (excepting the empty string).

相关标签:
1条回答
  • 2021-03-08 00:29

    这不就是一个排列组合的实现么? 可以递归实现:

    LoopAll(array, N)

    {

    loop (单词的长度 从 1 .. N) 

            如果N == 0; 那么就打印迄今为止的字符串; 返回;

           选择一个开始的字符; 把它接到要输出的字符串最后;

            LoopAll(剩下的字符, 选 N - 1)

    }

    0 讨论(0)
提交回复