자료구조와 알고리듬 With Java/[Study] BAEKJOON 프로그래머스 CodeUp LeetCode

Code up 2016 천단위 구분기호

계란💕 2022. 3. 16. 09:40

  Ex) 길이가 n (n <= 200)인 수가 입력됐을 때, 콤마를 삽입.

 

<hide/>
import java.util.*;
interface IStackableComma1{
	
	boolean empty();
	void pop();
	void push(int nData);
	int peek();
}

public class  insertComma implements IStackableComma1 {
	
	private int [] mnArray;
	private int mnTop;
	private int mnCapacity;
	
	public insertComma(int nCapacity) {
		
		mnArray = new int[nCapacity];
		mnCapacity = nCapacity;
		mnTop = 0;
	}
	
	public boolean empty() {
		
		return (mnTop == 0);
	}
	
	public int peek() {
		if(empty() == true) {
			System.out.println("Stack underflow!");
			return -1;
		}
		return mnArray[mnTop - 1];
	}
	
	public void pop() {
		if(empty() == true) {
			System.out.println("Stack underflow!");
			return;
		}
		--mnTop;
	}
		
	public void push(int nData) {
		if(true == full()) {
			System.out.println("Stack Overflow!");
			return;
		}
		mnArray[mnTop++] = nData;
	}
	
	public boolean full() {
		return (mnCapacity == mnTop);
	}
	
	public void print() {
		
		for(int i = 0; i < mnTop; ++i) {
			
			System.out.printf("%d ", mnArray[i]);
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		int nCapacity = 200;
		int length = scan.nextInt();		// 자릿수 n을 입력받는다.
		String str = scan.next();		// 숫자가 아닌 문자열로 입력받는다.
	
		Comma x = new Comma(nCapacity);

		if(length % 3 == 0) {
					
			for(int i = 0; i < length / 3 ; ++i) {
	
				for(int j = 0 ; j < 3; ++j ) {
					System.out.format("%c"  , str.charAt(i * 3 + j));
				}
				if( i == (length / 3) -1) {
					return;
				}
				System.out.printf(",");
			}	
			
		}else if( length % 3  == 1) {
			
			System.out.format("%c"  , str.charAt(0));
		
			for(int i = 0 ;i < length / 3 ; ++i) {
				
				System.out.printf(",");
				
				for(int j = 1 ; j <= 3; ++j) {
				
					System.out.format("%c"  , str.charAt(3 * i + j));
				}
			}
		}else {
			System.out.format("%c"  , str.charAt(0));
			System.out.format("%c"  , str.charAt(1));
			
			for(int i = 0 ;i < length / 3 ; ++i) {
				
				System.out.printf(",");
				
				for(int j = 1 ; j <= 3; ++j) {
				
					System.out.format("%c"  , str.charAt(3 * i + j + 1));
				}

			}
		}
	}		
}

  Note) 실행 결과