国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言連續(xù)子向量的最大和及時間度量實(shí)例

2020-01-26 15:19:02
字體:
供稿:網(wǎng)友

本文實(shí)例分析了C語言連續(xù)子向量的最大和及時間度量,分享給大家供大家參考之用。具體方法如下:

#include <stdio.h>#include <time.h>#include <stdlib.h>#define SCALE 3000int maxnum(int a, int b);int main(int argc, char const *argv[]){ FILE *fp; fp = fopen("maximum.in", "r"); // int x[] = {1,12,-11,10,-65,54,22,-9,21,5,48,5,-8,-2,56,54,-88,-5,2,-8,554,-56,35,-55,555,-65,-545,-23,48,-5,88,-56,16,-8}; int *x = (int *)malloc(sizeof(int)*(SCALE+1)); int xi = SCALE,a = 0,num_in = 0; while(xi--){  fscanf(fp, "%d", &x[a++]); } clock_t start, end; // ***Algorithm-1 cube*** start = clock(); int max = 0; int length = SCALE; int i,j,k; for (i = 0; i < length; ++i) {  for (j = i; j < length; ++j)  {   int sum = 0;   for (k = i; k <= j; ++k)   {    sum += x[k];   }   max = maxnum(max, sum);  } }  // long num = 10000000L; // while(num--); end = clock(); double times = (double)(end - start)/CLOCKS_PER_SEC; double dend = (double)end; printf("/n***Algorithm-1 cube***/n"); printf("end: %f/n", dend); printf("Time consuming: %f/n", times); printf("%d/n", max); // ***Algorithm-2 square*** start = clock(); max = 0; for (i = 0; i < length; ++i) {  int sum = 0;  for (j = i; j < length; ++j)  {   sum += x[j];   max = maxnum(max, sum);  } } end = clock(); times = (double)(end - start)/CLOCKS_PER_SEC; dend = (double)end; printf("/n***Algorithm-2 square***/n"); printf("end: %f/n", dend); printf("Time consuming: %f/n", times); printf("%d/n", max); // ***Algorithm-3 linear*** start = clock(); max = 0; int max_end_here = 0; for (i = 0; i < length; ++i) {  max_end_here = maxnum(max_end_here + x[i], 0);  max = maxnum(max, max_end_here); } end = clock(); times = (double)(end - start)/CLOCKS_PER_SEC; dend = (double)end; printf("/n***Algorithm-3 linear***/n"); printf("end: %f/n", dend); printf("Time consuming: %f/n", times); printf("%d/n", max); free(x); x = NULL; return 0;}int maxnum(int a, int b){ return a > b ? a : b;}

感興趣的朋友可以測試運(yùn)行一下本文實(shí)例以加深理解。希望本文所述對大家C程序設(shè)計的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 达日县| 宁德市| 育儿| 邯郸县| 望奎县| 沂源县| 惠来县| 梓潼县| 武胜县| 闽清县| 汉源县| 吉林市| 长沙县| 舒兰市| 保康县| 邳州市| 林芝县| 奉贤区| 文登市| 乌恰县| 安阳市| 都兰县| 赞皇县| 霞浦县| 金乡县| 汉阴县| 交城县| 建平县| 浦北县| 南部县| 固阳县| 阿合奇县| 大洼县| 彭水| 花莲市| 双江| 威海市| 尼木县| 惠安县| 柯坪县| 乐平市|