Deep_Dev
article thumbnail

๋ฌธ์ œ

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

์ž…๋ ฅ

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

์ถœ๋ ฅ

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

 

 

StringTokenizer๋ฅผ ์•Œ๊ธฐ์ „์—๋Š”

int cnt=1;

for(char x:s.toCharArray()){

     if(x==' ')

          cnt++;

} ๋กœ cnt๊ฐ’์œผ๋กœ ์ถœ๋ ฅํ•ด๋ดค๋Š”๋ฐ, ์‹คํ–‰ํ•˜๋ฉด ๊ฒฐ๊ณผ๋Š” ์ž˜ ๋‚˜์˜ค๋Š”๋ฐ ๋ฐฑ์ค€์—์„œ ์ฑ„์ ์ด ์•ˆ๋œ๋‹ค . ใ…‹ใ…‹


import java.util.*;

public class Main {
    public static void main(String[] args){

        Scanner kb=new Scanner(System.in);
        String s=kb.nextLine();

        StringTokenizer st=new StringTokenizer(s," ");
        System.out.print(st.countTokens());
    }
}