์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ / Java ] 1152 - ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

Forest Yun 2021. 6. 20. 01:18
728x90

๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

๋ฌธ์ œ

์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๋„์–ด์“ฐ๊ธฐ๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์—๋Š” ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์„๊นŒ? ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ํ•œ ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฉด ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ชจ๋‘ ์„ธ์–ด์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ

์ฒซ ์ค„์— ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๋„์–ด์“ฐ๊ธฐ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‹จ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ, ๊ณต๋ฐฑ์ด ์—ฐ์†ํ•ด์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด์˜ ์•ž๊ณผ ๋’ค์—๋Š” ๊ณต๋ฐฑ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

import java.util.Scanner;

//2021.06.19 1152๋ฒˆ : ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜
public class baekjoon_1152 {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		
		String str;
		String[] strArray= {};
		int num;
		
		str=scanner.nextLine();
		
		if(!str.isBlank()) {
			//๋ฌธ์ž์—ด์˜ ์•ž๊ณผ ๋’ค์—๋Š” ๊ณต๋ฐฑ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.->trim ๋ฉ”์†Œ๋“œ๋กœ ์•ž ๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ
			str=str.trim();
			//๋‹จ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„-> split๋ฉ”์†Œ๋“œ๋กœ ๋ฐ˜ํ™˜๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
			strArray=str.split(" ");
			num=strArray.length;
		}
		else
			num=0;
		
		System.out.println(num);
		
		scanner.close();
	}
}

 

  • ํ•œ ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฉด ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ชจ๋‘ ์„ธ์–ด์•ผ ํ•œ๋‹ค. -> ๋‹จ์–ด ์ค‘๋ณต ์ œ๊ฑฐ ๋ถˆํ•„์š”
  • ๋‹จ์–ด๋Š” ๋„์–ด์“ฐ๊ธฐ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„  -> split(๊ตฌ๋ถ„์ž " ") ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
  • ๋ฌธ์ž์—ด์˜ ์•ž๊ณผ ๋’ค์—๋Š” ๊ณต๋ฐฑ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.  -> strim() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ
  • ํ‹€๋ฆฐ ๋ถ€๋ถ„ : ์ž…๋ ฅ ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ 0์ด ์•„๋‹ˆ๋ผ 1์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

 

 

 

 


 

 

 

 

1. String.isBlank ์™€ String.isEmpty 

boolean java.lang.String.isBlank()

Returns true if the string is empty or contains only white space codepoints,otherwise false.
: String์ด ๊ณต๋ฐฑ์ผ ๊ฒฝ์šฐ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean java.lang.String.isEmpty()

Returns:true if length() is 0, otherwise false
: String์˜ ๊ธธ์ด๊ฐ€ 0์ผ ๊ฒฝ์šฐ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ๊ณต๋ฐฑ์ด ํ•˜๋‚˜ ์žˆ์„ ๊ฒฝ์šฐ ๊ธธ์ด๊ฐ€ 0์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— fasle ๋ฐ˜ํ™˜.

 

 

2. String.trim()

String java.lang.String.trim()

Returns: a string whose value is this string, with all leading and trailing space removed, or this string if it has no leading or trailing space.
:์›๋ณธ String์˜ ์•ž๊ณผ ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ String ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์•ž๊ณผ ๋’ค์— ๊ณต๋ฐฑ์ด ์—†๋‹ค๋ฉด ์›๋ณธ String ๊ฐ์ฒด๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Otherwise, if all characters in this string are space (as defined above), then a String object representing an empty string is returned.
:๋งŒ์•ฝ ํ•ด๋‹น String์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ๊ณต๋ฐฑ์ผ ๊ฒฝ์šฐ ๋นˆ ๋ฌธ์ž์—ด ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

 

 

3. String.split(String regex)

String[] java.lang.String.split(String regex)

Splits this string around matches of the given regular expression.
: ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ์–ด์ง„ ์ •๊ทœ์‹์œผ๋กœ ํ•ด๋‹น String์„ ๋ถ„ํ• ํ•˜๋ผ.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
:์ด ๋ฉ”์†Œ๋“œ๋Š” split(์ •๊ทœ์‹, ์ œํ•œ: 0) ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ›„ํ–‰์€ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

+
String[] java.lang.String.split(String regex, int limt)

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
: limt ์€ ์ •๊ทœ์‹ ํŒจํ„ด์ด ์ ์šฉ๋˜๋Š” ํšŸ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์˜ ๊ธธ์ด์— ์˜ํ–ฅ์„ ์ค€๋‹ค.
728x90