找到最长的连续数字范围

时间:2017-06-14 05:21:40

标签: c

我一直在努力寻找大于1的最长连续数字范围。 一个例子: 0 0 0 1 2 1 2 2 2 3 3 0。 我真的不确定如何解决这个问题。基本上我的目标是找到起始值的位置和范围结束的位置。

 typedef struct fitbit
{
    char minute[9];
    double calories;
    double distance;
    unsigned int floors;
    unsigned int heartRate;
    unsigned int steps;
    Sleep sleepLevel;
} FitbitData;

typedef enum sleep
{
    NONE = 0, ASLEEP = 1, AWAKE = 2, REALLYAWAKE = 3
} Sleep;

void sleep_level(FitbitData *arr)
{
    char *start;
    char *end;
    int max_sum = 0;


    for (int i = 0; i < 1440; i++)
    {
        if (arr[i].sleepLevel > 1)
        {
            start = arr[i].minute;
            max_sum += arr[i].sleepLevel;
        }
        else if (arr[i].sleepLevel == 1 || arr[i].sleepLevel < 1)
        {
            end = arr[i].minute;
        }
    }


    printf("The longest range of consecutive starts at %s and ends at %s",start,end);

3 个答案:

答案 0 :(得分:0)

这可能适合你。

#include <stdio.h>
#include <stdlib.h>

typedef enum sleep
{
    NONE = 0, ASLEEP = 1, AWAKE = 2, REALLYAWAKE = 3
} Sleep;

typedef struct fitbit
{
    char minute[9];
    double calories;
    double distance;
    unsigned int floors;
    unsigned int heartRate;
    unsigned int steps;
    Sleep sleepLevel;
} FitbitData;


void sleep_level(FitbitData *arr, int n) {
    char *start;
    char *end;
    int max_sum = 0;

    char *current_start;
    char *current_end;
    int current_sum = 0;
    int i;

    if (n < 0)
        return ;

    current_start = current_end = arr[0].minute; 
    for (i = 0; i < n; i++) {
        if (arr[i].sleepLevel > 1) {
            current_sum += arr[i].sleepLevel;
            current_end  = arr[i].minute;
        }
        else {
            if (current_sum > max_sum) {
                start   = current_start;
                max_sum = current_sum;
                end     = current_end;
            }
            do
                i++;
            while (i < n && arr[i].sleepLevel <= 1);
            if (i >= n)
                break;
            current_start = arr[i].minute;
            current_sum   = arr[i].sleepLevel;
            current_end   = arr[i].minute;
            }
    }

    if (current_sum > max_sum) {
        start   = current_start;
        max_sum = current_sum;
        end     = current_end;
    }

    printf("The longest range of consecutive starts at %s and ends at %s\n",start,end);
}

int main(void)
{
    FitbitData arr[12];

    strcpy( arr[0].minute, "a");
    arr[0].sleepLevel = 0;
    strcpy( arr[1].minute, "b");
    arr[1].sleepLevel = 0;
    strcpy( arr[2].minute, "c");
    arr[2].sleepLevel = 0;
    strcpy( arr[3].minute, "d");
    arr[3].sleepLevel = 1;
    strcpy( arr[4].minute, "e");
    arr[4].sleepLevel = 2;
    strcpy( arr[5].minute, "f");
    arr[5].sleepLevel = 1;
    strcpy( arr[6].minute, "g");
    arr[6].sleepLevel = 2;
    strcpy( arr[7].minute, "h");
    arr[7].sleepLevel = 2;
    strcpy( arr[8].minute, "i");
    arr[8].sleepLevel = 2;
    strcpy( arr[9].minute, "g");
    arr[9].sleepLevel = 3;
    strcpy( arr[10].minute, "k");
    arr[10].sleepLevel = 3;
    strcpy( arr[11].minute, "l");
    arr[11].sleepLevel = 0;


    sleep_level(arr, sizeof arr / sizeof arr[0]);

    return 0;
}

答案 1 :(得分:0)

  

我一直在努力寻找最长的连续数字大于1的数字

这不是你的代码所做的。你会想要这个伪代码的内容:

int current_size = 0;  // size of the current sequence
int current_val = 0;   // value of the current sequence   
int longest_size = 0;  // size of the longest sequence
int longest_val = 0;   // value of the longest sequence

for(int i=0; i<n; i++)
{
  if(array[i] != 0)
  {
    if(array[i] == current_val)
    {
      current_size++;
    }
    else
    {
      current_val  = array[i];
      current_size = 1;
    }

    if(current_size > longest_size)
    {
      longest_size = current_size;
      longest_val  = current_val;
    }
  }
}

答案 2 :(得分:0)

您可能会觉得这很有用。

int max = 0, temp = 0, end = 0;
for(int i=0; i<n; i++)
{
  if(arr[i] > 1) temp++;
  else 
  {
    if(temp > max) 
    {
      max = temp;
      end = i
    }
    temp = 0;
  }
}

在代码的最后,序列的最大长度将以最大值存储,终点将存储在最后,如果你想要开始,你可以

int start = end - max + 1;