-
-
Save tbuehlmann/5517792 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data = [1000,1000,5,1000,4,12,-3,999,0,1000] | |
| # declare and initialize variables for the two largest | |
| max, max_2 = nil, nil | |
| # compute the two largest | |
| data.each_with_index do |n, index| | |
| case | |
| when index == 0 | |
| max = n | |
| when index == 1 | |
| if n > max | |
| max_2 = max | |
| max = n | |
| else | |
| max_2 = n | |
| end | |
| else | |
| if n > max | |
| max_2 = max | |
| max = n | |
| end | |
| max_2 = n if n > max_2 | |
| end | |
| end | |
| # write out the two largest | |
| puts max | |
| puts max_2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class fourtyseven2 | |
| { | |
| public static void main (String[] args) | |
| { | |
| int[] data = {1000,1000,5,1000,4,12,-3,999,0,1000}; | |
| //check whether there are max and secondmax | |
| boolean allthesame=true; | |
| for(int value:data) | |
| { | |
| allthesame=(data[0]==value)? true:false; | |
| if (allthesame==false) | |
| { | |
| break; | |
| } | |
| } | |
| //declare & init | |
| int max=data[0], secondmax=data[0]; | |
| if(allthesame==false) | |
| { | |
| //comp max | |
| for(int value:data) | |
| { | |
| if(value>max) | |
| { | |
| max=value; | |
| } | |
| } | |
| //change secondmax to value lower than max | |
| for(int value:data) | |
| { | |
| if(value<max) | |
| { | |
| secondmax=value; | |
| break; | |
| } | |
| } | |
| //calc secondmax | |
| for(int value:data) | |
| { | |
| if(value<max && value>secondmax) | |
| { | |
| secondmax=value; | |
| } | |
| } | |
| } | |
| //write out the two largest | |
| System.out.println("max: "+max); | |
| if(allthesame==false) | |
| { | |
| System.out.println("secondmax: "+secondmax); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment