【问题标题】:Is there an alternative on nested loop for displaying possible combination in range of n?嵌套循环是否有替代方法来显示 n 范围内的可能组合?
【发布时间】:2021-11-18 22:03:25
【问题描述】:

我创建了一些代码,每当您将数字输入pinCombo(x)(例如pinCombo(3))时,输出将是:

000
001
002

……直到达到 999。

所以,pinCombo(4) 的输出将是:

0000
0001
....
....
9999

这是我的代码:

#include <iostream>

using namespace std;

void pinCombo(int x)
{
    int a,b,c,d,e,f,g,h,i,j;
    
    if(x>=1)
    for (a = 0;a<10;a++)
    {
        if(x>=2)
        for (b = 0;b<10;b++)
        {
            if(x>=3)
            for (c = 0;c<10;c++)
            {
                if(x>=4)
                for (d = 0;d<10;d++)
                {
                    if(x>=5)
                    for (e = 0;e<10;e++)
                    {
                        if(x>=6)
                        for (f = 0;f<10;f++)
                        {
                            if(x>=7)
                            for (g = 0;g<10;g++)
                            {
                                if(x>=8)
                                for (h = 0;h<10;h++)
                                {
                                    if(x>=9)
                                    for (i = 0;i<10;i++)
                                    {
                                        
                                        if(x>=10)
                                        for (j = 0;j<10;j++)
                                        {
                                            cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<endl;
                                        }if(x==9)cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<endl;
                                    }if(x==8)cout<<a<<b<<c<<d<<e<<f<<g<<h<<endl;
                                }if(x==7)cout<<a<<b<<c<<d<<e<<f<<g<<endl;
                            }if(x==6)cout<<a<<b<<c<<d<<e<<f<<endl;
                        }if(x==5)cout<<a<<b<<c<<d<<e<<endl;
                    }if(x==4)cout<<a<<b<<c<<d<<endl;
                }if(x==3)cout<<a<<b<<c<<endl;
            }if(x==2)cout<<a<<b<<endl;
        }if(x==1)cout<<a<<endl;
    }
    
}

using namespace std;

int main()
{
pinCombo(3);
return 0;
}

有没有办法在不使用嵌套循环或不使用许多变量的情况下创建这样的程序?

【问题讨论】:

  • 是的,使用一个变量和%(模数)
  • 有一些I/O manipulators 可用于使用单个循环执行您想要的操作。现在你只需要想出一种方法将3 转换为最大数999(或者可能是1000,即10 提高到3 的幂)。
  • 我想知道这个answer 是否有用
  • 您还可以使用具有单个循环的递归函数。从技术上讲,每个递归调用都会执行一个循环,这是一种嵌套形式,但在代码方面只会编写一个循环。

标签: c++ algorithm brute-force


【解决方案1】:

您可以使用% 运算符实现此目的:

#include <iostream>
#include <cmath>

void pinCombo(int x)
{
    int* digits = new int[x];
    int limit = std::pow(10, x);
    
    for (int n = 0; n < limit; n++) {
        int nn = n;
        
        for (int i = 0; i < x; i++) {
            digits[i] = nn % 10;
            nn /= 10;
        }
        
        for (int i = x-1; i >= 0; i--) {
            std::cout << digits[i];
        }
        
        std::cout << "\n";
    }

    delete[] digits;
}

int main()
{
    pinCombo(3);
}

这将输出:

000
001
002
...
999

编辑:您可以使用&lt;iomanip&gt; 设施实现同样的目的:

void pinCombo(int x)
{
    int limit = std::pow(10, x);
    for (int i = 0; i < limit; i++)
        std::cout << std::setw(x) << std::setfill('0') << i << std::endl;
}

旁注: 使用 using namespace std 被认为是一种不好的做法,因为它会使用 C++ 标准中包含的所有内容污染全局命名空间。使用std:: 前缀或using std::&lt;whataver_you_want&gt;(如果您更喜欢快捷方式)。

【讨论】:

  • 这不是有效的 C++
  • @HattedRooster 为什么?
  • 因为您使用的是 C++ 所没有的可变长度数组。
  • @HattedRooster 非常感谢您的评论!我已经编辑了我的答案。
【解决方案2】:

这是使用setwidthsetfill 的解决方案。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void pinCombo(int x){
    for (int i = 0; i < pow(10, x); i++){
        cout << setw(x) << setfill('0') << i << endl;
    }
}

int main(){
    pinCombo(3);
    return 0;
}

【讨论】:

  • 就个人而言,我会使用一个简单的循环来使用整数来计算十的幂。否则不错的答案。
  • 谢谢先生,这真的帮助我获得更多关于编程的知识,因为我是一个初学者
  • 在每次迭代中,您都在计算 pow(10, x)。让它脱离循环(不要指望你的编译器为你做这件事)。
猜你喜欢
  • 2022-12-31
  • 1970-01-01
  • 2016-11-08
  • 2012-09-11
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多