如何在使用C的数组中打印第二大数?

  • 本文关键字:打印 数组 c
  • 更新时间 :
  • 英文 :


我是C中的新手,我一直在努力寻找第二大索引值。计划:

#include<stdio.h>
int main(){
int i,max=0,smax=0;
int A[] = {2,44,6,8,9,10,47};
int n=7;
for(i=1;i<n;i++){
if(A[max]<A[i]){
smax=max;
max=i;
}
else if(A[max]>A[i]){
smax=i;
}
}
printf("First value: %dn",A[max]);
printf("Second value: %d",A[smax]);
return 0;
}

现在当我运行程序时,它显示给我这个:

First value: 44
Second value: 9

我该怎么做来解决这个问题?

在我的解决方案中,我首先通过只查看数组的前两个元素来初始化maxsmax。之后,我检查所有剩余的数组元素,以确定是否有任何元素大于smax。如果我发现一个大于smax,我检查它是否也大于max,并采取相应的行动。

#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int max, smax;
int A[] = { 2, 44, 6, 8, 9, 10, 47 };
//set n to length of array
const int n = sizeof A / sizeof *A;
//make sure that the array has at least two values
if ( n < 2 )
{
fprintf( stderr, "Array must contain at least two values!n" );
exit( EXIT_FAILURE );
}
//initialize max and smax by processing the first two
//elements of the array
if ( A[0] > A[1] )
{
max  = 0;
smax = 1;
}
else
{
max  = 1;
smax = 0;
}
//process the remaining elements of the array
for( int i = 2; i < n; i++ )
{
if ( A[i] > A[smax] )
{
if ( A[i] > A[max] )
{
smax = max;
max = i;
}
else
{
smax = i;
}
}
}
//print the results
printf( "Highest value: %dn", A[max] );
printf( "Second-highest value: %d", A[smax] );
return EXIT_SUCCESS;
}

程序输出如下:

Highest value: 47
Second-highest value: 44

考虑到存在其他答案,这可能看起来很圆滑,但您可以简单地按降序对数组进行排序,然后选择第二个元素。

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int int_cmp_desc(const void *a, const void *b) {
int x = *(int *)a;
int y = *(int *)b;
return x == y ? 0 :
x <  y ? 1 : -1;
}
int main(void) {
int A[] = {2,44,6,8,9,10,47};
size_t sz = sizeof(A) / sizeof(*A);
int B[sz];
memcpy(B, A, sizeof(A));
qsort(B, sz, sizeof(*B), int_cmp_desc);
printf("%dn", B[1]);
return 0;
}

当然,如果您需要第二个最大的唯一的数字,这也会使它更容易。

int main(void) {
int A[] = {2,44,6,8,9,10,47,47};
size_t sz = sizeof(A) / sizeof(*A);
int B[sz];
memcpy(B, A, sizeof(A));
qsort(B, sz, sizeof(*B), int_cmp_desc);
printf("%dn", B[1]);
int first_number = B[0];
size_t i = 1;
// Advance index until it's at the next-to-last position.
// If it's there and we still haven't found a unique element, 
// index will advance to the last element of the array, which
// we can then print.
while (i < sz - 1 && B[i] == first_number) i++;
printf("%dn", B[i]);
return 0;
}

打印:

47
44

寻找最大和第二大问题是非常标准的面试问题,有非常简单的描述,但没有那么明显的解决方案,这是为了检查你如何处理边缘情况:

  1. 空输入数组-在这种情况下没有任何最大的,所以你必须考虑如何表示它
  2. 单元素数组-在这种情况下没有第二大数组,你需要考虑如何表示它
  3. 数组中所有元素都是相同的-你需要明确第二大元素的实际含义,它是与第一大元素相同还是应该不同,然后-你没有第二大

这里是查找最大和第二大的变量:

void printMaxAnd2ndMax(int* a, int n) {
int max = -1;
int smax = -1;
for (int i = 0; i < n; i++) {
if (max == -1 || a[i] > a[max]) {
smax = max;
max = i;
}
else if ((smax == -1 || a[i] > a[smax]) && (a[i] != a[max])) {
smax = i;
}
}
if (max >= 0)
printf("Largest value: %dn", a[max]);
else
printf("No largest valuen");
if (smax >= 0)
printf("Second value: %d", a[smax]);
else
printf("No second largest valuen");
}
int main() {
int a[] = { 2,44,6,8,9,10,47 };
int n = sizeof(a) / sizeof(a[0]);
printMaxAnd2ndMax(a, n);
return 0;
}

首先,你的n值是错误的;应该是7,不是5。这就是为什么你的"最大"。Value被发现为44,因为47甚至没有被循环检查。

我能想到的最直接的方法是对数组进行排序。如果使用默认设置,那么倒数第二个元素将是第二大元素。如果您使用自定义降序排序,则访问第二个元素(即元素&;1&;)。

如果你想在没有排序的情况下进行操作,那么理论上你必须循环遍历数组两次:第一次找到最大值,第二次(不包括最大值)找到第二个最大值。但是,这确实会出现一些问题,例如,当最大值元素出现多次时。(我假设你会说3是集合{1,2,3,3}中第二大的元素)因此,如果不使用排序,就必须循环遍历数组两次。第一次,查找最大值并存储该值的索引。第二次,找到最大值;不包括存储值

这是我的实现:

#include <stdio.h>
int main() {
int array[] = {2,44,6,8,9,10,47};
int i = 0, max_v = array[0], max_i = 0, smax = array[0];
int n = 7;
for (i = 0; i < n; i++) {
if (array[i] > max_v) {
max_v = array[i];
max_i = i;
}
}
for (i = 0; i < n; i++) {
if (i == max_i) {
i = max_i;
} else {
if (array[i] > smax) {
smax = array[i];
}
}
}
printf("First value: %dnSecond value: %dn", max_v, smax);
return 0;
}

[UPDATE]:-我终于在不久前完成了这个程序,它运行得很好:

#include<stdio.h>
int main(){
int arr[] = {2,4,6,28,19,12,11};
int n=7,max=0,smax=0,i;
for( i=1;i<n;i++){
if(arr[max]<arr[i]){
smax=max;
max=i;
}
else if(arr[smax]<arr[i] && arr[max]!=arr[i]){
smax=i;
}
else if(n==0){
printf("The array is empty");
return 0;
}
else if(n>=1){
printf("The array has only one element");
return 0;
}
printf("%dn",arr[max]);
printf("%dn",arr[smax]);
}

在此之前,我误解了else if的概念,但现在它起作用了。

最新更新