求大神帮解关于C++的一道题!

2025-03-15 00:36:32
推荐回答(1个)
回答1:

//用链表方法通过键盘输入整数,直到遇到一个负数为止,
//该负数不考虑在内,求所输整数的合、平均值、最大值和最小值,并在屏幕上输出。不要用数组方法
#include 
using namespace std;
struct Node
{
     int data;
     Node *next;
}*head=NULL;
int main()
{
  int d;
  int n=0;
  int max;
  int min;
  int sum=0;
  Node *p;
  cin>>d;
  while(d>=0)
  {
    n++;
 p=new Node;
 p->next=NULL;
 p->data=d; 
 if(head==NULL)
 {
   head=p;
      max=d;
      min=d;
      p->next=head;
      sum=sum+d;
 }
 else
 {
  if(max    max=d;
  if(min>d)
    min=d;
  sum=sum+d;
  p->next=head;
  head=p;    
 }
 cin>>d;
  }
  cout<<"max="<  cout<<"min="<  cout<<"sum="<  cout<<"acg="<  return 0;
}