本文旨在提供用于图案打印的C++实现。
- 简单的金字塔图案
CPP
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Function
int main()
{
int n = 5;
pypart(n);
return 0;
}
CPP
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
// Outer loop to handle number of rows
// n in this case
int i = 0, j = 0;
while (i < n) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
while (j <= i) {
// Printing stars
cout << "* ";
j++;
}
j = 0; // we have to reset j value so as it can
// start from begining and print * equal to i.
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
pypart(n);
return 0;
}
CPP
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
// number of spaces
int k = 2 * n - 2;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";
// Decrementing k after each loop
k = k - 2;
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 10;
// Function Call
pypart2(n);
return 0;
}
CPP
// C++ code to demonstrate pattern printing
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
int i = 0, j = 0, k = 0;
while (i < n) {
// for number of spaces
while (k < (n - i - 1)) {
cout << " ";
k++;
}
// resetting k because we want to run k from
// beginning.
k = 0;
while (j <= i) {
cout << "*";
j++;
}
// resetting k so as it can start from 0.
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
Method 1
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void triangle(int n)
{
// number of spaces
int k = 2 * n - 2;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";
// Decrementing k after each loop
k = k - 1;
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
triangle(n);
return 0;
}
Method 2
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
// Number of spaces
int i, j, k = n;
// Outer loop to handle number of rows
// n in this case
for (i = 1; i <= n; i++) {
// Inner loop for columns
for (j = 1; j <= n; j++) {
// Condition to print star pattern
if (j >= k)
cout << "* ";
else
cout << " ";
}
k--;
cout << "\n";
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
Method 3
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
int i = 0, j = 0, k = 0;
while (i < n) {
// for spacing
while (k <= n - i - 2) {
cout << " ";
k++;
}
k = 0;
// For Patter printing
while (j < 2 * i - 1) {
cout << "*";
j++;
}
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
CPP
// C++ code to demonstrate printing
// pattern of numbers
#include
using namespace std;
// Function to demonstrate printing
// pattern
void numpat(int n)
{
// initializing starting number
int num = 1;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++)
cout << num << " ";
// Incrementing number at each column
num = num + 1;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
numpat(n);
return 0;
}
CPP
// C++ Code for pattern Printing
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
int i = 1, j = 0;
while (i <= n) {
while (j <= i - 1) {
cout << i << " ";
j++;
}
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of numbers
#include
using namespace std;
// Function to demonstrate printing pattern
void numpat(int n)
{
// Initialising starting number
int num = 1;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing number
cout << num << " ";
// Incrementing number at each column
num = num + 1;
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
numpat(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of numbers
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
int i = 1, j = 0;
// here we declare an num variable which is
// assigned value 1
int num = 1;
while (i <= n) {
while (j <= i - 1) {
// Printing numbers
cout << num << " ";
// here we are incresing num for every
// iteration.
num++;
j++;
}
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void alphapat(int n)
{
// Initializing value corresponding to 'A'
// ASCII value
int num = 65;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Explicitly converting to char
char ch = char(num);
// Printing char value
cout << ch << " ";
}
// Incrementing number
num = num + 1;
// Ending line after each row
cout << endl;
}
}
// Driver Function
int main()
{
int n = 5;
alphapat(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void alphapat(int n)
{
int i = 1, j = 0;
// assigning ASCII value of A which is 65
int num = 65;
// converting ASCII value to character,
// now our alpha variable is having
// value A after typecasting.
char alpha = char(num);
while (i <= n) {
// alpha is having A value and it
// will change as soon as alpha
// increased or decreased.
while (j <= i - 1) {
cout << alpha << " ";
j++;
}
// incrementing alpha value so as it can
// point to next character
alpha++;
// we have to reset j value so as it can
// start from beginning and print * equal to
// i.
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
alphapat(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void contalpha(int n)
{
// Initializing value corresponding to 'A'
// ASCII value
int num = 65;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Explicitely converting to char
char ch = char(num);
// Printing char value
cout << ch << " ";
// Incrementing number at each column
num = num + 1;
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
contalpha(n);
return 0;
}
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void contalpha(int n)
{
int i = 1, j = 0;
int num = 65;
char alpha = char(num);
while (i <= n) {
while (j <= i - 1) {
cout << alpha << " ";
// incrementing alpha value in every
// iteration so as it can assign to
// next character
alpha++;
j++;
}
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
contalpha(n);
return 0;
}
输出:
*
* *
* * *
* * * *
* * * * *
使用while循环在图案上打印
CPP
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
// Outer loop to handle number of rows
// n in this case
int i = 0, j = 0;
while (i < n) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
while (j <= i) {
// Printing stars
cout << "* ";
j++;
}
j = 0; // we have to reset j value so as it can
// start from begining and print * equal to i.
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
pypart(n);
return 0;
}
输出
*
* *
* * *
* * * *
* * * * *
- 180度旋转后
CPP
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
// number of spaces
int k = 2 * n - 2;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";
// Decrementing k after each loop
k = k - 2;
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 10;
// Function Call
pypart2(n);
return 0;
}
输出
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
使用while循环在图案上方打印
CPP
// C++ code to demonstrate pattern printing
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
int i = 0, j = 0, k = 0;
while (i < n) {
// for number of spaces
while (k < (n - i - 1)) {
cout << " ";
k++;
}
// resetting k because we want to run k from
// beginning.
k = 0;
while (j <= i) {
cout << "*";
j++;
}
// resetting k so as it can start from 0.
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
输出
*
**
***
****
*****
- 印刷三角
方法1
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void triangle(int n)
{
// number of spaces
int k = 2 * n - 2;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number spaces
// values changing acc. to requirement
for (int j = 0; j < k; j++)
cout << " ";
// Decrementing k after each loop
k = k - 1;
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing stars
cout << "* ";
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
triangle(n);
return 0;
}
方法2
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
// Number of spaces
int i, j, k = n;
// Outer loop to handle number of rows
// n in this case
for (i = 1; i <= n; i++) {
// Inner loop for columns
for (j = 1; j <= n; j++) {
// Condition to print star pattern
if (j >= k)
cout << "* ";
else
cout << " ";
}
k--;
cout << "\n";
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
输出
*
* *
* * *
* * * *
* * * * *
方法3
// C++ code to demonstrate star pattern
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart2(int n)
{
int i = 0, j = 0, k = 0;
while (i < n) {
// for spacing
while (k <= n - i - 2) {
cout << " ";
k++;
}
k = 0;
// For Patter printing
while (j < 2 * i - 1) {
cout << "*";
j++;
}
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart2(n);
return 0;
}
输出
*
***
*****
*******
- 数字模式
CPP
// C++ code to demonstrate printing
// pattern of numbers
#include
using namespace std;
// Function to demonstrate printing
// pattern
void numpat(int n)
{
// initializing starting number
int num = 1;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++)
cout << num << " ";
// Incrementing number at each column
num = num + 1;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
numpat(n);
return 0;
}
输出
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
上面的模式使用while循环
CPP
// C++ Code for pattern Printing
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
int i = 1, j = 0;
while (i <= n) {
while (j <= i - 1) {
cout << i << " ";
j++;
}
j = 0;
i++;
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart(n);
return 0;
}
输出
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
- 没有重新分配的号码
CPP
// C++ code to demonstrate printing pattern of numbers
#include
using namespace std;
// Function to demonstrate printing pattern
void numpat(int n)
{
// Initialising starting number
int num = 1;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Printing number
cout << num << " ";
// Incrementing number at each column
num = num + 1;
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
numpat(n);
return 0;
}
输出
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
上面的模式使用while循环
CPP
// C++ code to demonstrate printing pattern of numbers
#include
using namespace std;
// Function to demonstrate printing pattern
void pypart(int n)
{
int i = 1, j = 0;
// here we declare an num variable which is
// assigned value 1
int num = 1;
while (i <= n) {
while (j <= i - 1) {
// Printing numbers
cout << num << " ";
// here we are incresing num for every
// iteration.
num++;
j++;
}
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
pypart(n);
return 0;
}
输出
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
- 字符模式
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void alphapat(int n)
{
// Initializing value corresponding to 'A'
// ASCII value
int num = 65;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Explicitly converting to char
char ch = char(num);
// Printing char value
cout << ch << " ";
}
// Incrementing number
num = num + 1;
// Ending line after each row
cout << endl;
}
}
// Driver Function
int main()
{
int n = 5;
alphapat(n);
return 0;
}
输出
A
B B
C C C
D D D D
E E E E E
上面的模式使用while循环
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void alphapat(int n)
{
int i = 1, j = 0;
// assigning ASCII value of A which is 65
int num = 65;
// converting ASCII value to character,
// now our alpha variable is having
// value A after typecasting.
char alpha = char(num);
while (i <= n) {
// alpha is having A value and it
// will change as soon as alpha
// increased or decreased.
while (j <= i - 1) {
cout << alpha << " ";
j++;
}
// incrementing alpha value so as it can
// point to next character
alpha++;
// we have to reset j value so as it can
// start from beginning and print * equal to
// i.
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
alphapat(n);
return 0;
}
输出
A
B B
C C C
D D D D
E E E E E
- 连续字符模式
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void contalpha(int n)
{
// Initializing value corresponding to 'A'
// ASCII value
int num = 65;
// Outer loop to handle number of rows
// n in this case
for (int i = 0; i < n; i++) {
// Inner loop to handle number of columns
// values changing acc. to outer loop
for (int j = 0; j <= i; j++) {
// Explicitely converting to char
char ch = char(num);
// Printing char value
cout << ch << " ";
// Incrementing number at each column
num = num + 1;
}
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
contalpha(n);
return 0;
}
输出
A
B C
D E F
G H I J
K L M N O
上面的模式使用while循环
CPP
// C++ code to demonstrate printing pattern of alphabets
#include
using namespace std;
// Function to demonstrate printing pattern
void contalpha(int n)
{
int i = 1, j = 0;
int num = 65;
char alpha = char(num);
while (i <= n) {
while (j <= i - 1) {
cout << alpha << " ";
// incrementing alpha value in every
// iteration so as it can assign to
// next character
alpha++;
j++;
}
j = 0;
i++;
// Ending line after each row
cout << endl;
}
}
// Driver Code
int main()
{
int n = 5;
// Function Call
contalpha(n);
return 0;
}
输出
A
B C
D E F
G H I J
K L M N O
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。