m0_54132659 2021-03-10 11:24 采纳率: 83.3%
浏览 196
已结题

c# 编程题,大佬直接留下代码,输入字符串(可以是字母也可以是数字)输出所有子字符串不能用递归等

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).

  • 写回答

5条回答 默认 最新

  • ProfSnail 2021-03-10 11:53
    关注
    using System;
    namespace HelloWorldApplication
    {
        class HelloWorld
        {
            static void Main(string[] args)
            {
                /* 我的第一个 C# 程序*/
    			String line;
                //line="jump";
    			line = Console.ReadLine();
    			int n = line.Length;
    			long  maxn = 1 << n;
    			for(int i = 0; i < maxn; i++){
    				for(int j = 0; j < n; j++){
    					long mask = 1 << j;
    					long masked = i & mask;
    					if(masked!=0){
    						Console.Write(line[j]);
    					}
    				}
    				Console.Write("\n");
    			}
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制