|
|
back to boardWrong answer? 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; } |
|
|