๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Python

[์•Œ๊ณ ๋ฆฌ์ฆ˜] Day3 - 2309๋ฒˆ ์ผ๊ณฑ ๋‚œ์Ÿ์ด

by Danna 2018. 8. 8.
728x90
728x90

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);
               }
        }
}




728x90
728x90