๊ธฐ๋ก๋ฐฉ

BOJ_10820 : ๋ฌธ์ž์—ด ๋ถ„์„ ๋ณธ๋ฌธ

CodingTest/C++

BOJ_10820 : ๋ฌธ์ž์—ด ๋ถ„์„

Soom_1n 2022. 6. 2. 21:18

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

10820๋ฒˆ: ๋ฌธ์ž์—ด ๋ถ„์„

๋ฌธ์ž์—ด N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž, ๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๊ฐ ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ๋ฌธ์ž์—ด์„ ํ•œ์ค„ ์”ฉ ์ž…๋ ฅ๋ฐ›์•„, ์ˆซ์ž๋กœ ๋ณ€ํ™˜ ํ›„ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

#include<iostream>
#include<string>
using namespace std;

int main(void) {
	string s;

	while (getline(cin, s))
	{
		int sum[4] = { 0 };
		for (char c : s) {
			if ('a' <= c && c <= 'z')
			{
				sum[0]++;
			}
			else if ('A' <= c && c <= 'Z')
			{
				sum[1]++;
			}
			else if ('0' <= c && c <= '9')
			{
				sum[2]++;
			}
			else if (c == ' ')
			{
				sum[3]++;
			}
		}
		printf("%d %d %d %d \n", sum[0], sum[1], sum[2], sum[3]);
	}
	return 0;
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • <string> ํ—ค๋”์˜ getline(cin, s) ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋ฐ›๊ณ , ํ•œ ๋ฌธ์ž์”ฉ ๊ฒ€์‚ฌํ•ด์„œ sum ๋ฐฐ์—ด์— ๋”ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ while(getline(cin, s)) ๊ตฌ์กฐ๋กœ ์ž…๋ ฅ์ด ์—†์œผ๋ฉด ๋ฐ˜๋ณต์ด ๋๋‚˜๋Š” ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค.
728x90

'CodingTest > C++' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1259 : ํŒฐ๋ฆฐ๋“œ๋กฌ์ˆ˜  (0) 2022.06.02
BOJ_15351 : ์ธ์ƒ ์ ์ˆ˜  (0) 2022.06.02
BOJ_1181 : ๋‹จ์–ด ์ •๋ ฌ  (0) 2022.06.02
BOJ_2292 : ๋ฒŒ์ง‘  (0) 2022.06.01
BOJ_2231 : ๋ถ„ํ•ดํ•ฉ  (0) 2022.06.01