Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ สำหรับการพิมพ์รูปแบบ Expressionless Face

โจทย์

กำหนดตัวเลข n งานคือสร้างรูปแบบ Expressionless face จนถึง n บรรทัดและแสดงผลลัพธ์ Expressionless face ถูกสร้างโดยใช้ตัวอักษรพิเศษ expressionless face ด้วยตัวอักษรพิเศษดูเหมือน: "*_*"

ตัวอย่าง

Input-: n = 6 Output-:

โปรแกรม C++ สำหรับการพิมพ์รูปแบบ Expressionless Face

Input-: n = 8 Output-:

โปรแกรม C++ สำหรับการพิมพ์รูปแบบ Expressionless Face

อัลกอริทึม

เริ่ม
ขั้นตอน 1 → ในฟังก์ชัน print_stars(int i)
  วนลูป For j = 1 ถึง i และ j++
    พิมพ์ "*"
ขั้นตอน 2 → ในฟังก์ชัน print_pattern(int rows)
  วนลูป For i = 1 ถึง rows และ i++
    เรียกฟังก์ชัน print_stars(i)
    พิมพ์ "_"
    เรียก print_stars(rows - i + 1)
    พิมพ์ "_"
    เรียก print_stars(rows - i + 1)
    พิมพ์ "_"
    เรียก print_stars(i)
    พิมพ์ newline
ขั้นตอน 3 → ในฟังก์ชัน int main()
  ประกาศและตั้งค่า rows = 8
  เรียก print_pattern(rows)
หยุด

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;
// ฟังก์ชันพิมพ์ดาว
void print_stars(int i) {
  for (int j = 1; j <= i; j++)
    cout << "*";
}
void print_pattern(int rows) {
  for (int i = 1; i <= rows; i++) {
    print_stars(i);
    cout << "_";
    print_stars(rows - i + 1);
    cout << "_";
    print_stars(rows - i + 1);
    cout << "_";
    print_stars(i);
    cout << endl;
  }
}
int main() {
  int rows = 8;
  print_pattern(rows);
  return 0;
}

ผลลัพธ์

โปรแกรม C++ สำหรับการพิมพ์รูปแบบ Expressionless Face