Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. int S (int *A, int n) {
  6.     int i = 0, cont = 0;
  7.     bool giusto = false;
  8.    
  9.    
  10.     while (!giusto && A[i] != -2){
  11.        
  12.         if (A[i] == -1){
  13.            
  14.             cont++;
  15.            
  16.             if (cont == n){
  17.                 giusto = true;
  18.             }  
  19.         }
  20.        
  21.         i++;
  22.     }
  23.    
  24.     if(!giusto || A[i] == -2) i = -2;
  25.    
  26.     return i;
  27. }  
  28.  
  29.  
  30.  
  31. int main(){
  32.    int X[400];
  33.    
  34.    cin >> X[0];
  35.    
  36.    for(int i=1; i<400 && X[i-1]!=-2; i++)
  37.    cin >> X[i];
  38.    
  39.     int n;
  40.     cin >> n;
  41.    
  42.     cout<<"start"<<endl;
  43.     int b = S(X,n);
  44.    
  45.     if(b == -2)
  46.      cout<<"sottosequenza "<<n<<" non presente"<<endl;
  47.     else
  48.      cout<<"inizio sottosequenza "<<n<<" indice="<<b<<endl;
  49.      
  50.     cout<<"end"<<endl;
  51.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement