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
№
|
Input
|
Output
|
1
|
3
4 5 6
|
1
|
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<return 0 ;}
vector — dinamik massiv doir misol ishlash
#include
#include
using namespace std;
int main() {
vector num {1, 2, 3, 4, 5};
vector::iterator iter;
iter = num.begin();
cout << "num[0] = " << *iter << endl;
iter = num.begin() + 2;
cout << "num[2] = " << *iter <iter = num.end() - 1;
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 osuvchi(x n, x a[]){
for (int i=0; i int k=i+1;
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; icin>>a[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;icout<}
}
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;icin>>a[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(){
listl1,l2,l;
list::iterator it;
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(){
listl1,l2,l;
list::iterator it;
list::iterator i;
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<<" ";
}
}
.
Rekursiv algoritmlar va ularning funksiyalari
#include
#include
using namespace std;
int A(int n, int m){
if(n==0)
return m+1;
else if(m==0&&n!=0)
return A(n-1,1);
else if(n>0&&m>0)
return A(n-1,A(n,m-1));
else
return 0 ;
}
int main(){
int m,n;
cin>>n>>m;
cout<}
|
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 for(int j=0;j if(int(a[i]) swap(a[i],a[j]);
}
}
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<info< vizual(tree->left,l+1);
}
}
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; inode *p=new node;
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->infoinfo)next=next->left;
else next=next->right;
if(next==NULL)break; }
if(p->infoinfo)last->left=p;
else last->right=p;}
cout<cout<<"\nbinar daraxt:\n";
vizual(tree,0);
AVLtree(tree);
if(GetFlag()) cout<<"ha,muvozanatlangan daraxt"; else cout<<"yo’q, muvozanatlanmagan daraxt";cout<getch();
}
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.
№
|
Input
|
Output
|
1
|
5
1 3 5 6 13
|
4
|
#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; ix = b[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; icin>>b[i];
if (iZ+n-1) x = b[i];
}
for (int i=0; ic[i1] = b[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; ifor (k=0; kfor (i=0; ifor (j=0; jif (D[i][k] && D[k][j] && i!=j)
if (D[i][k]+D[k][j]D[i][j]=D[i][k]+D[k][j];}}}
for (i=0; i{
for (j=0; j cout<} }
int main()
{
setlocale(LC_ALL, "Rus");
cout<<"Graf o'lchami > "; cin>>n;
cout<<"Grafni kiririting:\n";
for (i=0; ifor (j=0; j{
cout<<"GR["< ";
cin>>GR[i][j];
}
cout<<"Eng qisqa yo'l graf matritsasi':"<FU(GR, n);
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;
stacka,b,c;
for(int i=0;icin>>x;
a.push(x);
}
int d=a.top();
for(int i=0;iif(d<=a.top()){
d=a.top();
}
b.push(a.top());
a.pop();
}
for(int i=0;iif(d==b.top()){
c.push(0);
c.push(b.top());
}
else c.push(b.top());
b.pop();
}
for(int i=0;i<=n;i++){
cout<c.pop();
}
return 0;
}
Stek eng kichik elementi o„chirilsin.
#include
#include
using namespace std;
int main(){
int n,m;
cin>>n;
stacka,b,c;
for(int i=0;icin>>m;
a.push(m);
}
m=a.top();
int q=0;
for(int i=0;iif(m>=a.top()){
m=a.top();
}
b.push(a.top()); a.pop(); }
for(int i=0;iif(m==b.top()){
b.pop();
}
else {
c.push(b.top()); b.pop(); q++;
}
}
for(int i=0;icout<c.pop();
}}
Stekda birinchi elementga teng barcha elementlar o„chirilsin.
#include
#include
using namespace std;
int main(){
int n,m;
cin>>n;
stacka,b,c;
for(int i=0;icin>>m;
a.push(m);
}
for(int i=0;iif(i==n-1){
m=a.top();
}
b.push(a.top());
a.pop();
}
int q=0;
for(int i=0;iif(m==b.top()){
b.pop();
}
else {
c.push(b.top()); b.pop(); q++ ;}
}
for(int i=0;icout<c.pop();
}
}
Stek oxirgi elementiga teng barcha elementlar o„chirilsin.
#include
#include
using namespace std;
int main(){
int n,m;
cin>>n;
stacka,c;
for(int i=0;icin>>m;
a.push(m);
}
m=a.top();
int q=0;
for(int i=0;iif(m==a.top()){
a.pop();
}
else {
c.push(a.top()); a.pop(); q++ ;}
}
for(int i=0;icout<c.pop();
}}
Navbatda birinchi elementga teng barcha elementlar o’chirilsin.
#include
using namespace std;
int main(){
queuea,b;
int n,m,q=0;
cin>>n;
for(int i=0;icin>>m;
a.push(m);
}
int c=a.front();
for(int i=0;iif(a.front()==c){
a.pop();
}
else {
b.push(a.front());
a.pop();
q++;
}}
for(int i=0;icout<b.pop();
}}
Navbatda oxirgi elementga teng barcha elementlar o’chirilsin.
#include
#include
using namespace std;
int main(){
int n,m,q=0;
cin>>n;
queuea,b;
for(int i=0;icin>>m;
a.push(m);
}
for(int i=0;iif(m==a.front()){
a.pop();
}
else{
b.push(a.front());
a.pop();
q++;
}
}
for(int i=0;icout<b.pop();
}
}
Navbat eng katta elementi o’chirilsin.
#include
#include
using namespace std;
int main(){
int n,m,q=0;
cin>>n;
queuea,b;
for(int i=0;icin>>m;
a.push(m);
}
int c=a.front();
for(int i=0;iif(cc=a.front();
}
b.push(a.front());
a.pop();
}
for(int i=0;iif(c==b.front()){
b.pop();
}
else {
a.push(b.front());
b.pop();
q++;
}}
for(int i=0;icout<a.pop();
}
}
Navbat eng kichik elementi topilsin va uning o’rniga 0 joylashtirilsin.
#include
using namespace std;
int main(){
int n,m,q=0;
cin>>n;
queuea,b;
for(int i=0;icin>>m;
a.push(m);
}
int c=a.front();
for(int i=0;iif(c>a.front()){
c=a.front();
}
b.push(a.front());
a.pop();
}
for(int i=0;iif(c==b.front()){
a.push(0);
b.pop();
q++;
}
else {
a.push(b.front());
b.pop();
q++;
}}
for(int i=0;icout<a.pop();
}
}
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;
dequea;
queueb;
stackc;
for(int i=0;icin>>m;
a.push_back(m);
}
for(int i=0;iif(a.front()%2!=0){
b.push(a.front());
q++;
}
else{
c.push(a.front());
d++;
}
a.pop_front();
}
cout<<"Navbat elemetlari::\n";
for(int i=0;icout<b.pop();
}
cout<<"\nStack elemetlari::\n";
for(int i=0;icout<c.pop();
}
}
Dekdagi manfiy sonlarni o’chiring.
#include
#include
using namespace std;
int main(){
int n,m,q=0,d=0;
cin>>n;
dequea;
for(int i=0;icin>>m;
a.push_back(m);
}
for(int i=0;iif(a.front()<0){
a.pop_front();
}
else{
cout<a.pop_front();
}
}}
Dekni o’rtasiga “dek” so’zini kiriting.
#include
#include
using namespace std;
int main(){
int n,m;
cin>>n;
dequea;
for(int i=0;icin>>m;
a.push_back(m);
}
for(int i=0;iif(i==n/2+1)
cout<<" dek ";
cout<a.pop_front();
}}
Do'stlaringiz bilan baham: |