创建各类三角形图案
创建各类三角形图案
第一种:
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "输入行数: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
第二种:
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "输入行数: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
以上程序执行输出结果为:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
第三种:
#include <iostream>
using namespace std;
int main()
{
char input, alphabet = 'A';
cout << "输入最后一个大写字母: ";
cin >> input;
for(int i = 1; i <= (input-'A'+1); ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet;
cout << endl;
}
return 0;
}
以上程序执行输出结果为:
A
B B
C C C
D D D D
E E E E E
第四种:
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "输入行数: ";
cin >> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
第五种:
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "输入行数: ";
cin >> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}
以上程序执行输出结果为:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
|