Untitled

 avatar
unknown
c_cpp
4 years ago
1.9 kB
7
Indexable
#include<iostream>
using namespace std;

struct Node
{
  int info;
  struct Node *Next;
};
typedef struct Node NODE;
NODE *KhoiTaoNODE(int x)
{
  NODE *p= new NODE;
  p->info=x;
  p->Next=NULL;
  return p;
}

struct list
{
  NODE *Head;
};
typedef struct list LIST;
void KhoiTao(list &l)
{
  l.Head=NULL;
}

void ThemVaoDau(LIST &l, NODE *p)
{
  if(l.Head==NULL) l.Head=p;
  else
  {
    p->Next=l.Head;
    l.Head=p;
  }
}

void XuatDanhSach(LIST l)
{
  for(NODE *i=l.Head; i != NULL; i= i->Next)
  {
    cout << i->info << "\t";
  }
}
int Tongdanhsach(LIST l)
{ 
  int sum = 0;
  for(NODE *i=l.Head; i != NULL; i= i->Next)
  {
    sum += i->info; 
  }
  return sum;
}
void Sapxep(LIST l)
{
  for(NODE *i=l.Head; i->Next != NULL; i= i->Next)
  {
    for (NODE *j=i->Next; j != NULL; j=j->Next)
    {
      if (j->info < i->info) swap (j->info,i->info);
    }
  }
}
bool kiemtracapsocong(LIST l)
{
  bool check = false;
  if (l.Head->Next == NULL) return check;
  if (l.Head->Next->Next == NULL )
  {
    check = true;
    return check;
  }
  else
  {
    int d = l.Head->Next->info - l.Head->info;
    for (NODE *i=l.Head->Next; i->Next != NULL; i = i->Next)
    {
      if (i->info + d != i->Next->info)
      {
        check = false;
        return check;
        break;
      }
      else check = true;
    }
    return check;
  }
  
}
int main()
{
  LIST l;
  KhoiTao(l);
  while (true)
  {
    int x;
    cin >> x;
    if (x <= 0) break;
    NODE *p=KhoiTaoNODE(x);
    ThemVaoDau(l,p);
  }
  //XuatDanhSach(l); // Xuat danh sach

  cout <<"Tong gia tri danh sach = " << Tongdanhsach(l) << endl; // Tinh tong danh sach
  Sapxep(l); //Sap xep thu tu tang

  if (kiemtracapsocong(l) == true) cout << "Day la cap so cong \n";
  else cout <<"Khong phai cap so cong \n";
  
  return 0;
}

Editor is loading...