请在C中回答这个问题 [英] Please answer this question in C

查看:91
本文介绍了请在C中回答这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.给出一个数组(最多包含1000个正整数),找到第二大整数。如果没有第二大整数,则返回-1。



输入规格:

输入1:阵列长度

输入2:正整数数组



输出规格:

相应地返回第二大数字或-1。



1.Given an array (containing at most 1000 positive integers),find the second largest integer.If there exists no second largest integer,return,-1.

Input Specifications:
Input1:length of the array
Input2:array of positive integers

Output Specification:
Return the second largest number or -1 accordingly.

#include<stdio.h>
#include<string.h>
// Read only region start

int SecondLargest(int input1,int input2[])
{
// Read only region end
//write code here

Void print2largest(int arr[],int arr_size)
}





我尝试了什么:



我已经尝试了很多,但是遇到了很多错误



What I have tried:

I have tried a lot,but getting lot of errors

推荐答案

也许你习惯了Java。首先,你无法在C中传递类似C的数组。


Maybe you are used to Java. First of all you cannot pass an array like that in C. in C

int arr1[10];
int arr2[20];





两种不同的类型和 arr [] 根本就没有类型,这就是你的代码无法编译的原因。


以下是如何传递数组的示例:使用示例将数组传递给C编程中的函数 [ ^ ]



然后你可以从 getLargest开始(int size,int * arr) ...



我建议你避免排序,花费很多CPU。



如果没有排序,你可以实现 getSecondLargest(int size,int maximum,int * arr)思考



你可能想要漂亮的代码并保持一致。不是一个大写的函数和一个小写的函数。



Are two different types and arr[] is no type at all that is why your code does not compile.

Here is an example of how you can pass an array: Passing array to function in C programming with example[^]

Then you can start with getLargest(int size, int *arr) ...

I suggest you avoid sorting, costs a lot of CPU.

Without sorting you can implement getSecondLargest(int size, int largest, int *arr) with a bit of thinking

And you might want pretty code and be consistent. Not one function with upper-case and one with lower-case.


不要因为成本而对数组进行排序,因此不需要。遍历数组以搜索值。将max2(第二大值)的初始值设置为-1。



解决方案非常简单,找到新的mxaimum 比max2是旧的最大值。
Do not sort the array because of the cost and it is not needed. Run over the array for the search of the value. Set the initial value of max2 (second biggest value) of -1.

the solution is quite easy, when you find a new mxaimum than max2 is the old maximum.


这里有一种方法:

将数组从最小到最大排序(如果你不确定如何对数组进行排序,请使用谷歌ints)

现在得到数组的最后一个元素

和presto,鲍勃是你的叔叔
Ok here is one approach:
Sort the array smallest to largest(use google if you are unsure how to sort an array of ints)
Now get the next to last element of the array
and presto, Bob's you uncle


这篇关于请在C中回答这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆