Advertisement
llvlleo1810

Dạng bài tập về vẽ hình bằng số

Apr 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. Viết chương trình nhập vào n và in ra hình theo quy luật
  2. INPUT
  3. 5
  4. OUTPUT
  5. 1 2 3 4 5
  6. 16 17 18 19 6
  7. 15 24 25 20 7
  8. 14 23 22 21 8
  9. 13 12 11 10 9
  10. -------------
  11. #include<iostream>
  12. using namespace std;
  13.  
  14. int a[100][100];
  15. int n;
  16.  
  17. void input(){
  18.  cin >> n;
  19. }
  20.  
  21. void output(){
  22.  int r=0;
  23.  int index = 1;
  24.  while(index < n*n){
  25.   for(int i = r; i < n-r-1; i++)
  26.    a[r][i] = index++;
  27.   for(int i = r; i < n-r-1; i++)
  28.    a[i][n-r-1] = index++;
  29.   for(int i = n-r-1; i >= r+1; i--)
  30.    a[n-r-1][i] = index++;
  31.   for(int i = n-r-1; i >= r+1; i--)
  32.    a[i][r] = index++;
  33.   r++;
  34.  }
  35.  if(n % 2 != 0){
  36.   a[n/2][n/2] = n*n;
  37.  }
  38.  for(int i = 0; i < n; i++){
  39.   for(int j = 0; j < n; j++)
  40.    cout << a[i][j] << " ";
  41.   cout << endl;
  42.  }
  43. }
  44.  
  45. int main(){
  46.  input();
  47.  output();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement