用个链表写,效率还不错:
#include <stdio.h> typedef struct Node{ int key; struct Node* next; }Node; int main(){ Node* L=(int*)malloc(sizeof(int)); Node* p; p=L; int t,positive=0,negative=0;float sum=0; while(scanf("%d",&t)!=EOF){ Node* s=(int*)malloc(sizeof(int)); s->key=t; p->next=s; p=s; } p=L->next; while(p){ if(p->key>=0){ positive++; sum+=p->key; p=p->next; } else{ negative++; p=p->next; } } free(L); if(!positive){ printf("%d\n0.0",negative); } else{ printf("%d\n%.1f",negative,sum/positive); } }