1. 打印菱形

Image 1.png

解析

  • 利用点到中心点的曼哈顿距离,距离<= n/2打印

Image 2.png

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main(){
  5. int n;cin>>n;
  6. int x = n/2 , y = n/2;
  7. for(int i = 0 ; i < n ; i++){
  8. for(int j = 0 ; j < n ; j++){
  9. if(abs(i-x) + abs(j-y) <= n/2)cout<<"*";
  10. else cout<<" ";
  11. }
  12. cout<<endl;
  13. }
  14. return 0;
  15. }