繁体   English   中英

循环 C++ 不同 output

[英]Looping C++ different output

如何创建 output 像:

1
1122
111222333
1111222233334444


#include <iostream>
using namespace std;

int main() {
  int a,b,c,d,n;
  cout<<"Masukkan akhir perulangan = ";
  cin>>n;
  cout<<"\nMenggunakan For\n";
    for(a=1; a<=n; a++)
    {
        for(c=1;c<=a;c++)
        {
            for(b=1;b<=a;b++)
            {
                cout<<" "<<b;
            }
        }
        cout<<endl;
    }
  return 0;
}'

但我的 output 是

1
1212
123123123
1234123412341234

您应该在内部循环中使用 output c而不是b

for(a=1; a<=n; a++)
{
    for(c=1;c<=a;c++)
    {
        for(b=1;b<=a;b++)
        {
            cout<<" "<<c;
        }
    }
    cout<<endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM