An example of string truncation and Output

Source: Internet
Author: User

Requirement: If the string "1234" or "12345" is specified, the following sequence is output.

Output->

"1234", "123", "234", "12", "23", "34", "1", "2", "3", "4"

"12345", "1234", "2345", "123", "234", "345", "12", "23", "34", "45 ", "1", "2", "3", "4", "5"

Can a general method be provided?

Please pay attention to the symbol problem !!!!!!!!!!

Package COM. java. interview. algorithm;/*** name: string_output.java ** Date: 20:00:00 ** function: Output special string ** copyright: open ** @ author wangxin **/public class string_output {public static void main (string [] ARGs) {string STR = "12345"; output_string (STR );} /*** output the specified format string ** @ Param Str */Private Static void output_string (string Str) {int Len = Str. length (); For (INT I = 0; I <Len; I ++) {int COUNT = 0; For (Int J = 0; j <= I; j ++) {system. out. print ("\" "+ Str. substring (J, len-I + Count ++) + "\" "); if (I! = (LEN-1) | j! = I) {system. Out. Print (",");}}}}}

What do you see here?

Is it similar to full sorting? 650) This. width = 650; "src =" http://img.baidu.com/hi/jx2/j_0039.gif "alt =" j_0039.gif "/>

Just getting started is much easier than that.

Extend it. I will change the requirements:

A random integer string with the length of Len and no repeated values in the string. The output method shown above is provided. 650) This. width = 650; "src =" http://img.baidu.com/hi/jx2/j_0021.gif "alt =" j_0021.gif "/>

Example:

Generate a random numeric string "7902"

Output"0279", "027", "279", "02", "27", "79", "0", "2", "7", "9"

650) This. width = 650; "src =" http://img.baidu.com/hi/jx2/j_0063.gif "alt =" j_0063.gif "/>

It only increases the difficulty.

Analysis: ① repeated, string; ② ordered output;

Package COM. java. interview. algorithm; import Java. util. arrays; import Java. util. hashset; import Java. util. set;/*** name: string_output2.java ** Date: 20:30:43 ** function: Output special string ** copyright: open ** @ author wangxin **/public class string_output2 {public static void main (string [] ARGs) {string random_string = random_string (4); system. out. println ("randomly generated numeric string: \" "+ random _ String + "\" "); system. out. println ("output->"); output_string (random_string);} Private Static void output_string (string Str) {int Len = Str. length (); For (INT I = 0; I <Len; I ++) {int COUNT = 0; For (Int J = 0; j <= I; j ++) {system. out. print ("\" "+ Str. substring (J, len-I + Count ++) + "\" "); if (I! = (LEN-1) | j! = I) {system. out. print (",") ;}}} Private Static string random_string (INT Len) {If (LEN <= 0 | Len> = 10) {system. out. println ("input error, between 1-9"); Return "fasle";} int COUNT = 0; set <integer> set = new hashset <integer> (LEN ); do {set. add (INT) (math. random () * 9); Count = set. size () ;}while (count <Len); int temp [] = new int [Len]; Count = 0; For (integer INTEGER: Set) {temp [count ++] = integer;} arrays. sort (temp, 0, Len); string result = ""; for (int I: temp) {result + = I;} return result ;}}
Output example: the randomly generated numeric string is: "1678" output-> "1678", "167", "678", "16", "67", "78 ", "1", "6", "7", "8"

Not optimized. Welcome to the discussion after optimization!

This article from the "Long Way" blog, please be sure to keep this source http://wangxin88.blog.51cto.com/3228434/1537598

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.