namespace myjinxin
{using System;using System.Linq;publicclassKata{publicstringTestit(string s,int n){var index =-1;return Convert.ToString(n,2).ToCharArray().Aggregate("",(a, b)=> a +(((++index < s.Length)&&(b =='1'))? s[index].ToString():""));}}}
答案3:
namespace myjinxin {using System;using System.Linq;publicclassKata{publicstringTestit(string s,int n ){if(string.IsNullOrEmpty( s )){returnstring.Empty;}var bin = Convert.ToString( n,2).Select( d => d -'0').ToArray();returnstring.Concat( bin.Select(( a, i )=> a >0&& i < s.Length ? Convert.ToString( s [ i ]):""));}}}
答案4:
using System;using System.Linq;namespace myjinxin
{publicclassKata{publicstringTestit(string s,int n){var b = Convert.ToString(n,2);
b = b.Substring(0, Math.Min(b.Length, s.Length)).Trim('0');returnstring.Concat(b.Select((c, i)=>new{ c, i }).Where(a => a.c =='1').Select(a => s[a.i]));}}}
答案5:
using System.Linq;namespace myjinxin
{using System;publicclassKata{publicstringTestit(string s,int n){var showBits = Convert.ToString(n,2).PadRight(s.Length,'0');returnstring.Concat(s.Select((c, i)=> showBits[i]=='1'? c +"":""));}}}
答案6:
namespace myjinxin
{using System;publicclassKata{publicstringTestit(string s,int n){string bits = Convert.ToString(n,2);string result ="";for(int i =0; i < bits.Length; i++){if(bits[i]=='1'&& s.Length > i)
result += s[i];}return result;}}}
答案7:
namespace myjinxin
{using System;publicclassKata{publicstringTestit(string s,int n){var b = Convert.ToString(n,2);var answer =string.Empty;for(int i =0; i < b.Length; i++){if(b[i]=='1'){if(s.Length <= i)return answer;
answer += s[i%s.Length];}}return answer;}}}
答案8:
namespace myjinxin
{using System;using System.Linq;publicclassKata{publicstringTestit(string s,int n){var bits = Convert.ToString(n,2);string result ="";for(int i =0; i < bits.Length; i++){if(bits[i]=='1'&& s.Length > i) result += s[i];}return result;}}}
思考与测试:保留和丢弃II【难度:1级】:答案1:namespace myjinxin{ using System; using System.Linq; public class Kata { public string Testit(string s, int n) { var showBi...