zoukankan      html  css  js  c++  java
  • C----------输入一组整数,求出这组数字子序列和中的最大值,只要求出最大子序列的和,不必求出最大值对应的序列。

    © 版权声明:本文为博主原创文章,转载请注明出处

    代码:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define GET_ARRAY_LEN(array, len){len = sizeof(array) / sizeof(array[0]);}// 定义一个带参数的宏,将数组长度存储在变量len中
     4 
     5 int main()
     6 {
     7     int seq[] = {2, 11, -4, 13, -5, 2, -5, -3, 12, -9};// 数组
     8     int i, j, len, max;
     9 
    10     GET_ARRAY_LEN(seq, len);// 计算数组长度
    11     max = seq[0];// 初始化最大值
    12 
    13     for (i = 0; i < len; i++) {
    14         int temp = seq[0];
    15         for (j = i + 1; j < len; j++) {
    16             temp += seq[j];
    17             if (temp > max) {
    18                 max = temp;
    19             }
    20         }
    21     }
    22 
    23     printf("The maximum of the subsequence is %d
    ", max);
    24 
    25     return 0;
    26 }

    结果:

  • 相关阅读:
    Bluedroid介绍
    Android蓝牙介绍
    Android Bluetooth抓包
    Bluetooth LMP介绍
    Bluetooth Baseband介绍
    Bluetooth SDP介绍
    Bluetooth HFP介绍
    Bluetooth RFCOMM介绍
    Bluetooth L2CAP介绍
    Windows开发
  • 原文地址:https://www.cnblogs.com/jinjiyese153/p/7738192.html
Copyright © 2011-2022 走看看