[英]function that returns the sum of the longest arithmetic sequence(without using arrays)
所以我有这个问题我想解决几天了,我只是感到迷茫。 function 基本上需要获取序列的大小(n)。 用户输入尺码,然后function会要求他把序列的数字一个接一个地放上去。 一旦他输入所有数字,function 需要返回最长序列的和。 例如,n=8,用户输入 [1,3,5,7,11,13,15,16]。 结果将为 16,因为 [1,3,5,7] 是最长的序列。 如果 n=8 并且用户输入 [1,3,5,7,11,15,19,20],结果将是 52,因为虽然有 2 个长度为 4 的序列,但是 [7, 11,15,19] 比 [1,3,5,7] 大。 序列不一定需要递增,也可以递减。 function不能递归,arrays不能使用。 我希望问题是什么足够清楚,如果不是,请告诉我,这样我会尽力解释得更好。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i, size, num, nextNum, diff, prevDiff, currSeqLength = 0, currSum, prevSum = 0;
printf("Please enter the arithmetic list size: ");
scanf_s("%d", &size);
for (i = 1; i <= size; i++)
{
printf("Please enter num: ");
scanf_s("%d", &num);
while (i == 1)
{
prevSum = num;
nextNum = num;
currSeqLength++;
break;
}
while (i == 2)
{
currSum = prevSum + num;
diff = num - nextNum;
nextNum = num;
currSeqLength++;
break;
}
while (i >= 3)
{
prevDiff = diff;
diff = num - nextNum;
nextNum = num;
if (prevDiff == diff)
{
currSum += num;
currSeqLength++;
break;
}
else
{
prevDiff = diff;
// diff now should be the latest num - previous one
}
}
}
}
到目前为止,这基本上就是我所管理的。 我知道这里有些东西没有按预期工作,我知道代码只完成了一半,但我已经尝试了很多东西,但我似乎无法指出问题出在哪里,我真的很喜欢一些指导,我真的迷路了。
我遇到的几个问题。 当我进入一个新数字和旧数字之间的差异与之前的循环不同的循环时(例如,[4,8,11]),我似乎无法保存旧数字(在本例 8) 计算下一个差值(即 3)。 更不用说前 2 个 while 循环可能效率不高,可以合并在一起。
PS 我知道代码不是 function,但我这样写是为了跟踪每一步,一旦代码按预期工作,我将其转换为 function。
我尝试了您的代码,但如评论中所述,需要在序列的各个阶段跟踪检查哪个序列具有最长的一致差异值。 我添加了一些额外的 arrays 来执行 function。以下是如何完成的原型。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int len, diff, longest;
printf("Enter the size of your sequence: ");
scanf("%d", &len);
int num[len], seq[len], sum[len];
for (int i = 0; i < len; i++)
{
printf("Enter value #%d: ", i + 1);
scanf("%d", &num[i]);
seq[i] = 0; /* Initialize these arrays as the values are entered */
sum[i] = 0;
}
for (int i = 0; i < len - 1; i++)
{
seq[i] = 1; /* The sequence length will always start at "1" */
sum[i] = num[i];
diff = num[i + 1] - num[i];
for (int j = i; j < len - 1; j++)
{
if (diff == num[j + 1] - num[j])
{
sum[i] += num[j + 1]; /* Accumulate the sum for this sequence */
seq[i] += 1; /* Increment the sequence length for this sequence portion */
}
else
{
break;
}
}
}
longest = 0; /* Now, determine which point in the lise of numbers has the longest sequence and sum total */
for (int i = 1; i < len; i++)
{
if ((seq[i] > seq[longest]) || ((seq[i] == seq[longest]) && (sum[i] > sum[longest])))
{
longest = i;
}
}
printf("The sequence with the longest sequence and largest sum is: [%d ", num[longest]);
diff = num[longest + 1] - num[longest];
for (int i = longest + 1; i < len; i++)
{
if ((num[i] - num[i - 1]) == diff)
{
printf("%d ", num[i]);
}
else
{
break;
}
}
printf("]\n");
return 0;
}
需要注意的几点。
以下是使用初始查询中的列表值的一些示例终端 output。
@Dev:~/C_Programs/Console/Longest/bin/Release$ ./Longest
Enter the size of your sequence: 8
Enter value #1: 1
Enter value #2: 3
Enter value #3: 5
Enter value #4: 7
Enter value #5: 11
Enter value #6: 13
Enter value #7: 15
Enter value #8: 16
The sequence with the longest sequence and largest sum is: [1 3 5 7 ]
@Dev:~/C_Programs/Console/Longest/bin/Release$ ./Longest
Enter the size of your sequence: 8
Enter value #1: 1
Enter value #2: 3
Enter value #3: 5
Enter value #4: 7
Enter value #5: 11
Enter value #6: 15
Enter value #7: 19
Enter value #8: 20
The sequence with the longest sequence and largest sum is: [7 11 15 19 ]
毫无疑问,此代码片段需要一些润色,但请试一试,看看它是否符合您项目的精神。
我知道纯代码答案不受欢迎,但这是我能想到的最简单的答案,它的逻辑似乎很容易理解:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i, size;
int currNum, currDiff;
int prevNum = 0, prevDiff = 0;
int currSum = 0, currSeqLen = 0;
int bestSum = 0, bestSeqLen = 0;
printf("Please enter the arithmetic list size: ");
scanf_s("%d", &size);
for (i = 0; i < size; i++)
{
printf("Please enter num: ");
scanf_s("%d", &currNum);
if (currSeqLen > 0)
{
currDiff = currNum - prevNum;
if (currSeqLen > 1 && currDiff != prevDiff)
{
/* New arithmetic sequence. */
currSeqLen = 1;
currSum = prevNum;
}
prevDiff = currDiff;
}
currSum += currNum;
prevNum = currNum;
currSeqLen++;
if (currSeqLen > bestSeqLen ||
currSeqLen == bestSeqLen && currSum > bestSum)
{
/* This is the best sequence so far. */
bestSeqLen = currSeqLen;
bestSum = currSum;
}
}
printf("\nbest sequence length=%d, sum=%d\n", bestSeqLen, bestSum);
return 0;
}
我省略了对scanf_s
调用的错误检查。 对于非 Windows 平台,它们可以更改为scanf
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.