C++打印三角形、四边形

电玩女神 2022-10-09 06:27 341阅读 0赞

C++打印三角形、四边形

用“*“字符组成几种三角形、四边形

1、打印等腰三角形

20210624105636959.png

法一:

  1. //打印正三角形
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int n = 4; //行数
  7. for (int i = 1; i <= n; i++) {
  8. for (int k = 0; k < n - i; k++) {
  9. cout << " ";
  10. }
  11. for (int j = 0; j < 2 * i - 1; j++) {
  12. cout << "*";
  13. }
  14. cout << endl;
  15. }
  16. return 0;
  17. }

法二

  1. //打印三角形——简单
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << " *" << endl;
  7. cout << " ***" << endl;
  8. cout << " *****" << endl;
  9. cout << "*******"<< endl;
  10. return 0;
  11. }

2、打印倒等腰三角形

20210624105701246.png

法一

  1. //打印倒三角形
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int n = 4; //行数
  7. for (int i = n; i > 0; i--) {
  8. for (int k = 0; k < n - i; k++) {
  9. cout << " ";
  10. }
  11. for (int j = 0; j < 2 * i - 1; j++) {
  12. cout << "*";
  13. }
  14. cout << endl;
  15. }
  16. return 0;
  17. }

法二

  1. //打印倒三角形——简单
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "*******"<< endl;
  7. cout << " *****" << endl;
  8. cout << " ***" << endl;
  9. cout << " *" << endl;
  10. return 0;
  11. }

3、打印直角三角形

20210624105738118.png

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int n,i,j;
  5. n = 4; //行数
  6. for(i = 1;i <= n;i++){
  7. for(j = 1;j <= i;j ++){
  8. cout << "*";
  9. }
  10. cout << endl;
  11. }
  12. return 0;
  13. }

4、打印倒直角三角形

20210624105806702.png

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int n,i,j;
  5. n = 4; //行数
  6. for(i = 1;i <= n;i++){
  7. for(j = n;j >= i;j --){
  8. cout << "*";
  9. }
  10. cout << endl;
  11. }
  12. return 0;
  13. }

5、打印矩形

20210624213110675.png

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int n,i,j;
  5. n = 4; //行数
  6. for(i = 1;i <= n;i++){
  7. for(j = 1;j <= n;j ++){
  8. cout << "*";
  9. }
  10. cout << endl;
  11. }
  12. return 0;
  13. }

6、打印平行四边形

20210624213202574.png

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int n,i,j;
  5. n = 4; //行数
  6. for(i = 1;i <= n;i++){
  7. for(j = 1;j <= i - 1;j ++){
  8. cout << " ";
  9. }
  10. for(j = 1;j <= n;j++){
  11. cout << "*";
  12. }
  13. cout << endl;
  14. }
  15. return 0;
  16. }

7、打印菱形

20210624213337858.png

法一

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n = 4; //输入菱形的边长
  6. //上半层
  7. for (int i = 0; i < n; i++)
  8. {
  9. for (int j = 0; j < (n - 1) - i; j++)
  10. {
  11. cout << " ";
  12. }
  13. for (int j = 0; j < (2 * i + 1); j++)
  14. {
  15. cout << "*";
  16. }
  17. cout << endl;
  18. }
  19. //下半层
  20. for (int i = 0; i < n - 1; i++)
  21. {
  22. for (int j = 0; j < i + 1; j++)
  23. {
  24. cout << " ";
  25. }
  26. for (int j = 0; j < 2 * ( n - 1 ) - (1 + 2 * i); j++)
  27. {
  28. cout << "*";
  29. }
  30. cout << endl;
  31. }
  32. return 0;
  33. }

法二

  1. #include <iostream>
  2. #include <stdlib.h> //abs()用到
  3. using namespace std;
  4. int M = 4; //菱形边长字符数
  5. int f(int x, int y) {
  6. return abs(x) + abs(y) < M;
  7. }
  8. int main() {
  9. for (int y = 1 - M; y < M; ++y) {
  10. for (int x = 1 - M; x < M; ++x) {
  11. cout << " *"[f(x, y)];
  12. }
  13. cout << endl;
  14. }
  15. return 0;
  16. }

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读