programmers level1 알고리즘 문제 - 최대값과 최소값 Java
2가지 방법
public class maxmin {
public String getMinMaxString(String str) {
String result ="";
String[] str2 = str.split(" ");
int len = str2.length;
int[] temp= new int[len];
int temp2 =0;
for(int i =0; i <str2.length; i++){
temp[i] = Integer.parseInt(str2[i]);
}
if(str.length()<100){
for( int i =0; i<temp.length; i++){
for( int j=1; j<temp.length-i; j++){
if(temp[j-1]<temp[j]){
temp2 = temp[j-1];
temp[j-1]= temp[j];
temp[j] =temp2;
}
}
}
int temp_len = temp.length-1;
result = temp[temp_len]+" "+ temp[0];
}else{
int max=temp[0];
int min=temp[0];
for( int i =0; i<temp.length; i++){
if(temp[i]>max){
max = temp[i];
}
if(temp[i]<min){
min = temp[i];
}
}
result = min + " "+ max;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1 2 3 4";
maxmin minMax = new maxmin();
//아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println("최소값과 최대값은?" + minMax.getMinMaxString(str));
}
}
public class maxmin {
public String getMinMaxString(String str) {
String result ="";
String[] str2 = str.split(" ");
int len = str2.length;
int[] temp= new int[len];
int temp2 =0;
for(int i =0; i <str2.length; i++){
temp[i] = Integer.parseInt(str2[i]);
}
if(str.length()<100){
for( int i =0; i<temp.length; i++){
for( int j=1; j<temp.length-i; j++){
if(temp[j-1]<temp[j]){
temp2 = temp[j-1];
temp[j-1]= temp[j];
temp[j] =temp2;
}
}
}
int temp_len = temp.length-1;
result = temp[temp_len]+" "+ temp[0];
}else{
int max=temp[0];
int min=temp[0];
for( int i =0; i<temp.length; i++){
if(temp[i]>max){
max = temp[i];
}
if(temp[i]<min){
min = temp[i];
}
}
result = min + " "+ max;
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1 2 3 4";
maxmin minMax = new maxmin();
//아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println("최소값과 최대값은?" + minMax.getMinMaxString(str));
}
}
댓글
댓글 쓰기