Ma'lumotlar turlari, ularni e‘lon qilish
Download 0.84 Mb.
|
MTA mustaqil ish
- Bu sahifa navigatsiya:
- Input Output
- for(it=l2.begin();it!=l2.end();it++){
- Rekursiv algoritmlar va ularning funksiyalari
Ma'lumotlar turlari, ularni e‘lon qilish. #include using namespace std; int main(){ string a,b; getline(cin,a); cin>>b; cout<} #include using namespace std; int main(){ int a; float b; double c; cin>>a>>b>>c; cout<} Statik MT. Algoritm yozish usullari. Algoritm sinflari Savol Bizga ma'lumki, tub sonlar aniq ikkita musbat bo'luvchiga ega bo'lgan musbat butun sonlardir. Xuddi shunday, t ning aniq uchta musbat bo'luvchisi bo'lsa, t musbat sonini T-tutqich deb ataymiz. Sizga n ta musbat sondan iborat massiv berilgan. Ular orasida T-tutqich sonlar sonini aniqlang. Kiritish Birinchi qatorda massivda nechta raqam borligini ko'rsatadigan bitta musbat butun son, n (1 ≤ n ≤ 105) mavjud. Keyingi qatorda n ta boʻshliq bilan ajratilgan xi (1 ≤ xi ≤ 1012) sonlar mavjud. Chiqish Bitta raqamni chop eting: T-Primes raqamlari. Namuna
Eslatma: 4 ning aniq uchta bo'luvchisi bor - 1, 2 va 4. Bu T-Prime. 5 ning ikkita bo'luvchisi bor (1 va 5). Bu T-Prime emas. 6 ning to'rtta bo'luvchisi bor (1, 2, 3, 6). Bu T-Prime emas. #include using namespace std ; int main () { int n,m,q,t=0; cin>>n; int a[n]; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=n;i++){ m=a[i]; q=0; for(int j=1;j<=m;j++){ if(a[i]%j==0){ q++; } } if(q==3) t++; } cout< vector — dinamik massiv doir misol ishlash #include #include using namespace std; int main() { vector vector iter = num.begin(); cout << "num[0] = " << *iter << endl; iter = num.begin() + 2; cout << "num[2] = " << *iter < cout << "num[4] = " << *iter; return 0; } Yarimstatik MT. Algoritmlarning xossalari Dinamik MT. 1-misol #include using namespace std; void checkType(int arr[], int n) { if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1]) cout << "o'suvchi"; else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1]) cout << "kamayuvchi"; else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1]) cout << "o'sib kamayuvchi"; else cout << "kamayib o'suvchi"; } int main() { int arr[] = { 1, 2, 3, 2 }; int n = sizeof(arr) / sizeof(arr[0]); checkType(arr, n); return 0; } 3-misol #include using namespace std; template for (int i=0; i if(a[i]==a[k]) return true ; else { return false; break; } } } int main(){ int n; cin>>n; int a[n+1]; for (int i=0; i } if (osuvchi(n,a)) cout<<"True"; else cout<<"False"; } 4-misol Show_array funksiyasi shablonidan foydalanib massiv elementlarini chiqarish. #include using namespace std; void shArray(int b[],int j){ cout<<" elementlarni chiqarish::\n"; for(int i=0;i } int main(){ int n; cin>>n; int a[n]; for(int i=0;i } shArray(a,n); return 0; } 5-misol Ma’lum bir ishni bajaruvchi bitta va bir nechta konstruktordan iborat sinflar yaratish va ularni qo’llashga doir misollar #include using namespace std; class royhat{ string n,a; int t; public: royhat(){ cout<<"Ismi:: "; cin>>n; cout<<"telnomer::"; cin>>t; cout<<"Address::"; cin>>a; } void print(){ cout< }; int main(){ royhat j; j.print(); Xalqasimon bog‘langan ro’yhatlar. #include #include using namespace std; int main(){ list list int a,b,c; cin>>a>>b; for(int i=1;i<=a;i++){ cin>>c; l1.push_back(c); } for(int i=1;i<=b;i++){ cin>>c; l2.push_back(c); } c=1; for(it=l1.begin();it!=l1.end();it++){ if(c==3){ l.push_back(*it); c=0; } c++; } c=1; for(it=l2.begin();it!=l2.end();it++){ if(c==3){ l.push_back(*it); c=0; } c++; } for(it=l.begin();it!=l.end();it++){ cout<<*it<<" "; } } 2 ta ro’yhatning bir xil qiymatli elementlaridan yangi halqasimon ro’yhat yarating. #include #include using namespace std; int main(){ list list list int a,b,c; cin>>a>>b; for(int i=1;i<=a;i++){ cin>>c; l1.push_back(c); } for(int i=1;i<=b;i++){ cin>>c; l2.push_back(c); } for(i=l1.begin();i!=l1.end();i++){ for(it=l2.begin();it!=l2.end();it++){ if(*i==*it){ l.push_back(*it);} }} for(it=l.begin();it!=l.end();it++){ cout<<*it<<" "; } } . Savol: Kiritilgan ismning harflarini alfavit bo‘yicha tartibga keltiring. #include #include using namespace std ; int main () { string a; getline(cin,a); for(int i=0;i } } cout<return 0 ;} Binar daraxtlar bilan ishlash. Berilgan binar daraxt muvozanatlanganmi yoki yo‟qligini tekshiring #include #include using namespace std; class node{ public: int info; node *left; node *right; }; int k=0,Flag=1; int height(node *tree){ int h1,h2; if (tree==NULL) return (-1); else { h1 = height(tree->left); h2 = height(tree->right); if (h1>h2) return (1 + h1); else return (1 + h2); } } void vizual(node *tree,int l) { int i; if(tree!=NULL) { vizual(tree->right,l+1); for (i=1; i<=l; i++) cout<<" "; cout< } } int AVLtree (node *tree){ int t; if (tree!=NULL){ t = height (tree->left) - height (tree->right); if ((t<-1) || (t>1)) { Flag = 0; return Flag; } AVLtree (tree->left); AVLtree (tree->right); } } int GetFlag(){return Flag;} int main() { int n,key,s; node *tree=NULL,*next=NULL; cout<<"n="; cin>>n; int arr[n]; for(int i=0; i node *last=new node; cin>>s; p->info=s; p->left=NULL; p->right=NULL; if(i==0){tree=p; next=tree; continue; } next=tree; while(1) { last=next; if(p->info else next=next->right; if(next==NULL)break; } if(p->info else last->right=p;} cout< vizual(tree,0); AVLtree(tree); if(GetFlag()) cout<<"ha,muvozanatlangan daraxt"; else cout<<"yo’q, muvozanatlanmagan daraxt";cout< } Dastur natijasi Binar to‘plamlar shakldagi ma’lumotlar Qidiruv algoritmlarining qiyosiy tahlili 1 1 2 3 5 8 13 21 … Fibonachchi raqamlari bo'lgan butun sonlar massivi berilgan. Berilgan raqamlar to'plamidagi Fibonachchi raqamlarini hisoblang.
#include using namespace std; int binsearch(int a[], int b[],int n,int l, int r) { int x=0,count = 0; for (int i=0; i int r1 = r,l1 = l; while (r1-l1 > 1) { int middle = (l1+r1) / 2; if (a[middle] > x) r1 = middle; else l1 = middle; } if (a[l1]==x) count++; } return count; } int main(){ int n; cin>>n; int n1=0,n2 = 1,n3; int a[1000]; a[0] = 0; for (int i=1; i<1000; i++){ n3 = n1+n2; n1 = n2; n2 = n3; a[i] = n3; } int i1=0,x,k=0,b[n+1],c[1000]; for (int i=0; i if (iZ+n-1) x = b[i]; } for (int i=0; i i1++; if (a[i]==x) break; } cout< Ekranga berilgan uchlar orasidagi qisqa masofani va uning og’irligini chiqaruvchi; Quyidagi graf asosida tekshirib ko’ruvchi: #include using namespace std; const int maxV=1000; int i, j, n; int GR[maxV][maxV]; void FU(int D[][maxV], int V) { int k; for (i=0; i if (D[i][k]+D[k][j] for (i=0; i for (j=0; j int main() { setlocale(LC_ALL, "Rus"); cout<<"Graf o'lchami > "; cin>>n; cout<<"Grafni kiririting:\n"; for (i=0; i cout<<"GR["< "; cin>>GR[i][j]; } cout<<"Eng qisqa yo'l graf matritsasi':"< system("pause>>void"); } Stek eng katta elementi topilsin va undan keyin 0 joylashtirilsin. #include #include using namespace std; int main(){ int n,x; cin>>n; stack for(int i=0;i a.push(x); } int d=a.top(); for(int i=0;i d=a.top(); } b.push(a.top()); a.pop(); } for(int i=0;i c.push(0); c.push(b.top()); } else c.push(b.top()); b.pop(); } for(int i=0;i<=n;i++){ cout< } return 0; } Stek eng kichik elementi o„chirilsin. #include #include using namespace std; int main(){ int n,m; cin>>n; stack for(int i=0;i a.push(m); } m=a.top(); int q=0; for(int i=0;i m=a.top(); } b.push(a.top()); a.pop(); } for(int i=0;i b.pop(); } else { c.push(b.top()); b.pop(); q++; } } for(int i=0;icout< }} Stekda birinchi elementga teng barcha elementlar o„chirilsin. #include #include using namespace std; int main(){ int n,m; cin>>n; stack for(int i=0;i a.push(m); } for(int i=0;i m=a.top(); } b.push(a.top()); a.pop(); } int q=0; for(int i=0;i b.pop(); } else { c.push(b.top()); b.pop(); q++ ;} } for(int i=0;icout< } } Stek oxirgi elementiga teng barcha elementlar o„chirilsin. #include #include using namespace std; int main(){ int n,m; cin>>n; stack for(int i=0;i a.push(m); } m=a.top(); int q=0; for(int i=0;i a.pop(); } else { c.push(a.top()); a.pop(); q++ ;} } for(int i=0;icout< }} Navbatda birinchi elementga teng barcha elementlar o’chirilsin. #include using namespace std; int main(){ queue int n,m,q=0; cin>>n; for(int i=0;i a.push(m); } int c=a.front(); for(int i=0;i a.pop(); } else { b.push(a.front()); a.pop(); q++; }} for(int i=0;icout< }} Navbatda oxirgi elementga teng barcha elementlar o’chirilsin. #include #include using namespace std; int main(){ int n,m,q=0; cin>>n; queue for(int i=0;i a.push(m); } for(int i=0;i a.pop(); } else{ b.push(a.front()); a.pop(); q++; } } for(int i=0;icout< } } Navbat eng katta elementi o’chirilsin. #include #include using namespace std; int main(){ int n,m,q=0; cin>>n; queue for(int i=0;i a.push(m); } int c=a.front(); for(int i=0;i } b.push(a.front()); a.pop(); } for(int i=0;i b.pop(); } else { a.push(b.front()); b.pop(); q++; }} for(int i=0;icout< } } Navbat eng kichik elementi topilsin va uning o’rniga 0 joylashtirilsin. #include using namespace std; int main(){ int n,m,q=0; cin>>n; queue for(int i=0;i a.push(m); } int c=a.front(); for(int i=0;i c=a.front(); } b.push(a.front()); a.pop(); } for(int i=0;i a.push(0); b.pop(); q++; } else { a.push(b.front()); b.pop(); q++; }} for(int i=0;icout< } } Berilgan so’zning unli harflarini dekning chap tomonidan, undoshlarini o’ng tomondan kiriting. Dekning toq elementlaridan navbat, juft elementlaridan stek hosil qiling. #include #include #include #include using namespace std; int main(){ int n,m,q=0,d=0; cin>>n; deque queue stack for(int i=0;i a.push_back(m); } for(int i=0;i b.push(a.front()); q++; } else{ c.push(a.front()); d++; } a.pop_front(); } cout<<"Navbat elemetlari::\n"; for(int i=0;icout< } cout<<"\nStack elemetlari::\n"; for(int i=0;i } } Dekdagi manfiy sonlarni o’chiring. #include #include using namespace std; int main(){ int n,m,q=0,d=0; cin>>n; deque for(int i=0;i a.push_back(m); } for(int i=0;i a.pop_front(); } else{ cout< } }} Dekni o’rtasiga “dek” so’zini kiriting. #include #include using namespace std; int main(){ int n,m; cin>>n; deque for(int i=0;i a.push_back(m); } for(int i=0;i cout<<" dek "; cout< }} Download 0.84 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling