LOADING
378 words
2 minutes
有关三角形打印

各种三角形打印!

题目要求三角形打印的时候,不同的格式总是容易记混,这已经不是一般的题目了,必须要整理(

#include <iostream>
using namespace std;
int main()
{
int n = 5;
// 左下直角三角形
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 左上直角三角形
for(int i=n-1;i>=0;i--){
for(int j=0;j<=i;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 右下直角三角形
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
cout << " ";
}
for(int j=0;j<=i;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 右上直角三角形
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
cout << " ";
}
for(int j=0;j<n-i;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 正向等腰三角形
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
cout << " ";
}
for(int j=0;j<2*i+1;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 倒置等腰三角形
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
cout << " ";
}
for(int j=0;j<2*(n-i)-1;j++){
cout << "*";
}
cout << endl;
}
cout << endl;
// 空心直角三角形
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
if( j==0 || j==i || i==n-1 ){
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
// 空心等腰三角形
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
cout << " ";
}
for(int j=0;j<2*i+1;j++){
if( j==0 || j==2*i || i==n-1){
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
// 数字直角三角形
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
cout << j+1;
}
cout << endl;
}
cout << endl;
// 字母等腰三角形
int alpha = 65;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
cout << " ";
}
for(int j=0;j<2*i+1;j++){
printf("%c",j+65);
}
cout << endl;
}
return 0;
}

感觉手搓一遍已经记住了,不过这是好事啊😋

Some information may be outdated

Profile Image of the Author
Yoi027K
Hi
Announcement
Welcome to my blog!
Directory
Posts
Categories
Tags
Table of Contents