0
Find the largest three number in list
If we use for and if statment,what will be the code?
7 Antworten
+ 2
Your intention is unclear. Do you want to
1. Find the largest out of 3 numbers?
2. Find 3 largest numbers out of an array containing more than 3 numbers?
Or something else?
And please specify a language relevant to your question in the Relevant Tags ☝
You are taking multiple classes in SoloLearn, it's hard to deduce language context.
https://code.sololearn.com/W3uiji9X28C1/?ref=app
Add a link to your code in the Description for analysis purposes. Without a code to review, it's hard to tell what the problem is, and what you want to do ...
https://www.sololearn.com/post/75089/?ref=app
0
GIVE A TRY AND LOOK FOR YOUR MISTAKES, STILL HAVE PROBLEM , WE ARE HERE TO HELP
☺️
IN PYTHON
Here I assumed ,you asked how to find largest three digit number in the list, containing different numbers
a=[3,256,278,268,843,1019]#some list
for i in a:
if len(str(i)) != 3:
a.remove(i)
print(max(a))
USING LIST COMPREHENSION
a=[3,82,722,890,982,1823]
print(max([i for i in a if len(str(i)) == 3]))
0
What is largest three number? You mean third largest number? If yes, then the solution is like below:
(Assuming you want it in C++, C or Java and you use an integer array)
**********
int max3=arr[0]
for(int i=0, max=arr[0], max2=arr[0];i<length;++i)
{
if(arr[i]>max)
{
max3=max2;
max2=max;
max=arr[i];
}
else if(arr[i]>max2)
{
max3=max2;
max2=arr[i];
}
else if(arr[i]>max3)
{
max3=arr[i];
}
}
//Now max3 has the 3rd highest value in the array/list
**********
0
Ok TT
0
Time To Code okay =)
0
x = [9,8,1,2,5,3,4,11,12]
#x.sort()
#for i in range(3): print("Max",i+1, " : ", x[len(x)-(1+i)])
x.sort(reverse=True)
for i in range(3): print("Max",i+1, " : ", x[i])
# Keep learning & happy coding :D
0
In C
int a[3];
for(int i=0;i<3;i++){
printf("\na[i]=");
scanf("%d",&a[i]);
}
int max=a[0];
for(int i=0;i<n;i++){
if(a[i]>max){
max=a[i];
}
}
printf("\nMax is %d",max);