[알고리즘]/알고리즘

[알고리즘] 백준 2870번 : 수학숙제 (실버4)

쿠릉쿠릉 쾅쾅 2022. 3. 14. 11:47
728x90

 

 

https://www.acmicpc.net/problem/2870

 

2870번: 수학숙제

종이에서 찾은 숫자의 개수를 M이라고 하면, 출력은 M줄로 이루어져야 한다. 각 줄에는 종이에서 찾은 숫자를 하나씩 출력해야 한다. 이때, 비내림차순으로 출력해야 한다. 비내림차순은 내림차

www.acmicpc.net

 

 

📌 풀이1

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public  class Main {
	
    public static void main (String[] args) {    
    	
    	Scanner sc = new Scanner(System.in);
    	
    	int N = sc.nextInt();
    	
    	List<BigInteger> li = new ArrayList<>();
    	
    	for(int i=0; i<N; i++) {
    		String word = sc.next();
    		
    		String[] str = word.split("[^0-9]");
    		
    		for(String s : str) 
    			if(!"".equals(s)) 
    				li.add(new BigInteger(s));
    	}
    	
    	Collections.sort(li);
    	li.stream().forEach(System.out::println);
    	
    }
    
}

 

📌 풀이2

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public  class Main {
	
    public static void main (String[] args) {    
    	
    	Scanner sc = new Scanner(System.in);
    	
    	int N = sc.nextInt();
    	
    	List<BigInteger> li = new ArrayList<>();
    	
    	for(int i=0; i<N; i++) {
    		String s = sc.next();
    		int leng = s.length();
    		StringBuilder sb = new StringBuilder();
    		for(int k=0; k<leng; k++) {
    			
    			if('0'<=s.charAt(k) && s.charAt(k)<='9') {
    				sb.append(s.charAt(k)-'0');
    				
    				if(!(k+1<leng && '0'<=s.charAt(k+1) && s.charAt(k+1)<='9')) {
    					li.add(new BigInteger(sb.toString()));
        				sb.setLength(0);
    				}
    				
    			}
    		}
    	}
    	
    	Collections.sort(li);
    	li.stream().forEach(System.out::println);
    	
    }
    
}

 

 

728x90