会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
过客
记录工作中的点点滴滴
博客园
首页
新随笔
联系
订阅
管理
C#算法---求2进制的问题
Code
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
6
namespace
NumnerOutput
7
{
8
class
Program
9
{
10
static
void
Main(
string
[] args)
11
{
12
List
<
double
>
d
=
Get2PowArray(
10
);
13
foreach
(
double
dd
in
d)
14
{
15
Console.WriteLine(dd.ToString());
16
}
17
Console.ReadKey();
18
}
19
public
static
double
pow2(
double
n)
20
{
21
double
i
=
0
;
22
23
while
(Math.Pow(
2
, i)
<
n)
24
{
25
i
++
;
26
}
27
if
(Math.Pow(
2
, i)
>
n)
28
return
--
i;
29
else
30
return
i;
31
}
32
public
static
List
<
double
>
Get2PowArray(
double
n)
33
{
34
double
temp
=
n;
35
List
<
double
>
arrayList
=
new
List
<
double
>
();
36
while
(temp
>=
1
)
37
{
38
double
maxIndex
=
pow2(temp);
39
temp
-=
Math.Pow(
2
,maxIndex);
40
arrayList.Add(Math.Pow(
2
,maxIndex));
41
}
42
return
arrayList;
43
}
44
45
}
46
}
47
posted @
2009-08-11 14:15
Thinking.N
阅读(
199
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告