This is a Java interview question, which is implemented in C. The specific question is as follows: given 6 numbers 1, 2, 2, 3, 4, 5, we need to find the six digits that all the six digits are combined into and meet
(1) 3 and 5 cannot be connected
(2) 4 cannot be in the third place
Give your Algorithm .
I did this.
Using System;
Using System. IO;
Using System. collections;
Using System. Collections. Generic;
Using System. text;
Namespace Leleapplication1
{
Class Program
{
Static Void Main ( String [] ARGs)
{
// Char [] A = {'1', '2', '2', '3', '4', '5 '};
Char [] = { ' 1 ' , ' 2 ' , ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' } ;
Int I = 0 ;
Ilist result = Program. pailie ();
Ilist result2 = New Arraylist ();
// Streamwriter Tw = new streamwriter ("C :\\ out.txt ");
// Console. setout (TW );
Foreach ( String Str In Result)
{
If (Str. Contains ( " 35 " ) | Str. Contains ( " 53 " ) | Str. indexof ( ' 4 ' ) = 3 )
{
I++;
}
Else
Result2.add (STR );
// Console. writeline (STR );
}
Foreach ( String Str2 In Result2)
{
Console. writeline (str2 );
}
// Tw. writeline ("I =" + I );
// Tw. writeline ("Result: --" + result. Count );
// Tw. writeline ("Total: --" + result2.count );
Console. writeline ( " I = " + I );
Console. writeline ( " Result :-- " + Result. Count );
Console. writeline ( " Total :-- " + Result2.count );
}
Public Static Ilist pailie ( Char [])
{
Ilist resultstr = New Arraylist ();
// Resultstr. Add (new string ());
Int N = A. length;
If (N = 2 )
{
Resultstr. Add ( New String ());
Char Temp = A [ 0 ];
A [ 0 ] = A [ 1 ];
A [ 1 ] = Temp;
Resultstr. Add ( New String ());
}
If (N > 2 )
{
For ( Int I = 0 ; I < N; I ++ )
{
String B = New String ();
B = B. Remove (I, 1 );
Char [] C = B. tochararray ();
Ilist tempstr = Pailie (C );
Foreach ( String S In Tempstr)
{
StringXx=A [I]. tostring ()+S;
Resultstr. Add (XX );
}
}
}
Return Resultstr;
}
}
}
Remove some comments and export them to files.
You can give better algorithms and give more valuable comments!