Arrangement and combination

Source: Internet
Author: User

5 and 1 cannot be in the middle, and 1 must be in the first.
Public class zuhe {

Public static void main (string [] ARGs ){
String S = "12345 ";
Char [] C = S. tochararray ();
New zuhe (). zuhe (C, C. length, 0 );
System. Out. println ("test:" + KK );
}
Static int KK = 0;
Private void zuhe (char [] array, int N, int K ){
If (n = k ){
If (array [2]! = '5' & array [2]! = '1' & array [0] = '1 '){
String STR = new string (array );
System. Out. println (STR );
++ KK;
}
} Else {
For (INT I = K; I <n; I ++ ){
Swap (array, K, I );
Zuhe (array, n, k + 1 );
Swap (array, I, K );
}
}
}

Private void swap (char [] A, int X, int y ){
Char temp = A [x];
A [x] = A [y];
A [y] = temp;
}
}

------------------------------------------------------

Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;

Public class weave {
Private bufferedreader in;
Private inputstreamreader is;
Private char [] chars;
Private int charlen;
Int min;
Int Max;
Int COUNT = 0;

Public weave (){
Open ();
String STR = getstrinput ("Enter the characters to be combined :");
Chars = Str. tochararray ();
Charlen = chars. length;
Min = getintinput ("Enter the minimum length of the combination :");
Max = getintinput ("Enter the maximum length of the combination :");
Close ();
}

Public static void main (string [] ARGs ){

Weave weave = new weave ();
Long begin = system. currenttimemillis ();
Weave. Combination ();
Weave. Loger ("execution time:" + (system. currenttimemillis ()-begin) + "millisecond ");
}

Public void combination (){
Count = 0;
If (min> MAX ){
Int T = max;
Max = min;
Min = T;
}
For (INT I = min; I <= max; I ++ ){
Combination (New stringbuffer (""), I );
}
}

Public void combination (stringbuffer STR, int length ){

If (length <1 ){
Loger ("length less than 1 ");
}

If (length = 1 ){
For (INT I = 0; I <charlen; I ++ ){
Stringbuffer result = new stringbuffer (STR );
Result. append (chars [I]);
// Perform some operations based on the combined results
Dosome (result );
}
}

If (length> 1 ){
For (INT I = 0; I <charlen; I ++ ){
Stringbuffer temp = new stringbuffer (STR );
Combination (temp. append (chars [I]), length-1 );
}
}
}

Public void dosome (stringbuffer Str ){
Count ++;
Loger (count + "." + Str );
}

Public int getintinput (string info ){
Int temp = 0;
Loger ("/N" + info );
Try {
Temp = integer. parseint (in. Readline ());
}
Catch (exception e ){
Loger ("error !! Enter ") again ");
Return getintinput (Info );
}
If (temp <= 0 ){
Loger ("/n Error !! Must be greater than 0! ");
Return getintinput ("new input :");
}

Return temp;
}

Public String getstrinput (string info ){
Loger ("/N" + info );
String temp = "";
Try {
Temp = in. Readline ();
} Catch (ioexception e ){
Loger (E );
}
If (null = temp | "". Equals (temp )){
Loger ("/n Error !! You cannot enter an empty string! ");
Return getstrinput ("new input :");
}
Return temp;
}

/**
108. * Feedback
109. * @ Param o
110 .*/
Public void Loger (Object O ){
System. Out. println (O. tostring ());
}

Public void close (){
Try {
Is. Close ();
In. Close ();
} Catch (ioexception e ){
Loger (E );
}

}

Public void open (){
Is = new inputstreamreader (system. In );
In = new bufferedreader (is );
}

}

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.