Cod sursa(job #293687)

Utilizator crawlerPuni Andrei Paul crawler Data 1 aprilie 2009 23:56:59
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.91 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

int n = 5000000;
double start,stop;
int QWERTY = 1, LOL = 123456789;

#define DIM 10000
char buff[DIM];
int poz=0;
#define buffer fread(buff,1,DIM,stdin),poz=0

#define CIT(x)                                         \
{                                                      \
     x = 0;                                            \
     while (buff[poz] < '0' || buff[poz] > '9')        \
     if (++poz == DIM) buffer;                         \
     while ('0'<=buff[poz] && buff[poz]<='9')          \
     {                                                 \
          x = x*10 + buff[poz] - '0';                  \
          if (++poz == DIM) buffer;                    \
     }                                                 \
}

void init()
{
     QWERTY = 123456789;     
}

int numar_random()
{
     QWERTY += LOL;
     return QWERTY;     
}

double timp()
{
     return (stop-start)/1000;
}

void test_scanf()
{
     init();
     //printf("test stdio.h\n");
     
     //scriere
     start = clock();

     FILE *f = fopen("cutii.out","w");
     
     int tmp;
     long long suma=0;
     
     for (int i=1;i<=n;++i)
     {
          tmp = numar_random();
          suma += tmp;
          fprintf(f,"%d\n", tmp);
     }
          
     fclose(f);          
     stop = clock();
 /*    
     //printf("a scris %d numere in %.2f secunde\n", n,timp());
     
     //printf("suma lor este %lld\n", suma);

     //citire     


     start = clock();

     f = fopen("cutii.out","r");

     suma = 0;
     for (int i=1;i<=n;++i)
     {
          fscanf(f,"%d", &tmp);
          suma += tmp;
     }          
          
     fclose(f);          
     
     stop = clock();
  */   
     //printf("a citit %d numere in %.2f secunde\n", n,timp());
     //printf("suma lor este %lld\n", suma);
     
}

void test_parsare()
{
     init();
     //printf("test stdio.h\n");
     
     //scriere
     start = clock();

     FILE *f = fopen("cutii.out","w");
     
     int tmp;
     long long suma=0;
     
     for (int i=1;i<=n;++i)
     {
          tmp = numar_random();
          suma += tmp;
          fprintf(f,"%d\n", tmp);
     }
          
     fclose(f);          
     stop = clock();
     
     //printf("a scris %d numere in %.2f secunde\n", n,timp());
     
     //printf("suma lor este %lld\n", suma);

     //citire     


     start = clock();

     f = fopen("cutii.out","r");

     suma = 0;
     for (int i=1;i<=n;++i)
     {
          //fscanf(f,"%d", &tmp);
          CIT(tmp)
          suma += tmp;
     }          
          
     fclose(f);          
     
     stop = clock();
     
     //printf("a citit %d numere in %.2f secunde\n", n,timp());
     //printf("suma lor este %lld\n", suma);
     
}

void test_streamuri()
{
     init();
     
     //printf("test streamuri\n");

     //scriere     
     start = clock();
     
     ofstream out("cutii.out");

     int tmp;
     long long suma=0;


     for (int i=1;i<=n;++i)
     {
          tmp = numar_random();
          suma += tmp;
          out<<tmp<<'\n';
     }


     out.close();
 /*    
     stop = clock();
     
     //citire
     //printf("a scris %d numere in %.2f secunde\n", n,timp());
     //printf("suma lor este %lld\n", suma);
     
     start = clock();

     ifstream in("cutii.out");
     
     suma = 0;
     
     for (int i=1;i<=n;++i)
     {
          in>>tmp;
          suma += tmp;
     }          

          
     in.close();
     
     stop = clock();
 */    
     //printf("a citit %d numere in %.2f secunde\n", n,timp());
     //printf("suma lor este %lld\n", suma);     
}

int main()
{
     test_scanf();  
     
     //test_streamuri();
     
     //scanf("\n");
     
     return 0;   
}