简单有趣#159:中东排列【难度:4级】:
答案1:
namespace myjinxin
{
using System;
using System. Linq;
public class Kata
{
public string MiddlePermutation ( string s)
{
if ( s. Length % 2 == 1 )
{
var firstChar = s. OrderBy ( ch = > ch) . ElementAt ( s. Length / 2 ) ;
return firstChar + MiddlePermutation ( string . Concat ( s. Where ( ch = > ch != firstChar) ) ) ;
}
else
{
var ordered = s. OrderByDescending ( ch = > ch) ;
var firstChar = ordered. ElementAt ( s. Length / 2 ) ;
return firstChar + string . Concat ( ordered. Where ( ch = > ch != firstChar) ) ;
}
}
}
}
答案2:
namespace myjinxin
{
using System;
using System. Linq;
public class Kata
{
public string MiddlePermutation ( string s) {
if ( s. Length % 2 == 1 )
{
var firstChar = s. OrderBy ( ch = > ch) . ElementAt ( s. Length / 2 ) ;
return firstChar + MiddlePermutation ( string . Concat ( s. Where ( ch = > ch != firstChar) ) ) ;
}
else
{
var ordered = s. OrderByDescending ( ch = > ch) ;
var firstChar = ordered. ElementAt ( s. Length / 2 ) ;
return firstChar + string . Concat ( ordered. Where ( ch = > ch != firstChar) ) ;
}
}
}
}
答案3:
namespace myjinxin
{
using System;
using System. Linq;
public class Kata
{
public string MiddlePermutation ( string s)
{
s = new string ( s. OrderByDescending ( c = > c) . ToArray ( ) ) ;
int midLength = 1 + s. Length % 2 ;
return s. Substring ( s. Length / 2 ,