我试图解决 codechef 中不同最大和第二大的总和数组的问题。但它告诉我错误。我试过但还不能调试它。请你帮帮我好吗? 问题如下: 给定一个包含 N 个整数的数组 A。 找出数组中两个不同整数的最大总和。 示例输入:
4
3
4 1 6
7
3 7 2 1 1 5 3
5
8 2 9 4 9
2
1 2
样本输出:
10
12
17
3
我的解决方案:
#include <stdio.h>
int main(void)
{
int T;
scanf("%d", &T);
while (T--)
{
int ara[1000], n, i, t;
scanf("%d", &n);
int largest, secL;
// input
for (i = 0; i < n; i++)
{
scanf("%d", &ara[i]);
}
if (ara[0] > ara[1])
largest = ara[0], secL = ara[1];
else
largest = ara[1], secL = ara[0];
for (i = 2; i < n; i++)
{
t = ara[i];
if (t > largest && )
secL = largest,largest = t ;
else if (t > secL && t<largest)
secL = t;
}
printf("%d\n",largest + secL);
}
return 0;
}
虽然我的程序对于给定的输入工作正常。
其实我想知道我代码中的错误。
当前两个元素相同且最大时,您的代码将失败。
您不需要数组:您可以通过对传入数据进行一次解析来解决问题。
#include <stdio.h>
#include <limits.h>
int main(void)
{
int T;
scanf("%d", &T);
while (T--) {
int val, n;
scanf("%d", &n);
int largest = INT_MIN, secL = INT_MIN;
for (int i = 0; i < n; i++) {
scanf("%d", &val);
if (largest < val) {
secL = largest;
largest = val;
}
else if(secL < val && largest > val)
secL = val;
}
printf("%d\n", largest + secL);
}
return 0;
}
对于初学者来说,if 语句中有一个拼写错误
if (t > largest && )
所以程序不能编译。
然而,你的做法在逻辑上是错误的。如果数组的前两个元素彼此相等并且是数组中最大的元素,那么结果将不正确。
你可以写下类似下面的东西:
int first, second;
int second_present = 0;
first = ara[0];
for ( i = 1; i < n; i++ )
{
if ( first < ara[i] )
{
second_present = 1;
second = first;
first = ara[i];
}
else if ( ( ara[i] < first ) && ( second_present == 0 || second < ara[i] ) )
{
second_present = 1;
second = ara[i];
}
}
if ( second_present ) printf( "%lld\n", ( long long int )first + second );
else puts( "There is no unique second largest element." );
恕我直言,您不需要有 1000 个元素的数组来存储元素,因为处理后您不会使用数组元素。在我看来,您的代码具有这些标志:(为简洁起见,我将自己限制在内部循环中)
int ara[1000], n, i, t;
ara[1000]
,而是一个只包含两个元素的数组
最大的两个元素应该是必须的,因为您通过尝试进行所有可能的比较而使事情复杂化,并且如果您被要求提供最大的 N 个元素,您的代码不会升级。恕我直言,有一个 N 元素数组和
在适当的地方插入最后读取的值会更快(你会看到你失败的测试可能会导致时间损失,因为你对输入进行了两次传递:一次读取和存储,下一次获取
两个最大的数字)你也不检查 scanf 结果,所以如果你得到的输入不能解析为数字,你可能会遇到麻烦,因为你不问 scanf
它是否正确读取了数字.无论如何,您的代码无法编译,因为您在第 24 行中有一个格式错误的表达式(以
&&
结尾,这是不正确的语法)所以我无法使用您提供的输入运行您的代码。不过,我采用了这种方法,它可以根据您的输入正确运行,使用 NELEM
数组方法,并一次传递数据。元素被插入数组中的适当位置,因此,只需添加最大的元素,您可以考虑仅输入一个元素,这可能会在您的代码中失败(您假设所有输入都是两个或更多元素,但你没有指定它作为要求)插入排序方法是合适的,因为我们不允许数组增长超过 2 个元素并使比较更简单(你不必将所有元素与所有元素进行比较,只需跳过如果元素等于或小于每个元素)
#include <stdio.h>
#include <stdlib.h>
#define F(_fmt) __FILE__":%d: "_fmt, __LINE__
#define ERR(_efmt, ...) do { \
fprintf(stderr, F(_efmt), ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
/* number of elements we want to add, change this freely */
#define NELEM 2
int main()
{
int N;
int res = scanf("%d", &N);
if (res < 1)
ERR("couldn't scan number of tests\n");
for (int t = 1; t <= N; t++) {
int array[NELEM], array_n = 0;
int n, res = scanf("%d", &n);
if (res < 1) {
ERR("test #%d: couldnt scan number of elements of array\n", t);
}
/* read it, try to insert it in the proper place */
for (int i = 1; i <= n; i++) {
int el;
res = scanf("%d", &el);
if (res < 1) {
ERR("test #%d: cannot read element #%d\n", t, i);
}
for (int j = 0; j < array_n; j++) {
if (el > array[j]) {
/* shift all elements to the end of the array */
for (int k = j; k < array_n && k < NELEM; k++) {
/* exchange el <-> array[j] */
int temp = el;
el = array[j];
array[j] = temp;
}
}
}
/* only add up last element to array if smaller than NELEM */
if (array_n < NELEM)
array[array_n++] = el;
}
/* add the array elements */
int accum = 0;
for (int i = 0; i < array_n; i++) {
accum += array[i];
}
printf("%d\n", accum);
}
return EXIT_SUCCESS;
}