gpt4 book ai didi

c - 查找最长的连续数字范围

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:58 24 4
gpt4 key购买 nike

我一直在努力寻找大于 1 的最长连续数字范围。一个例子:0001个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);

最佳答案

这对你有用。

#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;
}

关于c - 查找最长的连续数字范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44536039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com