ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

Wrong answer?
Posted by icecream 26 Oct 2008 00:16
I got compile error when choose C, but get wrong answer if chosse C++. what's the problem.

#include <stdio.h>
#include <stdlib.h>
#define null 0

typedef struct Link{
        int data;
        struct Link *next;
        }Link, *LinkList;
#define L sizeof(struct Link)

Link * Create(int n)
{
    Link *p,*h,*s, *m;
    int i;
    int value;

    h = (Link *)malloc(L);
    h->data = 100001;
    h->next = null;

    for (i = 0; i < n; i++)
    {
        p = h;
        s = (Link *)malloc(L);
        scanf("%d",&value);
        s->data = value;

        while (p && p->data > s->data)
        {
            m = p;
            p = p->next;
        }

        m->next = s;
        s->next = p;
    }

    return (h);
}

int main(int argc, char *argv[])
{
    int number = 0;
    scanf("%d", &number);

  struct Link *header;
  header = Create(number);
  header = header->next;
  int min = 0;
  int max = 0;
  while (header)
  {
      if (min < max)
      {
          min += header->data;
      }
      else
      {
           max += header->data;
      }
      header = header->next;
  }

  printf("%d", min > max ? min - max : max - min);

  return 0;
}