创建列和行;询问用户想要输出的行数,然后询问显示

creating columns and rows; asking for the number of lines the user wants to output then the display

本文关键字:显示 然后 输出 用户 创建      更新时间:2023-10-16
//libraries
#include <iostream>
//global constants
//no functioning prototypes
using namespace std;
int main(){
    //Define variables
    int n; // Number of rows
    int i; // Row count in for loop
    int k; // Output for loop
    // Have user input n
    cout << "Enter number of rows: ";
    cin >> n;
    // Complete for loop
    for (i = 1; i <= n + 1; i++){
    for (k = 1; k < i; k++){
    cout << k%10;
}
  cout << endl;
}
    system("PAUSE");
    return EXIT_SUCCESS;
}

我正在尝试创建一个与此类似的代码,除了而不是放

1
12
123
1234
12345

我需要它看起来像

1
 2
  3
   4
    5

其中结束数字是列数是输入的数字和空格而不是以前的数字

您只需要替换此行:

cout << k%10;

对于此行:

if (k<i-1) cout << ' '; else cout << k%10;

如果我们在当前行的最后一个位置,这将打印数字,如果我们不在,则会打印一个空格。

#include "stdafx.h"
#include"iostream"
#include"conio.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a=1;
int n;
cout<<"enter the lenght of the numbers";
cin>>n;
for (int i=1;i<=n;i++)
{
    {for (int j=1;j<=n;j++)
        if (j==a)
            cout<<j;
        else cout<<" ";
    }
    cout<<endl;
    a=a+1;
}
_getch();   
}
相关文章: