int calculateSum(TreeNode* node, int sum, int tmpSum) {
     if (node == nullptr) {
         return 0;
     }
     int cnt = 0;
     tmpSum += node->val;
     if (tmpSum == sum) {
         cnt += 1;
     }
     cnt += calculateSum(node->left, sum, tmpSum);
     cnt += calculateSum(node->right, sum, tmpSum);
     return cnt;
 }
 void calculateSum(TreeNode *root, int &csum, int &maxSum) {
     if (root == NULL) {
         csum = 0;
         return;
     }
     
     int lsum = 0;
     int rsum = 0;
     calculateSum(root->left, lsum, maxSum);
     calculateSum(root->right, rsum, maxSum);
     
     csum = max(root->val, max(root->val + lsum, root->val + rsum));
     maxSum = max(maxSum, max(csum, root->val + lsum + rsum));
 }
 int preorderTraversal(TreeNode* node, int sum) {
     if (node == nullptr) {
         return 0;
     }
     int cnt = calculateSum(node, sum, 0);
     cnt += preorderTraversal(node->left, sum);
     cnt += preorderTraversal(node->right, sum);
     return cnt;
 }
Exemple #4
0
    void process(InputArray _src, OutputArray _dst)
    {
        Mat src = _src.getMat();
        CV_Assert(!src.empty());
        _dst.create(src.size(), CV_32FC3);
        Mat img = _dst.getMat();
        Ptr<Tonemap> linear = createTonemap(1.0f);
        linear->process(src, img);

        Mat gray_img;
        cvtColor(img, gray_img, COLOR_RGB2GRAY);
        Mat log_img;
        log(gray_img, log_img);

        std::vector<Mat> x_contrast, y_contrast;
        getContrast(log_img, x_contrast, y_contrast);

        for(size_t i = 0; i < x_contrast.size(); i++) {
            mapContrast(x_contrast[i]);
            mapContrast(y_contrast[i]);
        }

        Mat right(src.size(), CV_32F);
        calculateSum(x_contrast, y_contrast, right);

        Mat p, r, product, x = log_img;
        calculateProduct(x, r);
        r = right - r;
        r.copyTo(p);

        const float target_error = 1e-3f;
        float target_norm = static_cast<float>(right.dot(right)) * powf(target_error, 2.0f);
        int max_iterations = 100;
        float rr = static_cast<float>(r.dot(r));

        for(int i = 0; i < max_iterations; i++)
        {
            calculateProduct(p, product);
            float alpha = rr / static_cast<float>(p.dot(product));

            r -= alpha * product;
            x += alpha * p;

            float new_rr = static_cast<float>(r.dot(r));
            p = r + (new_rr / rr) * p;
            rr = new_rr;

            if(rr < target_norm) {
                break;
            }
        }
        exp(x, x);
        mapLuminance(img, img, gray_img, x, saturation);

        linear = createTonemap(gamma);
        linear->process(img, img);
    }
long calculateSum(int a[], int n) {
   static long sum = 0;
 
   if (n == 0)
      return sum;
 
   sum = sum + a[n-1];
 
   return calculateSum(a, --n);
}
int main()
{
   int n, c, array[100];
   long result;
 
   scanf("%d", &n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   result = calculateSum(array, n);
 
   printf("Sum = %ld\n", result);
 
   return 0;
}
bool SumShaCalculator::compareCheckSum() const
{
    if(!existsCheckSum())
    {
        return false;
    }

    QSharedPointer<CheckSumIfc> stored = theSumFileManager->readCheckSum(pFile);
    if(stored.isNull())
    {
        return false;
    }

    QFile f(theInputPath+"/"+pFile->getSubPath());
    if(!f.exists())
    {
        return false;
    }

    MemoryStoredCheckSum actual(calculateSum(f, theAlgorithm));

    return stored->compare(&actual);
}
bool SumShaCalculator::createCheckSum(bool addToCheckSumFile)
{
    QFile f(theInputPath+"/"+pFile->getSubPath());
    if(!f.exists())
    {
        return false;
    }

    QSharedPointer<CheckSumIfc> actual(new MemoryStoredCheckSum(calculateSum(f, theAlgorithm)));

    if(addToCheckSumFile)
    {
        if(!theSumFileManager->startCheckSumWrite(pFile, actual) == SumFileManagerIfc::FILE_FOUND)
        {
            return false;
        }
        if(!theSumFileManager->endCheckSumWrite(pFile) == SumFileManagerIfc::FILE_FOUND)
        {
            return false;
        }
    }

    return true;
}
Exemple #9
0
int main(void)
{
	calculateSum(4, 5, 1);
	calculateSum(3, 2, 0);
}
int main (void)
{
	FILE *inputStream = NULL,			/* File pointer for input file */
		 *outputStream = NULL;			/* File pointer for output file */

	int studentId1 = 0,
		studentId2 = 0,
		studentId3 = 0,
		studentId4 = 0,
		studentId5 = 0;					/* 5 student IDs */

	int studentClassStanding1 = 0,
		studentClassStanding2 = 0,
		studentClassStanding3 = 0,
		studentClassStanding4 = 0,
		studentClassStanding5 = 0;		/* 5 student class standings */

	double studentGpa1 = 0.0,
		   studentGpa2 = 0.0,
		   studentGpa3 = 0.0,
		   studentGpa4 = 0.0,
		   studentGpa5 = 0.0;			/* 5 student GPAs */

	double studentAge1 = 0.0,
		   studentAge2 = 0.0,
		   studentAge3 = 0.0,
		   studentAge4 = 0.0,
		   studentAge5 = 0.0;			/* 5 student ages */

	double sumGpa = 0.0,				/* sum of the 5 student's GPA */
		   sumClassStanding = 0.0,		/* sum of the 5 student's class standing */
		   sumAge = 0.0;				/* sum of the 5 student's ages */

	double meanGpa = 0.0,				/* mean of the 5 student's GPA */
		   meanClassStanding = 0.0,		/* mean of the 5 student's class standing */
		   meanAge = 0.0;				/* mean of the 5 student's ages */

	double deviationGpa1 = 0.0,		
		   deviationGpa2 = 0.0,
		   deviationGpa3 = 0.0,
		   deviationGpa4 = 0.0,
		   deviationGpa5 = 0.0;			/* deviation of each student's GPA */

	double varianceGpa = 0.0,			/* variance of the 5 student's GPA */
		   standardDeviationGpa = 0.0,	/* standard variance of the 5 student's GPA */
		   maxStudentGpa = 0.0,			/* maximum number out of the 5 student's GPA */
		   minStudentGpa = 0.0;			/* minimum number out of the 5 student's GPA */


	/* Opens an input file "input.dat" for reading; */
	inputStream = openFileStream (INPUT_FILE, INPUT_FILE_MODE);

    /* Opens an output file "output.dat" for writing; */
	outputStream = openFileStream (OUTPUT_FILE, OUTPUT_FILE_MODE);

	/* Checks the condition if files were successfully opened.
	 * If either file didn't open, then an error message is prompted,
	 * otherwise program will continue to execute other operations. */
	if (inputStream == NULL || outputStream == NULL)
	{
		/* Files were not successfully opened and prompts user of the error.
		 * Program will not continue to execute the rest of the program. */
		printf ("Error: Not able to open files!\n");
	}
	else
	{
		/* Reads five records from the input file (input.dat); */
	
		/* Student 1 */
		studentId1 = readInteger (inputStream);
		studentGpa1 = readDouble (inputStream);
		studentClassStanding1 = readInteger (inputStream);
		studentAge1 = readDouble (inputStream);

		/* Student 2 */
		studentId2 = readInteger (inputStream);
		studentGpa2 = readDouble (inputStream);
		studentClassStanding2 = readInteger (inputStream);
		studentAge2 = readDouble (inputStream);

		/* Student 3 */
		studentId3 = readInteger (inputStream);
		studentGpa3 = readDouble (inputStream);
		studentClassStanding3 = readInteger (inputStream);
		studentAge3 = readDouble (inputStream);

		/* Student 4 */
		studentId4 = readInteger (inputStream);
		studentGpa4 = readDouble (inputStream);
		studentClassStanding4 = readInteger (inputStream);
		studentAge4 = readDouble (inputStream);

		/* Student 5 */
		studentId5 = readInteger (inputStream);
		studentGpa5 = readDouble (inputStream);
		studentClassStanding5 = readInteger (inputStream);
		studentAge5 = readDouble (inputStream);

		/* Calculates the sum of the GPAs; */
		sumGpa = calculateSum (studentGpa1, studentGpa2, 
							   studentGpa3, studentGpa4, 
							   studentGpa5);

		/* Calculates the sum of the class standings; */
		sumClassStanding = calculateSum (studentClassStanding1, studentClassStanding2, 
										 studentClassStanding3, studentClassStanding4, 
										 studentClassStanding5);

		/* Calculates the sum of the ages; */
		sumAge = calculateSum (studentAge1, studentAge2,
								studentAge3, studentAge4,
								studentAge5);

		/* Calculates the mean of the GPAs; */
		meanGpa = calculateMean (sumGpa, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the class standings; */
		meanClassStanding = calculateMean (sumClassStanding, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the ages; */
		meanAge = calculateMean (sumAge, NUMBER_OF_STUDENTS);
 
		/* Calculates the deviation of each GPA from the mean */
		deviationGpa1 = calculateDeviation (studentGpa1, meanGpa);
		deviationGpa2 = calculateDeviation (studentGpa2, meanGpa);
		deviationGpa3 = calculateDeviation (studentGpa3, meanGpa);
		deviationGpa4 = calculateDeviation (studentGpa4, meanGpa);
		deviationGpa5 = calculateDeviation (studentGpa5, meanGpa);

		/* Calculates the variance of the GPAs */
		varianceGpa = calculateVariance (deviationGpa1, deviationGpa2,
										 deviationGpa3, deviationGpa4,
										 deviationGpa5, NUMBER_OF_STUDENTS);

		/* Calculates the standard deviation of the GPAs; */
		standardDeviationGpa = calculateStandardDeviation (varianceGpa);
	
		/* Determines the min of the GPAs; */
		minStudentGpa = findMin (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);	

		/* Determines the max of the GPAs; */
		maxStudentGpa = findMax (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);

		/* Writing the result to the output file (output.dat) */
		printDouble (outputStream, meanGpa);
		printDouble (outputStream, meanClassStanding);
		printDouble (outputStream, meanAge);
		printDouble (outputStream, standardDeviationGpa);
		printDouble (outputStream, minStudentGpa);
		printDouble (outputStream, maxStudentGpa);

		/* Closes the input and output files (i.e. input.dat and output.dat) */
		closeFileStream (inputStream);
		closeFileStream (outputStream);
	}

	return 0;
}
Exemple #11
0
    /**
     * Creates an R vector with a certian number of lineages and lineage counts.
     * @param n The number of lineages.
     * @param values The lineage counts.
     */
    R(int n, const std::array<int,dims>& values) : n(n), values(values){
        assert(isValidValues(n,values));

        sum = calculateSum(values);

    }
 int maxPathSum(TreeNode *root) {
     int maxSum = INT_MIN;
     int csum = 0;
     calculateSum(root, csum, maxSum);
     return maxSum;
 }
void main()
{
	int x=819;
	calculateSum(x);
}
Exemple #14
0
 void calculateProduct(Mat src, Mat& dst)
 {
     std::vector<Mat> x_contrast, y_contrast;
     getContrast(src, x_contrast, y_contrast);
     calculateSum(x_contrast, y_contrast, dst);
 }