V. Array Again
বলে রাখি, আমি ইচ্ছে করে এই কোডে একটি bug রেখেছি আপনারা জাস্ট debug করে উপলোড দিবেন or for RIGHT answer ক্লিক হেয়ার
Problem link Click here
Score: 1
CPU: 1s
Memory: 512MB
CPU: 1s
Memory: 512MB
You will be given a grid of size
n*n. Then you will be given n*n numbers of the grid. Now you have to print the numbers of grid in spiral way.
Suppose you have a grid of size
3 * 3 and the given numbers are 1 2 3 4 5 6 7 8 9. Then the number will be in the grid like below.1 2 3
4 5 6
7 8 9
Now you have to print numbers as
1 2 3 6 9 8 7 4 5
Input
First you will be given an positive integer
Output
Output For each test case you have to print
First you will be given an positive integer
T (1< T <= 40), the number of test case. For each test case you will be given n( 0 < n < 50 ), the size of the grid. Then there will be n lines, each containing n numbers. No numbers will be greater than 9.Output
Output For each test case you have to print
“Case X: ”, where X is the number of test case and n*n numbers separated by a single space in spiral order, from top left side of the grid.Sample
| Input | Output |
|---|---|
| 1 3 1 2 3 4 5 6 7 8 9 | Case 1: 1 2 3 6 9 8 7 4 5 |
Solution
#include <stdio.h> int main() { int row,t,i, col, n, matrix[49][49]; scanf("%d",&t); for(i=1;i<=t;i++){ scanf("%d", &n); for(row = 1; row <= n; row++){ for(col = 1; col <= n; col++){ scanf("%d", &matrix[row][col]); } } row = 1; printf("Case %d:",i); while(n > 0){ for(col = row; col <= n; col++){ printf(" %d",matrix[row][col]); } for(col = row+1; col <= n; col++){ printf(" %d", matrix[col][n]); } for(col = n-1; col > row-1; col--){ printf(" %d", matrix[n][col]); } for(col = n-1; col > row; col--){ printf(" %d", matrix[col][row]); } row++; n--; } printf("\n"); } return 0; }
No comments:
Post a Comment