JavaScript is required to use Bungie.net

雜七雜八

瀏覽大量隨機討論串。
由Good News編輯: 9/14/2013 6:18:32 AM
7

Programmers of the Flood, I need some help (C++)

I just started learning how to program, using c++, 3 weeks ago, and I have one class a week, so forgive me if this is so basic that asking for help on it makes you want to kill me. I need to write a program using some nested loops to create this: * *** ***** ******* ***** *** * Two problems I have: 1. I don't know how to make the 1-3-5-7-5-3-1. My code goes 1-2-3-4-5-6-7 2. I have no idea how to make it descend once it reached the 7 asterisks. For my loops, I'm trying to use two while loops. One for the ascending part of the triangle, and one for the descending. Both of them have for loops nested inside. [spoiler]#include <iostream> using namespace std; int main() { int counter=1; int x = 0; while(counter <= 7) { for (x=0; x < counter; x++) { cout <<"*"; } cout << endl; counter++; } //descending part while (counter >= 7) { for (x=0; x > counter; x--) { cout<<"*"; } cout << endl; counter--; } return 0; } [/spoiler] So as I said, I don't know how to make the descending part of the triangle. My code is indicative enough of that. Anyone have any advice? For both or either problems?

文章張貼語言:

 

以禮待人。發佈文章前請花點時間查看我們的行為準則 取消 編輯 創立火力戰隊 文章

檢視完整主題
  • [quote] #include <iostream> #include <iomanip> using namespace std; int main(){ cout.fill('*'); for(int i=1,j=2; i>0; i+=j,j*=((i%7)==0)?-1:1){ cout << setw(i + 1) << '\n'; } return 0; }[/quote]http://ideone.com/yrVrle You can replace the setw call with another loop to output character-by-character if you require the nesting.

    文章張貼語言:

     

    以禮待人。發佈文章前請花點時間查看我們的行為準則 取消 編輯 創立火力戰隊 文章

    1 回覆
    你無權檢閱此內容
    ;
    preload icon
    preload icon
    preload icon