using System; using System.Collections.Generic; using System.Text;namespace Ctest { class Program { static void Main(string[] args) { long k = 0; long.TryParse(Console.ReadLine(), out k); if (k == 0) return; else { printfresult(k); } } static void printfresult(long x) { long i = 0; for (; ;i++ ) { if(x<Math.Pow(2,i))break; } i--; for (; i >= 0; i--) { int tmp=(int)Math.Pow(2,i); if (x > tmp) { Console.Write(tmp.ToString()+"+"); x -= tmp; } else if (x == tmp) { Console.Write(tmp.ToString()); break; } } Console.WriteLine(); Main(null); } } }
16 8 4 2 1
---------------
10= 1 0 1 0
17= 1 0 0 0 1
11= 1 0 1 117的二进制表示就是10001。一个32bit的整数有最多32个位置来存储客服组。
不过这种数据库数据存储方式也有不足的地方,一个是扩展局限,一个是不容易理解。
using System.Collections.Generic;
using System.Text;namespace Ctest
{
class Program
{
static void Main(string[] args)
{
long k = 0;
long.TryParse(Console.ReadLine(), out k);
if (k == 0) return;
else
{
printfresult(k);
}
} static void printfresult(long x)
{
long i = 0;
for (; ;i++ )
{
if(x<Math.Pow(2,i))break;
}
i--;
for (; i >= 0; i--)
{
int tmp=(int)Math.Pow(2,i);
if (x > tmp)
{
Console.Write(tmp.ToString()+"+");
x -= tmp;
}
else if (x == tmp)
{
Console.Write(tmp.ToString());
break;
}
}
Console.WriteLine();
Main(null);
}
}
}
然后x=x-匹配值 即 x=17-16=1,如此循环向下找