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