打印重复数字模式

Print repeating digit pattern

本文关键字:模式 数字 打印      更新时间:2023-10-16

如何显示连续数字的重复模式?

这就是我希望实现的目标:

1
12
...
123456789
1234567890
12345678901

这是我的代码:

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int i,j,rows;
    cout << "Enter the number of rows: ";
    cin >> rows;
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           cout << j << " ";  
        }
        cout << endl;
    }
    getch();
    return 0;
}

这不会重复字符。如何让它在 9 后重复?

使用模运算符。

std::cout << (j % 10) << ' ';