C++中有意思的模式打印程序
本教程将讨论一个程序,用于打印一个给定的有趣的模式。
为此,我们将提供该模式的一半宽度。我们的任务是根据给定的宽度打印一个类似的模式,其左右两部分相互镜像。
示例
#include<stdio.h>
//printing the given pattern
void print_pattern(int n){
int i,j;
//printing the upper half
for (i=1; i<=n; i++){
for (j=1; j<=(2*n); j++){
// Left portion
if (i<j)
printf(" ");
else
printf("*");
// Right portion
if (i<=((2*n)-j))
printf(" ");
else
printf("*");
}
printf("\n");
}
//printing the lower half
for (i=1; i<=n; i++){
for (j=1;j<=(2*n);j++){
// Left portion
if (i>(n-j+1))
printf(" ");
else
printf("*");
// Right portion
if ((i+n)>j)
printf(" ");
else
printf("*");
}
printf("\n");
}
}
int main(){
print_pattern(6);
return 0;
}输出
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP