ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Общий форум

What functions of "C++" work slower than that of C?
Послано begi 3 окт 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?
Послано begi 3 окт 2006 23:30