-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrinting_Pattern_Using_Loops.c
More file actions
60 lines (48 loc) · 1.14 KB
/
Copy pathPrinting_Pattern_Using_Loops.c
File metadata and controls
60 lines (48 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
int main()
{
int n,temp;
scanf("%d",&n);
temp = n;
int p[2*n-1][2*n-1];
for(int i=0; i<2*n-1; i++)
{
for(int k = i; k < 2*n-1-i ; k++)
p[i][k] = temp;
for(int k = 2*n-2-i; k > i ; k--)
p[k][2*n-2-i] = temp;
for(int k = 2*n-3-i ; k >= i ; k--)
p[2*n-2-i][k] = temp;
for(int k = 2*n-3-i ; k > i ; k--)
p[k][i] = temp;
temp--;
}
for(int i = 0; i<2*n-1 ; i++)
{
for(int j=0; j < 2*n-1 ; j++)
printf("%d ",p[i][j]);
printf("\n");
}
return 0;
}
// Solution 2: Find the shortest distance of (i,j) from four sides and then print n - shortest_distance.
#include <stdio.h>
#define min(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int n;
scanf("%d", &n);
int len = n * 2 - 1;
for(int row = 0; row < len; row++)
{
for(int col = 0; col < len; col++)
{
int m = min(row, col);
m = min(m, len - row - 1);
m = min(m, len - col - 1);
printf("%d ", n - m);
}
printf("\n");
}
return 0;
}