程序空间换时间小例子

来源:互联网 发布:手机淘宝怎么添加客服 编辑:程序博客网 时间:2024/06/10 18:42

  我们写程序总是希望时间和空间复杂度相对的底一点,但是往往时间和空间俩个不可能同时的底,一般时间少了,空间不免多了,空间少了,时间又相映的增加,但是,要在时间上来减少复杂度有时候似乎不可能,除了在算法上下工夫,似乎无能为力,但是空间就不同了,随着硬件技术的发展,计算机的内存是越来越大,有时候损失空间来提高时间是可取的,用户对时间的等待是不可容忍的,但是对空间并不是那么敏感!

1,知道一组数的范围,知道数的个数,要求对这组数进行排序,快速排序,堆排序时间复杂度都不是线性的,要在 线性时间内完成排序,只要用到比较似乎是没有可能完成这个任务,看牺牲一下空间是否可以满足要求,答案的肯定的!

 

#include"stdio.h"
#include
"stdlib.h"
#include
"time.h"
#include
"math.h"

#define NUMBER 20
#define SIZE 10 
int number[NUMBER] ;

int main()
{

 
int totalss = 0,temp,i,j ;
 clock_t starttime ,endtime;
 
double totaltime ;
    printf(
"please input the number you want to sort ");
 
for (  i = 0 ; i < SIZE ; i ++ )
 {
  scanf (
"%d",&temp);
  number[temp] 
= 1 ;
 }
    starttime 
= clock() ;
 printf(
"after sort the number selization is: ");
 
for (  j = 0 ; j < NUMBER ; j ++ )
 {
  
if ( number[j] == 1 ) 
   printf(
"%-3d",j);

 }
 printf(
" ");
    endtime 
= clock();
    totaltime 
= (double)(endtime - starttime) / CLOCKS_PER_SEC;
    printf(
"the program id user time %lf second ",totaltime);
 
return 0 ;
}

 

20范围内的10个数进行排序,结果见下:

please input the number you want to sort
12 1 4 5 7 9 13 15 19 14
after sort the number selization is: 1  4  5  7  9  12 13 14 15 19
the program is  use time 0.000000 second

算法很简单,一般人都能够看懂,不加赘述了!

2 找出10000000范围内的素数,同样可以用以上的算法,先给出程序

 

#include"stdio.h"
#include
"stdlib.h"
#include
"time.h"
#include
"math.h"

#define NUMBER  100

int main()
{
 
int *number ;
 
int totalss = 0, i,j,k;
 clock_t starttime ,endtime;
 
double totaltime ;
 
int num  = (int)sqrt(NUMBER);
 starttime 
= clock() ;
    number 
= (int*) malloc (NUMBER * sizeof (int));
 
for (  i = 2; i < NUMBER ; i ++ )
  number[i] 
= 1 ;
 
    
for ( j = 2 ; j <num; j ++ )
   
if ( number[j] )
   
for ( k = j ; k <= (NUMBER/j) ; k ++ ) 
    number[k 
* j] = 0 ;
    endtime 
= clock();
    totaltime 
= (double)(endtime - starttime) / CLOCKS_PER_SEC;
    printf(
"the program id user time %lf second ",totaltime);
 
for ( i = 2 ; i < NUMBER ; i ++ )
  
if (number[i])
  {
   printf(
"the number %d is sushu ",i);
      totalss 
++ ;
  }

printf(
"the total number of sushu is %d " ,totalss);
    endtime 
= clock () ;
 totaltime 
= (double)(endtime - starttime) / CLOCKS_PER_SEC;
 printf(
"the program id user time %lf second ",totaltime);
 
return 0 ;
}

 

为了能够把结果给出,我将范围缩小到100,int num  = (int)sqrt(NUMBER);

这句代码值得说一下,查找数范围内的素数,并不需要从头找到尾,只要在他的平方根完成就可以了,证明很简单

大家可以证明.

the program id user time 0.000000 second
the number 2 is sushu
the number 3 is sushu
the number 5 is sushu
the number 7 is sushu
the number 11 is sushu
the number 13 is sushu
the number 17 is sushu
the number 19 is sushu
the number 23 is sushu
the number 29 is sushu
the number 31 is sushu
the number 37 is sushu
the number 41 is sushu
the number 43 is sushu
the number 47 is sushu
the number 53 is sushu
the number 59 is sushu
the number 61 is sushu
the number 67 is sushu
the number 71 is sushu
the number 73 is sushu
the number 79 is sushu
the number 83 is sushu
the number 89 is sushu
the number 97 is sushu
the total number of sushu is 25
the program id user time 0.000000 second
数字很小,时间为0,将数字该为10000000,看看运行的时间(由于数比较多,给出找出素数的时间,和打印时间)
the program is use time 1.765000 second
全部素数找到需要时间仅仅1.765秒!

原创粉丝点击