fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  6. int t;
  7. cin>>t;
  8. for(int k=1;k<=t;k++)
  9. {
  10. cout<<"Case #"<<k<<":"<<endl;
  11. int n;
  12. cin>>n;
  13. vector<vector<char>> a(n,vector<char>(n,' '));
  14. int sr=0,sc=0,er=n-1,ec=n-1;
  15. while(sr<=er && sc<=ec)
  16. {
  17. for(int i=sc;i<=ec;i++)
  18. a[sr][i]='*';
  19. sr++;
  20. for(int i=sr;i<=er;i++)
  21. a[i][ec]='*';
  22. ec--;
  23. if(sc!=0)
  24. sc++;
  25. if(sr<er)
  26. {for(int i=ec;i>=sc;i--)
  27. a[er][i]='*';
  28. er--;
  29. }
  30. sr++;
  31. //er--;
  32. if(sc<ec){
  33. for(int i=er;i>=sr;i--)
  34. a[i][sc]='*';
  35. sc++;
  36. }
  37. ec--;
  38. er--;
  39. //=sc++;
  40. }
  41. for(int i=0;i<n;i++)
  42. {
  43. for(int j=0;j<n;j++)
  44. {
  45. if(a[i][j]=='*')
  46. cout<<"* ";
  47. else
  48. cout<<" ";
  49. }
  50. cout<<endl;
  51. }
  52. cout<<endl;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5284KB
stdin
1
5
stdout
Case #1:
* * * * * 
        * 
* * *   * 
*       * 
* * * * *