2^n 复杂度算法

发布于 2024-10-28 18:15:12 字数 165 浏览 2 评论 0原文

我需要实现并测试复杂度为 2^n 的算法。一段时间以来我一直在努力寻找一个。如果有什么方法可以通过实现来实现这一点——精确的复杂度为 2^n 这将是最佳的。如果有人知道某个位置,我可以找到一个示例,或者可以帮助我实现一个示例,那就太棒了:-)。基本操作可以是任何内容,但不能是像 i++ 这样的单个语句;会是最好的。

I need to implement and test an algorithm with a 2^n complexity. I have been trying to find one for a while. If there is any way I can acheive this by implementation -- with a exact complexity of 2^n that would be optimal. If anyone knows of a location I can find an example, or could help me implement one, that would be awesome :-). The basic operation can be anything, but a single statment like i++; would be best.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぽ尐不点ル 2024-11-04 18:15:12

生成包含 n 个元素的集合的所有子集。

额外。
生成 S = {a0, a1, ..., an-1} 的所有子集的最简单方法可能是在秩和子集的二进制表示之间进行转换。

取S = {a0, a1, a2}。

rank binary subset
0    000    {} 
1    001    {a0}
2    010    {a1}
3    011    {a0, a1}
4    100    {a2}
5    101    {a0, a2}
6    110    {a1, a2}
7    111    {a0, a1, a2}

因此,二进制中的 1 表示相应的元素在子集中。 0 表示该元素不在子集中。

但您还应该查找格雷码。

Generate all subsets of a set with n elements.

Added.
The simplest way of generating all the subsets of S = {a0, a1, ..., an-1} is probably to translate between the binary representation of the rank and the subset.

Take S = {a0, a1, a2}.

rank binary subset
0    000    {} 
1    001    {a0}
2    010    {a1}
3    011    {a0, a1}
4    100    {a2}
5    101    {a0, a2}
6    110    {a1, a2}
7    111    {a0, a1, a2}

So, a 1 in the binary means the corresponding element is in the subset. A 0 means the element is not in the subset.

But you should also lookup Gray code.

在梵高的星空下 2024-11-04 18:15:12

经典的递归斐波那契数计算是 O(2^n)。

unsigned Fib(unsigned n)
{
    if (n <= 1)
        return n;
    else
        return Fib(n - 1) + Fib(n - 2);
}

由于上面实际上是 theta(Phi^n),所以我添加了一个返回 2^n 的 theta(2^n) 算法。感谢耶利米·威尔科克。

unsigned TwoExp(unsigned n)
{
    if (n == 0)
        return 1;
    else
        return TwoExp(n - 1) + TwoExp(n - 1);
}

The classical recursive Fibonacci number calculation is O(2^n).

unsigned Fib(unsigned n)
{
    if (n <= 1)
        return n;
    else
        return Fib(n - 1) + Fib(n - 2);
}

Since the above is actually theta(Phi^n), I'm adding a theta(2^n) algorithm that return 2^n. Thanks to Jeremiah Willcock.

unsigned TwoExp(unsigned n)
{
    if (n == 0)
        return 1;
    else
        return TwoExp(n - 1) + TwoExp(n - 1);
}
终遇你 2024-11-04 18:15:12

Quantum Bogosort 的空间复杂度为 2^n。

Quantum Bogosort has 2^n space complexity.

昵称有卵用 2024-11-04 18:15:12

我花了很多时间重新思考这个问题,并想发布我想出的解决方案。所有的答案都有助于我提出这个解决方案,并且非常感谢每个回复的人。 :-) 我意识到算法实际上什么也没做。

它是用java编写的
似乎无法让选项卡工作
基本操作是i++;

public class TwoToTheN
{  
     private static int twoToTheN = 0;  
     private static int power = 3;

     public static void main(String[] args)   
     {  
         twoToTheN(power);  
         System.out.println(twoToTheN);  
     }  

     private static void twoToTheN(int n)  
     {  
         if(n == 0)   
         {  
             twoToTheN++;  
             return;  
         } 
         else if(n == 1)  
         {  
             twoToTheN++;  
             twoToTheN++;  
             return;  
         }  
         twoToTheN(n-1);  
         twoToTheN(n-1);  
     }
}  

I spent a great deal of time rethinking the problem and would like to post a solution I came up with. All of the answers contributed to my ability to come up with this solution, and am very thankful for everyone that reponded. :-) I realize that algorithm does practically nothing.

it is written in java
can't seem to get the tabs to work
basic operation is i++;

public class TwoToTheN
{  
     private static int twoToTheN = 0;  
     private static int power = 3;

     public static void main(String[] args)   
     {  
         twoToTheN(power);  
         System.out.println(twoToTheN);  
     }  

     private static void twoToTheN(int n)  
     {  
         if(n == 0)   
         {  
             twoToTheN++;  
             return;  
         } 
         else if(n == 1)  
         {  
             twoToTheN++;  
             twoToTheN++;  
             return;  
         }  
         twoToTheN(n-1);  
         twoToTheN(n-1);  
     }
}  
土豪我们做朋友吧 2024-11-04 18:15:12

其一:输出 2^(2^n) 的数字。

Here's one: output the digits of 2^(2^n).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文