LoginSignup
1
0

More than 5 years have passed since last update.

【備忘録】C

Posted at

はじめて

PCがコードだらけで、特にデスクトップが汚くなってきたのでなんか昔作ったプログラムを備忘録としてここに。

備忘録コード

test.c
#include<stdio.h>
#include<time.h>

void change(int i,int j,int x[]){
    int t;
    t = x[i];
    x[i]=x[j];
    x[j]=t;
}

void sort(int l,int r,int x[]){
    int i,j,p;
    i=l;
    j=r;
    p=x[(l+r)/2];
    while(1){
        while(x[i]<p){
            i++;
        }
        while(p<x[j]){
            j--;
        }
        if(i>=j){
            break;
        }
        change(i,j,x);
        i++;
        j--;
    }
    if(l<i-1){
        sort(l,i-1,x);
    }
    if(j+1<r){
        sort(j+1,r,x);
    }
}

double Uniform( void ){
    static int x=10;
    int a=1103515245,b=12345,c=2147483647;
    x = (a*x + b)&c;

    return ((double)x+1.0) / ((double)c+2.0);
}

int main(){
    clock_t start,end,tStart,tEnd;
    time_t timer;
    struct tm *t_st;
    int numY = 1000000;
    int num,i,y[numY];
    int j = 0;
    double timeClock[3];
    int x[] = {6,3,1,7,5,4,8,5,2,9};

    start = clock();
    tStart = start;
    num=sizeof(x)/sizeof(x[0]);
    printf("%d",num);
    sort(0,num-1,x);
    for(i=0;i<num;i++) printf("%d",x[i]);

    end = clock();
    printf("\n");

    timeClock[0] = (end - start)/CLOCKS_PER_SEC;

    for (i=0; i<(numY*1000); i++) {
        j+=i;
        j*=i;
        j-=i;
    }
    printf("%d\n",j);

    start = clock();
    for(i=0; i<numY; i++) y[i]=(int)(Uniform()*1000000);

    sort(0,numY-1,y);
    for(i=0; i<numY; i++){
        if(i==numY-1)printf("%d:%d\n",i+1,y[i]);
    }

    end = clock();
    tEnd = end;
    timeClock[1] = (end - start)/CLOCKS_PER_SEC;
    timeClock[2] = (tEnd - tStart)/CLOCKS_PER_SEC;

    printf("1:%f[s]\t2:%f[s]\t3:%f[s]\n",timeClock[0],timeClock[1],timeClock[2]);

    printf("%lu\n",clock());

    time(&timer);
    printf("現在時刻:%s\n",ctime(&timer));
    t_st = localtime(&timer);
    printf("月:%d\n",t_st->tm_mon+1);
    printf("日:%d\n",t_st->tm_mday);
    printf("時:%d\n",t_st->tm_hour);
    printf("分:%d\n",t_st->tm_min);
    printf("秒:%d\n",t_st->tm_sec);
    return 0;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0