2013-10-15 63 views
-1

好的,我必須編寫一個C++程序來讀取一個數字,然後繼續寫每一個數字,直到讀取的次數與它的次數相同值。我完全不知道如何解釋這個或尋找什麼,所以我希望你明白我需要什麼,可以幫助我。編寫一個數字序列,如:1 22 333 4444 55555

基本上,如果我們cin >> 5,輸出應該是1 22 333 4444 55555。我有一種感覺,這是非常容易的,但現在沒有什麼讓我想起。我試着用2進行陳述,但我似乎無法做到。

這是我的嘗試:

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<<" "; 
    } 
} 
+10

有趣。讓我們來看看你的代碼,我們將幫助你完成它 –

+2

是的,應該可以用2 for循環。編輯你的問題,包括你的嘗試,我們將從那裏引導你 – Paddyd

+4

在量子計算機時代之前是不可能的。抱歉。 – dreamzor

回答

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<<" "; 
    } 
} 
+0

DUDE YOU ARE AWESOME – dreamzor

+0

好吧,我不應該在給他解決這個問題之前給這個代碼給OP,但不知何故我覺得可惜:P –

+0

我不'不知道如何在這裏使用代碼編輯器並粘貼我的代碼。我寫的代碼是55555 4444 333 22 1,而不是其他方式。 –

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; 
     } 


    } 

} 
0

是的,很容易。

  • 使用COUT提示輸入號碼
  • 將CIN讀取數
  • 你需要一個內部循環來打印數字的拷貝,後面加上一個空格
  • 你需要一個外環從1循環的數目,跟着以新行(行尾)

下面是答案,

#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; 
} 
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(); 
} 
+0

將詳細信息添加到您的答案中 – silwar

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); 
} 
相關問題