-1

好的,我必须编写一个 C++ 程序来读取一个数字,然后继续写入每个数字,直到我们读取的次数与其值相同的次数。我完全不知道如何解释或搜索什么,所以我希望你了解我需要什么并可以帮助我。

基本上,如果我们 cin >> 5,输出应该是1 22 333 4444 55555. 我有一种感觉,这非常容易,但现在我什么都没有想到。我尝试使用 2 for 语句,但似乎无法正确处理。

这是我的尝试:

int main () 
{ 
   int i,j,n;
   cout<<"n=";cin>>n;
   for (i=n;i>=1;i--) 
   { 
      for (j=1;j<=i;j++) 
      { 
         cout << i; 
      } 
      cout<<" ";
   } 
}
4

5 回答 5

3
#include<iostream>

int main()
{
  int a;
  std::cin>>a;
  for(int i=1;i<=a;i++)
  {
    for(int j=0;j<i;j++)
      std::cout<<i;
    std::cout<<"  ";
  }
}
于 2013-10-15T15:35:32.130 回答
2
#include <iostream>


int main()
{
    int n;
    cout << "Please enter a number";
    cin >> n;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
        cout<<i;
        }


    }

}
于 2013-10-15T15:36:24.540 回答
0
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,n=5;
clrscr();  
for(i=0;i<n;i++)
  {
    for(j=1;j<=i;j++)
      {
      cout<<i;
      }
    cout<<endl;
  }
getch();
}
于 2014-02-05T12:41:40.220 回答
0

是的,很容易。

  • 使用 cout 提示输入数字
  • 使用 cin 读取数字
  • 你需要一个内部循环来打印数字的副本,后面跟一个空格
  • 你需要一个外部循环从 1 循环到数字,然后是换行符(endline)

这是答案,

#include <iostream>
using namespace std;
int main()
{
    int upto, ndx, cdx;
    cout<<"number=";
    cin>>upto;
    for(ndx=1;ndx<=upto;++ndx)
    {
        for(cdx=1;cdx<=ndx;++cdx)
            cout<<ndx;
        cout<<" ";
    }
    cout<<endl;
}
于 2013-10-15T18:04:36.840 回答
0
#include<iostream>
using namespace std;

int main ()
{
  int i,j;  //declaring two variables I,j.

  for (i=1; i<10; i++)  //loop over the variable i so it variates from 1 to 10.
  {
      for (int j = 0; j<i; j++)  //create an other loop for a variable j to loop over again and o/p value of i
      {
          cout <<i;  //writes the value of i directly from i and also due to the loop over j.
      }
      cout<<endl;  //manipulator to analyze the result easily.
  }
  return (0);
}
于 2014-08-02T15:06:54.613 回答