๐ Algorithm/์๊ณ ๋ฆฌ์ฆ-Python
[์๊ณ ๋ฆฌ์ฆ] Day3 - 2309๋ฒ ์ผ๊ณฑ ๋์์ด
by Danna
2018. 8. 8.
00 ๋ฌธ์
01 ์์ด๋์ด
9๊ฐ ์ค์ ์์๋๋ก 7๊ฐ๋ฅผ ๋ฝ์์ 100์ด ๋๋์ง ํ์ธํ๊ณ result ๋ฐฐ์ด์ ์ ์ฅํ๋ค.
insert sort๋ฅผ ์ด์ฉํด ์ ๋ ฌํด ์ค๋ฆ์ฐจ์์ผ๋ก ์ถ๋ ฅํ๋ค.
1234567 1234568 1234569 1234578 1234579 ์๋ฐ ์์
02 ์ฝ๋
#define scanf scanf_s
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define N 9
void find_seven_dewarf(int *height, int *result);
void insertion_sort(int N_, int *num);
int main()
{
int i;
int height[9] = { 0, };
int result[7] = { 0, };
for (i = 0; i < 9; i++)
scanf("%d", &height[i]);
find_seven_dewarf(height, result);
insertion_sort(7, result);
for (i = 0; i < 7; i++)
printf("%d\n", result[i]);
return 0;
}
void find_seven_dewarf(int *height, int *result)
{
int sum;
int a, b, c, d, e, f, g;
for (a = 0; a < N; a++)
{
for (b = a + 1; b < N; b++)
{
for (c = b + 1; c < N; c++)
{
for (d = c + 1; d < N; d++)
{
for (e = d + 1; e < N; e++)
{
for (f = e + 1; f < N; f++)
{
for (g = f + 1; g < N; g++)
{
sum = height[a] + height[b] + height[c] + height[d] + height[e] + height[f] + height[g];
if (sum == 100)
{
result[0] = height[a];
result[1] = height[b];
result[2] = height[c];
result[3] = height[d];
result[4] = height[e];
result[5] = height[f];
result[6] = height[g];
return;
}
}
}
}
}
}
}
}
}
void insertion_sort(int N_, int *num)
{
int i, j;
for (i = 1; i < N_; i++)
{
int key = i;
for (j = i - 1; j >= 0; j--)
{
if (num[key] < num[j])
{
int tmp = num[key];
num[key] = num[j];
num[j] = tmp;
key = j;
}
//print_num(N_, num);
}
}
}