[백준 20735] Fifty Shades of Pink (C#, C++) - soo:bak
작성일 :
문제 링크
설명
색 이름이 적힌 문자열들 중에서, 대소문자를 무시하고 pink 또는 rose가 포함된 문자열의 개수를 구하는 문제입니다.
접근법
각 문자열을 소문자로 바꾼 뒤 pink 또는 rose가 부분 문자열로 들어 있는지 확인합니다.
조건을 만족하는 문자열의 개수를 세고, 그 개수가 0이면 문제에서 요구한 문장을 출력합니다. 아니면 개수를 그대로 출력하면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
class Program {
static void Main() {
int n = int.Parse(Console.ReadLine()!);
int count = 0;
for (int i = 0; i < n; i++) {
string s = Console.ReadLine()!.ToLower();
if (s.Contains("pink") || s.Contains("rose"))
count++;
}
if (count == 0)
Console.WriteLine("I must watch Star Wars with my daughter");
else
Console.WriteLine(count);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int count = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (char& ch : s)
ch = (char)tolower(ch);
if (s.find("pink") != string::npos || s.find("rose") != string::npos)
count++;
}
if (count == 0)
cout << "I must watch Star Wars with my daughter\n";
else
cout << count << "\n";
return 0;
}