在圣诞节来临之际,如何写一个会变大的圣诞树给你的朋友们

思路:可以通过联想初学c++时的输出三角型和矩形来写圣诞树,通过循环,我们就可以会得到一个能够控制层数的圣诞树。(不建议太多层,屏幕放不下)

​
#include<iostream>
using namespace std;
int main(){
	int n;
	int i;
	int a;
	cout<<"                "<<"你想要几层的树?:"; 
	cin>>n;
	cout<<endl;
 	for(i=0;i<n;i++){
 		 for(a=1;a<=i+2;a++){
        	for(int j=1;j<=2*n-2*a+2;j++)
        	cout<<"  ";
        	for(int j=1;j<=4*a-3;j++)
        	cout<<" "<<'*';
        	cout<<endl;
    	}
	}
		for( a=1;a<=n+1;a++){
        	for(int j=1;j<=(4*n+1)/2-1;j++)
        	cout<<"  ";
        	for(int j=1;j<=3;j++)
        	cout<<" "<<'*';
        	cout<<endl;
    	}
    	cout<<endl;
	cout<<"                 "<<"Merry Christmas !";
	system("pause"); 
	return 0;
}

​

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐