求一算法【在线等】

ajq1989 2010-06-11 02:34:59
现有一有规律数组
1 2 4 8 16 32 64 128

我要让输入一个数字 如 7 就可知道由 1 2 4 三个相加成的。
不考虑如 16 由2个8相加 16就是16
...全文
186 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
jbz001 2010-06-11
  • 打赏
  • 举报
回复
这个太简单了,就是把一个数字换算成二进制而已。二进制每一位都代表2的n次方~!
宇峰科技 2010-06-11
  • 打赏
  • 举报
回复
学习了。。。
leafold 2010-06-11
  • 打赏
  • 举报
回复
       const uint Baseint = ((uint)1) << 31;

static string Bin(uint num)
{
var valueList = new List<string>();
uint b = Baseint;
while (num > 0)
{
if (num >= b)
{
valueList.Add(b.ToString());
num = num - b;
}
b >>= 1;
if (b <= 0) break;
}
return string.Join(" + ", valueList.ToArray());
}
  • 打赏
  • 举报
回复
可以用“与”运算,判断每个规律数是否属于要求的结果内,代码如下,建了一个文本框用来输入要计算的数据,通过MessageBox返回结果,调试OK

private const long n_1 = 0x0001;
private const long n_2 = 0x0002;
private const long n_4 = 0x0004;
private const long n_8 = 0x0008;
private const long n_16 = 0x0010;
private const long n_32 = 0x0020;
private const long n_64 = 0x0040;
private const long n_128= 0x0080;

private void button1_Click(object sender, System.EventArgs e)
{
long result = Convert.ToInt64(tbResult.Text);
bool e1 = false;
bool e2 = false;
bool e4 = false;
bool e8 = false;
bool e16 = false;
bool e32 = false;
bool e64 = false;
bool e128 = false;

e1= ((result & n_1) != 0);
e2= ((result & n_2) != 0);
e4= ((result & n_4) != 0);
e8= ((result & n_8) != 0);
e16= ((result & n_16) != 0);
e32= ((result & n_32) != 0);
e64= ((result & n_64) != 0);
e128= ((result & n_128) != 0);

MessageBox.Show(result + "= 0 " + (e1 ? "+1":string.Empty) + (e2 ? "+2":string.Empty) + (e4 ? "+4":string.Empty)
+ (e8 ? "+8":string.Empty) + (e16 ? "+16":string.Empty) + (e32 ? "+32":string.Empty) + (e64 ? "+64":string.Empty)
+ (e128 ? "+128":string.Empty));
}
Xiaoyuan245437 2010-06-11
  • 打赏
  • 举报
回复
怎么感觉你们的超复杂????
threenewbee 2010-06-11
  • 打赏
  • 举报
回复
上面的代码n=0要单独考虑,代码自己完善了。
Xiaoyuan245437 2010-06-11
  • 打赏
  • 举报
回复
好象我的方法是可以的呢`
wuyi8808 2010-06-11
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
foreach (uint x in new uint[]{7, 1234567890, uint.MaxValue})
Console.WriteLine("{0} = {1}", x, Bin(x));
}

static string Bin(uint x)
{
uint[] mask =
{
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00000100, 0x00000200, 0x00000400, 0x00000800,
0x00001000, 0x00002000, 0x00004000, 0x00008000,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000, 0x00800000,
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000,
};
List<string> list = new List<string>();
foreach (uint m in mask)
{
if ((x & m) != 0)
list.Add(m.ToString());
}
return string.Join(" + ", list.ToArray());
}
}
threenewbee 2010-06-11
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n = 1;
while (n != 0)
{
n = Convert.ToInt32(Console.ReadLine());
ConvertNum(n);
}
}

public static void ConvertNum(int n)
{
Console.WriteLine("n = {0}", n);
Queue<int> s = new Queue<int>();
while (n > 0)
{
s.Enqueue(n % 2);
n /= 2;
}
bool has1 = false;
int i = 0;
while (s.Count > 0)
{
if (s.Dequeue() == 1)
{
has1 = true;
Console.WriteLine(Math.Pow(2, i));
}
i++;
}
if (!has1)
Console.WriteLine(Math.Pow(2, i));
Console.WriteLine();
}
}
}

23
n = 23
1
2
4
16

14
n = 14
2
4
8

7
n = 7
1
2
4

16
n = 16
16

24
n = 24
8
16

1
n = 1
1

66
n = 66
2
64

0
n = 0
1

Press any key to continue . . .

不过LZ要认识到这种问题和LZ的年龄是不相符的。
巴朗鱼 2010-06-11
  • 打赏
  • 举报
回复
1 2 4 8 16 32 64 相当于 2的0次方 2的1次方 2的2次方 2的3次方 2的4次方 2的5次方
然后取出一个数就对应进行进行二进制分解
像7 = 2的0次方 + 2的1次方 + 2的2次方
其实说白了,就是二进制转换。。。
threenewbee 2010-06-11
  • 打赏
  • 举报
回复
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ConvertNum(23);
}

public static void ConvertNum(int n)
{
Queue<int> s = new Queue<int>();
while (n > 0)
{
s.Enqueue(n % 2);
n /= 2;
}
bool has1 = false;
int i = 0;
while (s.Count > 0)
{
if (s.Dequeue() == 1)
{
has1 = true;
Console.WriteLine(Math.Pow(2, i));
}
i++;
}
if (!has1)
Console.WriteLine(Math.Pow(2, i));
}
}
}
lvpaizen 2010-06-11
  • 打赏
  • 举报
回复
这个牵扯到无穷大位数,要进行二进制转换! 比较麻烦!
Xiaoyuan245437 2010-06-11
  • 打赏
  • 举报
回复

string x = Convert.ToString(long.Parse(Console.ReadLine()), 2);
int b =1;
for (int i=0; i < x.Length; i++)
{

if (x.Substring(x.Length-i-1,1) == "1")
{
Console.WriteLine("选中"+b);
}
b=b*2;
}
Console.ReadLine();

closewbq 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 ajq1989 的回复:]
现有一有规律数组
1 2 4 8 16 32 64 128

我要让输入一个数字 如 7 就可知道由 1 2 4 三个相加成的。
不考虑如 16 由2个8相加 16就是16
[/Quote]
判断输入数字小于数组中某个数字的位置。输出这个位置前的数字
danyaozhuanjia 2010-06-11
  • 打赏
  • 举报
回复
规律2的N次方,求一个质数为那几个合数相加。 上面的算法效率是最高的,循环是最常规的。
兔子-顾问 2010-06-11
  • 打赏
  • 举报
回复
对于这种不知道有什么实际意义的题目,只能路过,无法有兴趣实现…
zhyale 2010-06-11
  • 打赏
  • 举报
回复
这个不难。
2的0次方=1
2的1次方=2
...

其实就是转换为二进制,本例中 7的二进制表示为 0111 ,从右往左看,第一位为1,则选中你CheckListBox的第一项,依次类似,如果为1就选中,如果为0,就不选中。

其实数字在计算机中本来就是二进制存储的,所以你也不需要转换,可以操作如下:
假设你的最大数值为Int32类型 ,变量为IntValue
首先直接与 0x00000001 进行按位运算,Result= IntValue & 0x00000001 ,Result为1则选中第一项;
IntValue向右移位:IntValue=IntValue>>1;
IntValue继续与 0x00000001 进行按位运算,Result= IntValue & 0x00000001 ,Result为1则选中第二项;
依次类推。



liuwei19860906 2010-06-11
  • 打赏
  • 举报
回复
晕啊 递归啊 !~~
ajq1989 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 caozhy 的回复:]
这就是转换成二进制的算法。循环取余。

参考小学电脑课本第一章。
[/Quote]

麻烦这位大虾帮帮忙。
小弟小学电脑课本是教打五笔。
Xiaoyuan245437 2010-06-11
  • 打赏
  • 举报
回复
汗 做是可以做 ` 不过很麻烦`
加载更多回复(10)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧