C# 返回C中小于n的所有2次幂#

C# 返回C中小于n的所有2次幂#,c#,arrays,C#,Arrays,对于给定的数字n,我需要返回小于n的所有2的幂,作为字符串,元素以“-”分隔。如果n1-2-4-8-16 n=8=>1-2-4 我是初学者,所以任何帮助都将不胜感激!:) 编辑:这不起作用 using System; class N { static int[] powerof2(int n) { int[] array = new int[n]; if (n < 2) return new int[0];

对于给定的数字n,我需要返回小于n的所有2的幂,作为字符串,元素以“-”分隔。如果n<2,则需要返回一个空字符串

例如:

n=20=>1-2-4-8-16

n=8=>1-2-4

我是初学者,所以任何帮助都将不胜感激!:)

编辑:这不起作用

using System; 

class N 
{
   static int[] powerof2(int n) 
   { 
       int[] array = new int[n];
       if (n < 2) 
          return new int[0];
        
       for (int i = 0; i < 8 * sizeof(uint); i++) 
       { 
          int curr = 1 << i; 

          if (curr > n) 
           break; 
  
          array[i] = curr;
    } 
  
    return array; 
    
    public override string ToString()
    {
        for (int i = 0; i < array.length; i++)
            return (array[i] + "-");
    }
} 

static public void Main () 
{ 
    int n = 10; 
    Console.WriteLine(powerof2(n).ToString()); 
} 
} 
使用系统;
N类
{
静态int[]幂2(int n)
{ 
int[]数组=新的int[n];
if(n<2)
返回新整数[0];
对于(int i=0;i<8*sizeof(uint);i++)
{ 
整数电流=1 n)
打破
数组[i]=curr;
} 
返回数组;
公共重写字符串ToString()
{
for(int i=0;i
课程计划
{
静态字符串幂2(整数n)
{
var str=“1”;
if(n<2)
返回“”;
其他的
{
var i=1;

while(Math.Pow(i,2)您需要使用以下规则运行for循环

for (int i = 1; i < n; i *= 2)
for(int i=1;i
整体解决方案

class Program
    {
        static void powerof2(int n)
        {
            if (n < 2)
                Console.WriteLine(string.Empty);

            for (int i = 1; i < n; i *= 2)
            {
                if (i > 1)
                    Console.Write("-");
                Console.Write(i);   
            }
        }

        static void Main(string[] args)
        {
            powerof2(20);
        }
类程序
{
静态无效功率2(整数n)
{
if(n<2)
Console.WriteLine(string.Empty);
对于(int i=1;i1)
控制台。写(“-”;
控制台。写入(i);
}
}
静态void Main(字符串[]参数)
{
幂2(20);
}

我想这就是你想要的:

class Program
{
    public static string Pow2LessThan(ulong n)
    {
        if (n < 2)
            return "";

        // start with 2^0
        string res = "1";

        // try further
        int p = 1;
        ulong cnum = 2;

        while (cnum < n)
        {
            res += "-" + cnum.ToString();
            ++p;
            cnum = (ulong)(1 << p);
        }

        return res;
    }

    static void Main(string[] args)
    {
        ulong n = 20;
        Console.WriteLine(Pow2LessThan(n));
        Console.ReadLine();
    }
}
类程序
{
公共静态字符串功率小于2(ulong n)
{
if(n<2)
返回“”;
//从2^0开始
字符串res=“1”;
//进一步尝试
int p=1;
ulong cnum=2;
而(cnum
使用系统;
使用System.Collections.Generic;
公共课程
{
公共静态IEnumerable GetPowersOf2(int maxN)
{
对于(int p=1;p
它不起作用的原因是:您从未以任何方式访问类
N
。调用应该是

Console.WriteLine(N.powerof2(n).ToString());
                  ^^ 
修改后,您将收到通知,方法
powerof2()
由于其保护级别而无法访问。您需要将其至少设置为内部,如下所示:

internal static int[] powerof2(int n) 
接下来,请注意,该方法缺少一个
}

    return array; 
}
修复此问题后,编译器将告诉您无法访问
ToString()
中的
array
,因为
array
的范围仅限于
powerof2()
。将数组设为类的字段,如

static  int[] array;
现在,编译器在
ToString()
中抱怨
array.length
。通过大写
length
来解决这个问题

错误号6:
ToString()
将在循环的第一次迭代中返回。如果
array.Length
为0,则它不会返回任何内容。该函数的外观应该有点像这样:

public override string ToString()
{
    string result = "";
    for (int i = 0; i < array.Length; i++)
        result += array[i] + "-";
    return result;
}
修复了7个问题后,输出现在是
1-2-4-8-0-0-0-0-0-
,因此仍有一些问题需要修复。也许你会有一点感觉,为什么每个人都提出了一个完全不同的解决方案

还需要解决什么问题:

  • 当然,输出仍然不正确
  • 如果有人输入4000000000作为数字,您肯定不希望在阵列中分配4GB的RAM
  • 为什么要分配一个数组而不立即构造字符串呢
  • 为什么
    8*sizeof(uint)
    ?您的移位频率不能超过
    sizeof(uint)

好的,但问题是什么?旁注:“如果n<2,它需要返回一个空字符串。”--2^0=1是2的幂,小于2。因此您可能需要重新考虑该规则。@stickybit这不是我的规则,任务如下that@ThomasWeller抱歉,我编辑了它。这不起作用
int n2=1;var result=new StringBuilder(n2.ToString());虽然((n2=n2)我会选择无限循环和
TakeWhile
,这更一般,但确实也很好…@阿列克谢列文科夫同意,我只是不想在等式中加上Linq,因为OP似乎是一个初学者,很明显OP不会理解
收益率,而是2(50)现在输出1-2-4-8-16-32-64-128。真的,在发布答案之前,使用LinqPad检查您的答案很好,您的答案非常精确,但这不是很必要
static  int[] array;
public override string ToString()
{
    string result = "";
    for (int i = 0; i < array.Length; i++)
        result += array[i] + "-";
    return result;
}
static public void Main () 
{ 
    var n = new N();
    n.powerof2(10); 
    Console.WriteLine(n.ToString()); 
}