-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiamond_alternative.c
114 lines (88 loc) · 2.32 KB
/
diamond_alternative.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// by Bourbxn
// เพชรในตม
// เพชรในตมที่ไม่ใช่สำนวนอะดิ แย่แล้ว!!!
// Specification
// Input
// บรรทัดที่ 1: ตัวเลขจำนวนเต็ม หากน้อยกว่า 3 ให้แสดง "ERROR!"
// Output
// เพชรในตมมมมม~
// Sample Case
// Case 1
// 3
// -*-
// *-*
// -*-
// Case 2
// 4
// -*-
// *-*
// *-*
// -*-
// Case 3
// 5
// --*--
// -*-*-
// *---*
// -*-*-
// --*--
// this is an attempt without using middle. big mistake
#include <stdio.h>
int create_triangle(int, int, char *, int *, int *, int);
int main()
{
int number;
int isEven;
scanf("%d", &number);
if (number < 3)
{
printf("ERROR!");
return 0;
}
isEven = number % 2 == 0;
if (isEven)
number++;
// change array length for gigantic diamonds
char bottom_half[10000];
char top_half[10000];
int row = isEven ? number / 2 : number / 2 + 1;
int column = isEven ? number - 2 : number;
int margin_start = 0, margin_end = column - 1;
int column_index = 0, bottom_half_index = 0, top_half_index = 0;
create_triangle(column, row, bottom_half, &margin_start, &margin_end, 1);
margin_start--;
margin_end++;
create_triangle(column, row, top_half, &margin_start, &margin_end, isEven);
printf("%s", top_half);
printf("%s", bottom_half);
return 0;
}
int create_triangle(int column, int row, char *str, int *margin_start, int *margin_end, int required_first_line)
{
int column_index = 0, array_index = 0;
for (int i = required_first_line ? 0 : column; i < column * row; i++)
{
if (column_index == *margin_start || column_index == *margin_end)
{
str[array_index] = '*';
}
else
{
str[array_index] = '-';
}
column_index++;
array_index++;
if (column_index % column == 0)
{
// this doesn't work as intented
// *margin_start++;
// *margin_end--;
*margin_start = *margin_start + 1;
*margin_end = *margin_end - 1;
str[array_index] = '\n';
array_index++;
column_index = 0;
}
}
str[array_index] = '\0';
return 0;
}