测试for循环和最大公约数

来源:互联网 发布:波段指标源码 编辑:程序博客网 时间:2024/05/17 07:25
//以下都在linux平台下,测试通过#include <stdio.h>#include <sys/time.h>#include <unistd.h>void tv_sub(struct timeval *end, struct timeval *start){    if((end->tv_usec -= start->tv_usec) < 0){        --end->tv_sec;        end->tv_usec += 1000000;    }        end->tv_sec -= start->tv_sec;}//const int  MAX_ROW = 5000;//const int  MAX_COL = 10000;#define MAX_ROW 1000#define MAX_COL 1000void row_col(){  //先行后列    struct timeval tv_start;    struct timeval tv_end;    int a[MAX_ROW][MAX_COL];    gettimeofday(&tv_start, NULL);    int i, j;    for(i = 0; i < MAX_ROW; i++){        for(j = 0; j < MAX_COL; j++){            a[i][j] = 1;        }    }    gettimeofday(&tv_end, NULL);    tv_sub(&tv_end, &tv_start);    printf(" row_col time %d sec , %d usec\n", tv_end.tv_sec, tv_end.tv_usec);    printf("ddd\n");}void col_row(){    struct timeval tv_start, tv_end;    int a[MAX_ROW][MAX_COL];    gettimeofday(&tv_start, NULL);    int i, j;    for(j = 0; j < MAX_COL; j++){        for(i = 0; i < MAX_ROW; i++){            a[i][j] = 1;        }    }    gettimeofday(&tv_end, NULL);    tv_sub(&tv_end, &tv_start);    printf(" col_row time %d sec , %d usec\n", tv_end.tv_sec, tv_end.tv_usec);}int main(int argc, char **args){    printf("3\n");    row_col();    col_row();    printf("ok\n");    return 0;}_________________________________________________________________________________________________#include <stdio.h>int main(){    unsigned long a, b, c = 0;  //两个整数和一个临时变量    unsigned long lcm = 0, gcd = 0;  //最小公倍数,最大公约数    while(1){        printf("please input two numbers(用空格隔开)...\n");        scanf("%lu %lu",&a, &b);        if(a <= 0 || b <= 0){            printf("input error, please input again..\n");            continue;        }else            break;    }    unsigned long ra = a, rb = b;  //保留原始数据    while(a % b != 0){        c = a % b;        a = b;        b = c;    }    gcd = b;    lcm = (ra * rb) / gcd;    printf("最大公约数是 %lu\n", gcd);    printf("最小公倍数是 %lu\n", lcm);    return 0;}

0 0
原创粉丝点击