378 words
2 minutes
有关三角形打印
各种三角形打印!
题目要求三角形打印的时候,不同的格式总是容易记混,这已经不是一般的题目了,必须要整理(
1#include <iostream>2
3using namespace std;4
5int main()6{7 int n = 5;8
9 // 左下直角三角形10
11 for(int i=0;i<n;i++){12 for(int j=0;j<=i;j++){13 cout << "*";14 }15 cout << endl;16 }17
18 cout << endl;19
20 // 左上直角三角形21
22 for(int i=n-1;i>=0;i--){23 for(int j=0;j<=i;j++){24 cout << "*";25 }26 cout << endl;27 }28
29 cout << endl;30
31 // 右下直角三角形32
33 for(int i=0;i<n;i++){34 for(int j=0;j<n-i-1;j++){35 cout << " ";36 }37 for(int j=0;j<=i;j++){38 cout << "*";39 }40 cout << endl;41 }42
43 cout << endl;44
45 // 右上直角三角形46
47 for(int i=0;i<n;i++){48 for(int j=0;j<i;j++){49 cout << " ";50 }51 for(int j=0;j<n-i;j++){52 cout << "*";53 }54 cout << endl;55 }56
57 cout << endl;58
59 // 正向等腰三角形60
61 for(int i=0;i<n;i++){62 for(int j=0;j<n-i-1;j++){63 cout << " ";64 }65 for(int j=0;j<2*i+1;j++){66 cout << "*";67 }68 cout << endl;69 }70
71 cout << endl;72
73 // 倒置等腰三角形74
75 for(int i=0;i<n;i++){76 for(int j=0;j<i;j++){77 cout << " ";78 }79 for(int j=0;j<2*(n-i)-1;j++){80 cout << "*";81 }82 cout << endl;83 }84
85 cout << endl;86
87 // 空心直角三角形88
89 for(int i=0;i<n;i++){90 for(int j=0;j<=i;j++){91 if( j==0 || j==i || i==n-1 ){92 cout << "*";93 }94 else{95 cout << " ";96 }97 }98 cout << endl;99 }100
101 cout << endl;102
103 // 空心等腰三角形104
105 for(int i=0;i<n;i++){106 for(int j=0;j<n-i-1;j++){107 cout << " ";108 }109 for(int j=0;j<2*i+1;j++){110 if( j==0 || j==2*i || i==n-1){111 cout << "*";112 }113 else{114 cout << " ";115 }116 }117 cout << endl;118 }119
120 cout << endl;121
122 // 数字直角三角形123
124 for(int i=0;i<n;i++){125 for(int j=0;j<=i;j++){126 cout << j+1;127 }128 cout << endl;129 }130
131 cout << endl;132
133 // 字母等腰三角形134
135 int alpha = 65;136
137 for(int i=0;i<n;i++){138 for(int j=0;j<n-i-1;j++){139 cout << " ";140 }141 for(int j=0;j<2*i+1;j++){142 printf("%c",j+65);143 }144 cout << endl;145 }146
147 return 0;148}感觉手搓一遍已经记住了,不过这是好事啊😋
Some information may be outdated