Beispiel #1
0
bool solution2(TreeNode *root, int &height){
    if(!root){
        height = 0;
        return true;
    }
    int left, right;
    if(!solution2(root->left, left) || !solution2(root->right, right) || abs(left - right) > 1) return false;
    height = max(left, right) + 1;
    return true;
}
int main(int argc, char* argv[])
{
	int p, n, c;

	printf("Enter a problem number\n");
	scanf("%d", &p);
	if (p == 1) { 
		printf("Enter a number\n");
		scanf("%d", &n);
	
		printf("Enter a larger number\n");
		scanf("%d", &c);
		printf("\n=========================\nSolution:%d\n==========================\n",solution(n,c));
	}else if (p==2) {
		printf("Enter a number\n");
		scanf("%d", &n);
		
		printf("\n=========================\nSolution:%d\n==========================\n",solution2(n));
	} else if (p==3){
		printf("Enter a radius\n");
		int r;
		scanf("%d", &r);
		int sum = 0;
		for(int i = 0; i <= (r*r)/4; i++){
			  sum += ((r*r)/((4*i)+1)) - ((r*r)/((4*i)+3));
		}
		sum = sum * 4 + 1;

		printf("\n=========================\nNumber of ints::%d\n==========================\n",sum);


	}


	return 0;
}
Beispiel #3
0
      /* 'Data'   Matrix of data containing observation data in rows, one
       *          row per observation and complying with this format:
       *                    x y z P
       *          Where x,y,z are satellite coordinates in an ECEF system
       *          and P is pseudorange (corrected as much as possible,
       *          specially from satellite clock errors), all expresed
       *          in meters.
       *
       * 'X'      Vector of position solution, in meters. There may be
       *          another solution, that may be accessed with vector
       *          "SecondSolution" if "ChooseOne" is set to "false".
       *
       * Return values:
       *  0  Ok
       * -1  Not enough good data
       * -2  Singular problem
       */
   int Bancroft::Compute( Matrix<double>& Data,
                          Vector<double>& X )
      throw(Exception)
   {

      try
      {

         int N = Data.rows();
         Matrix<double> B(0,4);     // Working matrix

            // Let's test the input data
         if( testInput )
         {

            double satRadius = 0.0;

               // Check each row of B Matrix
            for( int i=0; i < N; i++ )
            {
                  // If Data(i,3) -> Pseudorange is NOT between the allowed
                  // range, then drop line immediately
               if( !( (Data(i,3) >= minPRange) && (Data(i,3) <= maxPRange) ) )
               {
                  continue;
               }

                  // Let's compute distance between Earth center and
                  // satellite position
               satRadius = RSS(Data(i,0), Data(i,1) , Data(i,2));

                  // If satRadius is NOT between the allowed range, then drop
                  // line immediately
               if( !( (satRadius >= minRadius) && (satRadius <= maxRadius) ) )
               {
                  continue;
               }

                  // If everything is ok so far, then extract the good
                  // data row and add it to working matrix
               MatrixRowSlice<double> goodRow(Data,i);
               B = B && goodRow;              

            }

               // Let's redefine "N" and check if we have enough data rows
               // left in a single step
            if( (N = B.rows()) < 4 )
            {
               return -1;  // We need at least 4 data rows
            }

         }  // End of 'if( testInput )...'
         else
         {
               // No input filtering. Working matrix (B) and
               // input matrix (Data) are equal
            B = Data;
         }


         Matrix<double> BT=transpose(B);
         Matrix<double> BTBI(4,4), M(4,4,0.0);
         Vector<double> aux(4), alpha(N), solution1(4), solution2(4);

            // Temporary storage for BT*B. It will be inverted later
         BTBI = BT * B;

            // Let's try to invert BTB matrix
         try
         {
            BTBI = inverseChol( BTBI ); 
         }
         catch(...)
         {
            return -2;
         }

            // Now, let's compute alpha vector
         for( int i=0; i < N; i++ )
         {
               // First, fill auxiliar vector with corresponding satellite
               // position and pseudorange
            aux(0) = B(i,0);
            aux(1) = B(i,1);
            aux(2) = B(i,2);
            aux(3) = B(i,3);
            alpha(i) = 0.5 * Minkowski(aux, aux);
         }

         Vector<double> tau(N,1.0), BTBIBTtau(4), BTBIBTalpha(4);

         BTBIBTtau = BTBI * BT * tau;
         BTBIBTalpha = BTBI * BT * alpha;

            // Now, let's find the coeficients of the second order-equation
         double a(Minkowski(BTBIBTtau, BTBIBTtau));
         double b(2.0 * (Minkowski(BTBIBTtau, BTBIBTalpha) - 1.0));
         double c(Minkowski(BTBIBTalpha, BTBIBTalpha));

            // Calculate discriminant and exit if negative
         double discriminant = b*b - 4.0 * a * c;
         if (discriminant < 0.0)
         {
            return -2;
         }

            // Find possible DELTA values
         double DELTA1 = ( -b + SQRT(discriminant) ) / ( 2.0 * a );
         double DELTA2 = ( -b - SQRT(discriminant) ) / ( 2.0 * a );

            // We need to define M matrix
         M(0,0) = 1.0;
         M(1,1) = 1.0;
         M(2,2) = 1.0;
         M(3,3) = - 1.0;

            // Find possible position solutions with their implicit radii
         solution1 = M *  BTBI * ( BT * DELTA1 * tau + BT * alpha );
         double radius1(RSS(solution1(0), solution1(1), solution1(2)));

         solution2 = M *  BTBI * ( BT * DELTA2 * tau + BT * alpha );
         double radius2(RSS(solution2(0), solution2(1), solution2(2)));

            // Let's choose the right solution
         if ( ChooseOne )
         {
            if ( ABS(CloseTo-radius1) < ABS(CloseTo-radius2) )
            {
               X = solution1;
            }
            else
            {
               X = solution2;
            }
         }
         else
         {
               // Both solutions will be reported
            X = solution1;
            SecondSolution = solution2;
         }
     
         return 0;

      }  // end of first "try"
      catch(Exception& e)
      {
         GPSTK_RETHROW(e);
      }
   }  // end Bancroft::Compute()
int solution(int N, int T, int value[50]) {
    if (rand() % 2) return solution1(N, T, value);
    else    return solution2(N, T, value);
}
    void TestApplyToLinearSystem3Unknowns()
    {
        const int SIZE = 10;

        DistributedVectorFactory factory(SIZE);
        Vec template_vec = factory.CreateVec(3);
        LinearSystem linear_system(template_vec, 3*SIZE);
        PetscTools::Destroy(template_vec);

        for (int i = 0; i < 3*SIZE; i++)
        {
            for (int j = 0; j < 3*SIZE; j++)
            {
                // LHS matrix is all 1s
                linear_system.SetMatrixElement(i,j,1);
            }
            // RHS vector is all 2s
            linear_system.SetRhsVectorElement(i,2);
        }

        linear_system.AssembleIntermediateLinearSystem();

        Node<3>* nodes_array[SIZE];
        BoundaryConditionsContainer<3,3,3> bcc33;

        // Apply dirichlet boundary conditions to all but last node
        for (int i = 0; i < SIZE-1; i++)
        {
            nodes_array[i] = new Node<3>(i,true);

            ConstBoundaryCondition<3>* p_boundary_condition0 =
                new ConstBoundaryCondition<3>(-1);

            ConstBoundaryCondition<3>* p_boundary_condition1 =
                new ConstBoundaryCondition<3>(-2);

            ConstBoundaryCondition<3>* p_boundary_condition2 =
                new ConstBoundaryCondition<3>( 0);

            bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition0, 0);
            bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition1, 1);
            bcc33.AddDirichletBoundaryCondition(nodes_array[i], p_boundary_condition2, 2);
        }
        bcc33.ApplyDirichletToLinearProblem(linear_system);

        linear_system.AssembleFinalLinearSystem();

        Vec solution = linear_system.Solve();

        DistributedVector d_solution = factory.CreateDistributedVector(solution);
        DistributedVector::Stripe solution0(d_solution,0);
        DistributedVector::Stripe solution1(d_solution,1);
        DistributedVector::Stripe solution2(d_solution,2);

        for (DistributedVector::Iterator index = d_solution.Begin();
             index != d_solution.End();
             ++index)
        {
            if (index.Global!=SIZE-1)
            {
                TS_ASSERT_DELTA(solution0[index], -1.0, 0.000001);
                TS_ASSERT_DELTA(solution1[index], -2.0, 0.000001);
                TS_ASSERT_DELTA(solution2[index],  0.0, 0.000001);
            }

        }
        for (int i = 0; i < SIZE-1; i++)
        {
            delete nodes_array[i];
        }
        PetscTools::Destroy(solution);
    }
int solution(int n) {
    if (rand() % 2) return solution1(n, n);
    else            return solution2(n, n);
}
Beispiel #7
0
int maxDepth(TreeNode* root) {
    //return solution1(root);   //DFS
    return solution2(root);     //BFS
}
	string convert(string s, int numRows) {
		return solution2(s, numRows);
	}
Beispiel #9
0
string convertToTitle(int n) {
    //return solution1(n);    //recursive
    return solution2(n);    //iterative
}