#include <stdio.h>
#include <stdlib.h>
typedef struct Entry{
 int denominator; //분모
 int numerator; //분자
 struct Entry* next;
}ENTRY;
ENTRY head;
void deleteLink()
{
 ENTRY *go, *target;
 go = head.next;
 while(go)
 {
  target = go;
  go = go->next;
  free(target);
 }
}
void printLink()
{
 ENTRY *go;
 for(go = head.next; go != NULL; go=go->next)
 {
  printf("%d/%d\n", go->numerator, go->denominator);
 }

}
void insert(ENTRY* temp)
{
 ENTRY *go, *pre;

 pre = &head;
 for(go = head.next; go != NULL; pre=go, go=go->next)
 {
 // puts("111");
  if((float)temp->numerator/temp->denominator <= (float)go->numerator/go->denominator)
   break;
 }

 if(go != NULL) //중간에 나옴
 {
 // puts("222");
  if ((float)temp->numerator/temp->denominator == (float)go->numerator/go->denominator)
   return;
  else
  {
   go = (ENTRY *)malloc(sizeof(ENTRY));
   go->denominator = temp->denominator;
   go->numerator = temp->numerator;
   go->next = pre->next;
   pre->next = go;
  }

 }
 else //끝까지 봄
 {
 // puts("333");
  go = (ENTRY *)malloc(sizeof(ENTRY));
  go->denominator = temp->denominator;
  go->numerator = temp->numerator;
  go->next = pre->next;
  pre->next = go;
 }
}
void main()
{
 int n;
 int denominator; //분모
 int numerator; //분자
 ENTRY temp;
 

 // n입력
 scanf("%d", &n);
 //예외처리
 if (n < 1 || n > 160)
  return;
// printf("%d", n);

 for(denominator = 1; denominator <= n; denominator++)
 {
  for(numerator = 0; numerator <= denominator; numerator++)
  {
   temp.denominator = denominator;
   temp.numerator = numerator;
   insert(&temp);

  // printLink();
  // getchar();
  }
 }

 printLink(); 

 deleteLink();
}

'Programming Language' 카테고리의 다른 글

print start recursive c example  (0) 2015.06.19
zigzag c exmaple  (0) 2015.06.19
fraction sort c example  (0) 2015.06.19
shortest path c example  (0) 2015.06.19
generate all combination of elements using 2-dimension vector  (0) 2015.04.30
list example C++  (0) 2014.11.06
by TrafficYoon 2015.06.19 17:04
| 1 2 3 4 5 6 7 8 9 10 |