ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

What functions of "C++" work slower than that of C?
Posted by begi 3 Oct 2006 19:22
I just sent two solutions to proble 1048,one in C++ and the other in C.
The algorithm was the same but C++ got "time limit exceeded" but C got AC.Look below:

C++ program:

#include<iostream>
using namespace std;
int main()
{
        int *A,*B,*R,n,carry = 0;
        cin >> n;
        A = new int[n];
        B = new int[n];
        R = new int[n];
        for(int i=0;i<n;i++)
                cin >> A[i] >> B[i];
        for(int i=0;i<n;i++)
        {
                int tmp = A[n-i-1]+B[n-i-1];
                R[i] = (tmp+carry)%10;
                carry = (tmp+carry)/10;
        }
        if(carry) cout << carry;
        for(int i=0;i<n;i++)
                cout << R[n-i-1];
        cout << endl;
        return 0;
}

C program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
        int *A,*B,*R,i,n,tmp,carry = 0;
        scanf("%d",&n);
        A = (int *)malloc(n*sizeof(int));
        B = (int *)malloc(n*sizeof(int));
        R = (int *)malloc(n*sizeof(int));
        for(i=0;i<n;i++)
                scanf("%d %d",&A[i],&B[i]);
        for(i=0;i<n;i++)
        {
                tmp = A[n-i-1]+B[n-i-1];
                R[i] = (tmp+carry)%10;
                carry = (tmp+carry)/10;
        }
        if(carry) printf("%d",carry);
        for(i=0;i<n;i++)
                printf("%d", R[n-i-1]);
                printf("\n");

return 0;
}
Re: What functions of "C++" work slower than that of C?
Posted by begi 3 Oct 2006 23:30