這是我們程式老師上課時教迴圈時舉的一些例子,我是覺得題目本身不難,但是很適合拿來做迴圈的練習,訓練自己的反應速度,還有就是我現在寫VB的程度退化好多... 所以附上C++程式碼給大家看,自己研究吧~

題目就是最上面那張圖,要只使用迴圈來產生上面四種三角形,解答在下面

程式碼:

載點: Box.net4SharedDropboxGoogle

#include #include <math.h>

using namespace std;

int main() { int x=5; cout«“三角形一:"«endl; for(int i=1;i<=x;i++){ for(int y=1;y<=i;y++){ cout«”"; } cout«endl; } cout«endl; cout«“三角形二:"«endl; for(int i=x;i>=1;i–){ for(int y=1;y<=i;y++){ cout«”"; } cout«endl; } cout«endl; cout«“三角形三:"«endl; for(int i=1;i<=x;i++){ for(int y=x;y>i;y–){ cout«” “; } for(int y=1;y<=i;y++){ cout«”*"; }

    cout<<endl;
}
cout<<endl;
cout<<"三角形四:"<<endl;
for(int i=1;i<=x;i++){
    for(int y=1;y<i;y++){
        cout<<" ";
    }
    for(int y=x;y>=i;y--){
        cout<<"*";
    }

    cout<<endl;
}
return 0;

}