使用C ++中的任何嵌套循环显示特定数字模式

时间:2019-05-09 10:12:09

标签: c++

我是C ++的初学者。我有一个作业问题,其中我必须使用任何嵌套循环结构来打印此模式。但是我无法完成它。我只能打印程序的第一行,如输出所示。请向我建议正确的方向。

#include <iostream>

int main()
{
    int num1 = 1;
    int num2 = 2;
    int num3 = 3;
    int num4 = 4;
    int num5 = 5;
    for (int i = 0; i < 4; i++) {
        num1 = num1*num1;
        num2 = num2*1;
        num3 = num3*1;
        num4 = num4*1;
        num5 = num5*1;
        std::cout << num1 << "*#" << num2 << "*#" << num3 << "*#" << num4 << "*#" << num5 << "*#" << std::endl;
    }
    return 0;
}

实际输出:

1*#2*#3*#4*#5*#
1*#2*#3*#4*#5*#
1*#2*#3*#4*#5*#
1*#2*#3*#4*#5*#

预期输出:

1*#2*#3*#4*#5*#
1*#4*#9*#16*#25*#
1*#8*#27*#64*#125*#
1*#16*#81*#256*#625*#

1 个答案:

答案 0 :(得分:2)

实际上,您不必修改用于打印所需图案的变量。

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    const int num1 = 1, num2 = 2, num3 = 3, num4 = 4, num5 = 5; // "const" because I won't modify their values.
    for (int i = 1; i <= 4; i++) { // Pay attention: equal result of "for (int i = 1; i < 5; i++)"
        // In each iteration there will be a pow between numX and i.
        // First iteration: pow(num1, 1) => 1, pow(num2, 1) => 2, ...
        // Second iteration: pow(num1, 2) => 2, pow(num2, 2) => 4, ...
        // Third iteration: pow(num1, 3) => 3, pow(num2, 3) => 8, ...
        cout << pow(num1, i) << "*#" << pow(num2, i) << "*#" << pow(num3, i) << "*#" << pow(num4, i) << "*#" << pow(num5, i) << "*#" << endl;
    }
    return 0;
}

现在,由于num1,num2,...的值仅在程序中使用一次,并且由于它是非常小的程序,因此您可以跳过其声明部分并直接使用其值(“硬编码”): / p>

for (int i = 1; i <= 4; i++) {
    cout << pow(1, i) << "*#" << pow(2, i) << "*#" << pow(3, i) << "*#" << pow(4, i) << "*#" << pow(5, i) << "*#" << endl;
}

请注意,对于普通程序,绝对不建议使用“硬编码”,它可能会极大地影响您以后对该代码的工作,但这是一个选择。


编辑:

如L.F所述,在整数/单精度变量(int,long int,unsigned,char等)的情况下,std :: pow方法的主要缺点。在这种情况下,使用num * num代替pow(num, 2)num * num * num代替pow(num, 3)总是可以获得更好的性能。

要使代码运行更快,应使用自功率计算,如下所示:

int my_pow(int num, int n) {
    int res = 1;
    for (size_t i = 1; i <= n; i++) {
        res *= num;
    }
    return res;
}

以下代码检查my_powstd::pow之间的时间差(gcc,ubuntu 16.04):

for (int i = 1; i <= 4; i++) {
    cout << "Current i: " << i << endl;
    auto start = chrono::high_resolution_clock::now(); // Start clock (take the current time - when the selected code is about to start)
    pow(1, i); pow(2, i); pow(3, i); pow(4, i); pow(5, i);
    auto stop = chrono::high_resolution_clock::now(); // Stop clock (take the current time - when the selected code finished)
    cout << "std::pow [nanoseconds]: " << std::chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << endl; // Print clock results

    start = chrono::high_resolution_clock::now(); // Start clock (take the current time - when the selected code is about to start)
    my_pow(1, i); my_pow(2, i); my_pow(3, i); my_pow(4, i); my_pow(5, i);
    stop = chrono::high_resolution_clock::now(); // Stop clock (take the current time - when the selected code finished)
    cout << "my_pow [nanoseconds]: " << std::chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << endl; // Print clock results

    cout << endl;
}

----Output:

Current i: 1
std::pow [nanoseconds]: 19375
my_pow [nanoseconds]: 99

Current i: 2
std::pow [nanoseconds]: 14416
my_pow [nanoseconds]: 105

Current i: 3
std::pow [nanoseconds]: 3581
my_pow [nanoseconds]: 125

Current i: 4
std::pow [nanoseconds]: 614
my_pow [nanoseconds]: 134