Algorytmy 9.III.2012, Laborki, ASD
[ Pobierz całość w formacie PDF ]
1. Zaimplementowac algorytm napisany w pseudokodzie (program wyszukiwania binarnegouporzadkowanego ciagu liczb (t - dana tablica, m - szukana liczba)).#include <cstdlib>#include <iostream>#include <conio.h>#include <iomanip.h>#include <math.h>#define NMAX 10using namespace std;int main(int argc, char *argv[]){int l,p,s,m,j,i,n,wynik=0,t[NMAX];cout<<"Podaj ilosc elementow ";cin>>n;for (i=0;i<n;i++){cout<<"Podaj "<< i+1<<" element: ";cin>>t[i];cout<<endl;}cout<<"Podaj liczbe: ", cin>>m;l=0;p=n-1;do{s=(l+p)/2;if (t[s]==m) wynik=1;if (t[s]<m) l=s+1; else p=s-1;}while ((l<=p)&&(!wynik));if (wynik) cout <<"Jest" << endl;else cout <<"Nie ma";cout<<endl<<endl;system("PAUSE");return EXIT_SUCCESS;}#include <cstdlib>#include <iostream>#include <conio.h>#include <math.h>#define NMAX 7using namespace std;int main(int argc, char *argv[]){int x1,x2,y1,y2;int t[NMAX] [NMAX]={{0,0,0,0,0,0,0},{0,0,1,0,1,0,0},{0,1,1,0,1,0,0},{0,0,1,1,0,0,0},{0,1,0,1,0,1,0},{0,1,0,1,1,1,0},{0,0,0,0,0,0,0}};for (int i=0;i<NMAX;i++){for (int j=0;j<NMAX;j++){cout<<t[i][j]<<" ";}cout<<endl;}cout<<endl<<endl;cout<<"Podaj wspolrzedne punktu poczatkowego "<< endl;cout<<"x1 "; cin >>x1;cout<<"x2 "; cin >>x2;cout<<"Podaj wspolrzedne punktu koncowego "<< endl;cout<<"y1 "; cin >>y1;cout<<"y2 "; cin >>y2;if (t[x1-1][y1]==1) t[x1-1][y1]=2;if (t[x1+1][y1]==1) t[x1+1][y1]=2;if (t[x1][y1-1]==1) t[x1][y1-1]=2;if (t[x1][y1+1]==1) t[x1][y1+1]=2;cout<<endl<<endl;system("PAUSE");return EXIT_SUCCESS;}Droga po macierzy#include <cstdlib>#include <iostream>#include <conio.h>#include <math.h>#define NMAX 7using namespace std;int main(int argc, char *argv[]){int flaga,n,x1,x2,y1,y2;int t[NMAX] [NMAX]={{0,0,0,0,0,0,0},{0,0,1,0,1,0,0},{0,1,1,0,1,0,0},{0,0,1,1,0,0,0},{0,1,0,1,0,1,0},{0,1,0,1,1,1,0},{0,0,0,0,0,0,0}};for (int i=0;i<NMAX;i++){for (int j=0;j<NMAX;j++){cout<<t[i][j]<<" ";}cout<<endl;}cout<<endl<<endl;cout<<"Podaj wspolrzedne punktu poczatkowego "<< endl;cout<<"x1 "; cin >>x1;cout<<"y1 "; cin >>y1;cout<<"Podaj wspolrzedne punktu koncowego "<< endl;cout<<"x2 "; cin >>x2;cout<<"y2 "; cin >>y2;if (t[x1-1][y1]==1) t[x1-1][y1]=2;if (t[x1+1][y1]==1) t[x1+1][y1]=2;if (t[x1][y1-1]==1) t[x1][y1-1]=2;if (t[x1][y1+1]==1) t[x1][y1+1]=2;for (int i=0;i<NMAX;i++){for (int j=0;j<NMAX;j++){cout<<t[i][j]<<" ";}cout<<endl;}cout<<endl;n=2;while(flaga){flaga =0;for (int i=1;i<NMAX-1;i++)for (int j=1;j<NMAX-1;j++){if(t[i][j]==n){if (t[i-1][j]==1) {t[i-1][j]=n+1; flaga=1;}if (t[i+1][j]==1) {t[i+1][j]=n+1; flaga=1;}if (t[i][j-1]==1) {t[i][j-1]=n+1; flaga=1;}if (t[i][j+1]==1) {t[i][j+1]=n+1; flaga=1;}}}n++;cout<<endl;}t[x1][y1]=1;for (int i=0;i<NMAX;i++){for (int j=0;j<NMAX;j++){cout<<t[i][j]<<" ";}cout<<endl;}cout<<endl<<endl;system("PAUSE");return EXIT_SUCCESS;}
[ Pobierz całość w formacie PDF ]