• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言连续子向量的最大和及时间度量实例

c语言 搞代码 4年前 (2022-01-06) 22次浏览 已收录 0个评论

这篇文章主要介绍了C语言连续子向量的最大和及时间度量,需要的朋友可以参考下

本文实例分析了C语言连续子向量的最大和及时间度量,分享给大家供大家参考之用。具体方法如下:

 #include  #include  #include  #define SCALE 3000 int 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**<div style="color:transparent">来源gaodai.ma#com搞#代!码网</div>*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  b ? a : b; } 

感兴趣的朋友可以测试运行一下本文实例以加深理解。希望本文所述对大家C程序设计的学习有所帮助。

以上就是C语言连续子向量的最大和及时间度量实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言连续子向量的最大和及时间度量实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址