배열 '입력'에 n개의 문자열이 주어진다고 가정합니다. 문자열은 이름이므로 남성 또는 여성 이름인지 확인해야 합니다. 이름이 'a', 'e', 'i' 또는 'y'로 끝나는 경우; 여성의 이름이라고 할 수 있습니다. 문자열의 각 입력에 대해 '남성' 또는 '여성'을 인쇄합니다.
따라서 입력이 n =5, 입력 ={"Lily", "Rajib", "Thomas", "Riley", "Chloe"}인 경우 출력은 여성, 남성, 남성, 여성, 여성이 됩니다.
단계
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
for initialize i := 0, when i < n, update (increase i by 1), do:
s := input[i]
l := size of s
if s[l - 1] is same as 'a' or s[l - 1] is same as 'e' or s[l - 1] is same as 'i' or s[l - 1] is same as 'y', then:
print("Female")
Otherwise,
print("Male") 예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, string input[]) {
for(int i = 0; i < n; i++) {
string s = input[i];
int l = s.size();
if (s[l - 1] == 'a' || s[l - 1] == 'e' || s[l - 1] == 'i' || s[l - 1] == 'y')
cout<< "Female" << endl;
else
cout << "Male" << endl;
}
}
int main() {
int n = 5;
string input[] = {"Lily", "Rajib", "Thomas", "Riley", "Chloe"};
solve(n, input);
return 0;
} 입력
5, {"Lily", "Rajib", "Thomas", "Riley", "Chloe"} 출력
Female Male Male Female Female