- /*
- 需求:
- *
- * * *
- * * * * *
- * * *
- *
- 分析:
- 1,用for循环来实现。
- 2,代码具有可扩展性
- 3,应该有相应的帮助字符,来帮用户进行输入
- 4,应该用if语句来控制进行输入的字符,错误则提示用户从新输入
- */
- #include<iostream>
- using namespace std;
- int main()
- {
- int t;
- cin>>t;
- while(t%2==0)
- {
- cout<<"输入字符有误,请检查后重新输入一个奇数"<<endl;
- cin>>t;
- }
- for(int x=1;x<=t;x++)
- {
- cout<<"\\t";
- if(x<=(t+1)/2)
- {
- for(int y=1;y<=2*((t+1)/2-x);y++)
- {
- cout<<' ';
- }
- for(int z=1;z<=2*x-1;z++)
- {
- cout<<"* ";
- }
- cout<<""<<endl;
- }
- else
- {
- for(int y=1;y<=2*(x-(t+1)/2);y++)
- {
- cout<<' ';
- }
- for(int z=1;z<=(t-x)*2+1;z++)
- {
- cout<<"* ";
- }
- cout<<""<<endl;
- }
- }
- return 1;
- }
- /*
- 疑问:
- 1,如果需要打印完之后重新打印其他数字,而不进行关闭后重新打印,该如何去实现 。
- 2,如果需要实现清屏后在打印上述需求,又该如何实现。
- */
- //该片段来自于http://www.codesnippet.cn/detail/2301201511613.html
来源: http://www.codesnippet.cn/detail/2301201511613.html