これは、プログラムの先生が授業でループを教えたときに出してくれた例の一つです。問題そのものは難しくないと思いますが、ループの練習や反応速度のトレーニングにはとても向いています。それに、今の私は VB の腕がだいぶ落ちてしまいました……。そこで、C++ のコードを添付するので、ぜひ研究してみてください~
問題は一番上の画像のとおりです。ループだけを使って、上の 4 種類の三角形を出力してください。解答は下にあります。
コード:
ダウンロード先:Box.net|4Shared|Dropbox|Google
#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;
}
