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
|
#include <stdio.h>
int med3(int a,int b,int c)
{
// a b
if(a>=b)
{
if(b>=c)
{
return b;
}
else if(a<=c)
{
return a;
}
else
{
return c;
}
}
// b a
else if(a>c)
{
return a;
}
else if(b>c)
{
return c;
}
else //c b a
{
return b;
}
}
int med(int a,int b,int c)
{
if(a>b) // a b
{
if(a<c) //c a b
{
return a;
}
else if(c>b)// a b
{
return c;
}
else // a b c
{
return b;
}
}
else if(c>b) //c b a
{
return b;
}
else if(c>a) // b c a
{
return c;
}
else // b a c
{
return a;
}
}
int med_3(int a,int b,int c)
{
if((b>=a && c<=a) || (b<=a && c>=a))
return a;
else if((a>b&&c<b)||(a<b&&c>b))
return b;
return c;
}
int main(void)
{
int arr[3];
int i=0;
while(1)
{
for(i=0;i<3;i++)
{
printf("%d num input:",i);
scanf("%d",arr+i);
}
printf("med num : %d\n",med(arr[0],arr[1],arr[2]));
}
return 0;
}
|
cs |
med_3 의 경우 효율이 떨어진다.
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 | #include <stdio.h> void spira_2(int a); void spira_1(int a) { char temp=32; int i,j; // tttt* // ttt*** // tt***** // t******* // ********* for(i=1;i<=a;i++) { for(j=a;j>i;j--) { printf("%c",temp); } // 1 3 5 7 9 for(j=1;j<=i*2-1;j++) { printf("*"); } printf("\n"); } spira_2(a); } void spira_2(int a) { char temp=32; int i,j; for(i=2;i<=a;i++) { for(j=1;j<i;j++) { printf("%c",temp); } for(j=a*2;j>i*2-1;j--) { printf("*"); } printf("\n"); } } int main(void) { int num; while(1){ printf("input:"); scanf("%d",&num); if(num<=0) { break; } spira_1(num); } return 0; } | cs |
'IT 관련 공부 > 자료구조와 알고리즘 (C)' 카테고리의 다른 글
이진 검색 binary search (0) | 2019.07.17 |
---|---|
선형검색, 선형검색 보초법 (0) | 2019.07.09 |
진수변환 프로그램 (0) | 2019.07.05 |
n단 숫자 피라미드, 바람개비 (0) | 2019.07.02 |
자료구조와 알고리즘 스터디 시작합니다. (0) | 2019.07.01 |