生活随笔
收集整理的這篇文章主要介紹了
巧妙打印对称图案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
算法:
第一個的代碼的思想是以前從網上看到的,原理很簡單,就是利用高中數學中線性規劃的知識; ? ? ?? ?????? 第二個代碼是利用圖案的對稱關系,其總共有5*2-1行,分析可知,每行的空格數量是:5-當前行數;每行的空格數量是:行數*2-1;下面最后4行的圖案是根據上面4行的圖案換算得來的。
運行效果1???????????? ? ?? ?運行效果2
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{short int n;while(cin>>n){for(int i=-n; i<=n; i++){for(int j=-n; j<=n; j++)abs(i)+abs(j) >= n ? cout<<" " : cout<<"*";cout<<endl;//可適當更改abs(i)+abs(j) 和 n的關系,比如>=}}return 0;
}
#include <iostream>
using namespace std;
int main(void)
{int n; //n代表要打印圖案的行數(母圖)h_1代表行數 h_2也是代表行數 l代表列數。while(cin>>n){for(int h_1=1,h_2=1; h_1<=2*n-1; h_1++){if( h_1 > n)h_2 = 2*n - h_1;elseh_2 = h_1;for(int l=n-h_2; l>=1; l--)cout<<" ";for(int l=1; l<=2*h_2-1; l++)cout<<"*";cout<<endl;}}return 0;
}
總結
以上是生活随笔為你收集整理的巧妙打印对称图案的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。