C++代码用于判断姓名是男性还是女性
假设,我们在一个数组 'input' 中给定 n 个字符串。这些字符串是姓名,我们需要找出它们是男性姓名还是女性姓名。如果一个姓名以 'a'、'e'、'i' 或 'y' 结尾;可以认为它是女性姓名。我们为字符串中的每个输入打印 'male' 或 'female'。
因此,如果输入类似于 n = 5,input = {"Lily", "Rajib", "Thomas", "Riley", "Chloe"},则输出将是 Female, Male, Male, Female, Female。
步骤
为了解决这个问题,我们将遵循以下步骤:
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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP