Cod sursa(job #293670)

Utilizator crawlerPuni Andrei Paul crawler Data 1 aprilie 2009 23:39:00
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 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;

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_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;   
}