1 条题解
-
0
C :
#include<stdio.h> #include<stdlib.h> #include<string.h> #define OK 1 #define ERROR 0 typedef int Status; typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode ; typedef struct Link { LNode *head; }Link; int Init(Link *L) { L->head=(LNode *)malloc(sizeof(LNode)); if(!L->head)return ERROR; L->head->next=NULL; return OK; } void insert(Link *L,ElemType e) { LNode *q=L->head; LNode *p=(LNode *)malloc(sizeof(LNode)); if(!q->next) { p->data=e; p->next=q->next; q->next=p; } else { while(q->next&&q->next->data<e)q=q->next; p->data=e; p->next=q->next; q->next=p; } } Status Listshanc(Link *L,int i) { LNode *p,*q; p=L->head; int j=0; while(p->next&&j<i-1) { p=p->next; j++; } if(!p->next||j>=i)return ERROR; q=p->next; p->next=q->next; free(q); return OK; } void shu(Link L) { LNode *p=L.head->next; while(p) { printf("%d ",p->data); p=p->next; } } int hfm(Link *L) { int i,a,b,t,s=0; LNode *r=L->head; while(r->next&&r->next->next) { a=r->next->data; b=r->next->next->data; t=a+b; s=s+t; insert(L,t); Listshanc(L,1); Listshanc(L,1); } return s; } int main() { int i,n,m,a,b,s=0; Link L; while(scanf("%d",&n)!=EOF) { Init(&L); for(i=0;i<n;i++) { scanf("%d",&m); insert(&L,m); } printf("%d\n",hfm(&L)); } }
C++ :
#include <stdio.h> #include <stdlib.h> int val[1001]; int cmp(const void * a,const void * b) { return *(int *)a - *(int *)b; } int main() { int n; int i; int ans; while(scanf("%d",&n) != EOF) { ans = 0; for(i = 0;i < n;i++) scanf("%d",&val[i]); for(i = 1;i < n;i++) { qsort(&val[i - 1],n - i + 1,sizeof(val[0]),cmp); ans += val[i - 1] + val[i]; val[i] += val[i - 1]; } printf("%d\n",ans); } return 0; }
Pascal :
program hfm; type htree=record d:longint; f:longint; l:longint; r:longint; end; var ht:array[1..10000] of htree; n,m,i,j,s1,s2,p1,p2,w:longint; begin while not eof do begin w:=0; readln(n); if n=0 then break else begin for i:=1 to n do begin ht[i].d:=0; ht[i].f:=0; ht[i].l:=0; ht[i].r:=0; end; for i:=1 to n do read(ht[i].d); m:=2*n-1; for j:=n+1 to m do begin s1:=1000000; s2:=1000000; p1:=0; p2:=0; for i:=1 to j-1 do if ht[i].f=0 then if ht[i].d<s1 then begin s2:=s1; s1:=ht[i].d; p2:=p1; p1:=i; end else if ht[i].d<s2 then begin s2:=ht[i].d; p2:=i; end; ht[j].d:=s1+s2; ht[j].l:=p1; ht[j].r:=p2; ht[p1].f:=j; ht[p2].f:=j; end; for i:=n+1 to m do w:=w+ht[i].d; writeln(w); end; end; end.
- 1
信息
- ID
- 1116
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者