Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Viết chương trình nhập vào n và in ra hình theo quy luật
- INPUT
- 5
- OUTPUT
- 1 2 3 4 5
- 16 17 18 19 6
- 15 24 25 20 7
- 14 23 22 21 8
- 13 12 11 10 9
- -------------
- #include<iostream>
- using namespace std;
- int a[100][100];
- int n;
- void input(){
- cin >> n;
- }
- void output(){
- int r=0;
- int index = 1;
- while(index < n*n){
- for(int i = r; i < n-r-1; i++)
- a[r][i] = index++;
- for(int i = r; i < n-r-1; i++)
- a[i][n-r-1] = index++;
- for(int i = n-r-1; i >= r+1; i--)
- a[n-r-1][i] = index++;
- for(int i = n-r-1; i >= r+1; i--)
- a[i][r] = index++;
- r++;
- }
- if(n % 2 != 0){
- a[n/2][n/2] = n*n;
- }
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++)
- cout << a[i][j] << " ";
- cout << endl;
- }
- }
- int main(){
- input();
- output();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement