【问题标题】:creating columns and rows; asking for the number of lines the user wants to output then the display创建列和行;询问用户想要输出的行数然后显示
【发布时间】:2013-10-15 16:57:56
【问题描述】:
//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

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

【问题讨论】:

    标签: c++ rows multiple-columns


    【解决方案1】:

    你只需要替换这一行:

    cout << k%10;
    

    对于这一行:

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

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

    【讨论】:

      【解决方案2】:
      #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();   
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多