Wyszukiwanie lidera w zbiorze liczb całkowitych C++

[code=cpp] #include #include using namespace std; int lider(int tab[], int n); int main() { int x[100],n,i,y; cout«“Podaj liczbe elementow: “; cin»n; for(i=1;i<=n;i++) { cout«“Podaj element o indeksie: “« i «” : “; cin»x[i]; } y=lider(x,n); if (y==-1) cout«“Brak lidera”«endl; else { cout«“Liderem jest “« y«endl; for(i=1;i<=n;i++) { if (x[i]!=y) cout«“Liderem nie jest: “«x[i]«endl; } } system(“PAUSE”); return 0; } int lider(int tab[], int n) { int L,c,i; L=tab[1]; c=1; for(i=2; i<=n;i++) if (c==0) { L=tab[i]; c=1; } else if (tab[i]==L) c++; else c–; if (c>0) {c=0; for (i=1; i<=n; i++) if (tab[i]==L)c++; } if (c>n/2) return L; else return -1; }

[code=cpp] #include #include

using namespace std; int lider(int tab[], int n); int main()

{ int x[100],n,i,y; cout«“Podaj liczbe elementow: “; cin»n; for(i=1;i<=n;i++) { cout«“Podaj element o indeksie: “« i «” : “; cin»x[i]; } y=lider(x,n); if (y==-1) cout«“Brak lidera”«endl; else { cout«“Liderem jest “« y«endl; for(i=1;i<=n;i++) { if (x[i]!=y) cout«“Liderem nie jest: “«x[i]«endl; } } system(“PAUSE”); return 0; }

int lider(int tab[], int n) { int L,c,i; L=tab[1]; c=1; for(i=2; i<=n;i++) if (c==0) { L=tab[i]; c=1; } else if (tab[i]==L) c++; else c–; if (c>0) {c=0; for (i=1; i<=n; i++) if (tab[i]==L)c++; } if (c>n/2) return L; else return -1; }

[/code]