C++ 程序:找出比 Jay 身高高的人
以下是使用 C++ 编写的程序,计算比 Jay 身高高的人数并输入比 Jay 身高高的人的名字:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int jayHeight = 150;
vector<string> names = {'cyp', 'mwy', 'zp', 'wxa', 'zl', 'yl'};
vector<string> tallerNames;
for (const string& name : names) {
int height;
cout << "请输入" << name << "的身高:";
cin >> height;
if (height > jayHeight) {
tallerNames.push_back(name);
}
}
int tallerCount = tallerNames.size();
cout << "比 Jay 身高高的人数为:" << tallerCount << endl;
cout << "比 Jay 身高高的人的名字为:" << endl;
for (const string& name : tallerNames) {
cout << name << endl;
}
return 0;
}
这段程序使用了iostream、string和vector库来进行输入输出和存储名字的操作。首先,定义了 Jay 的身高为 150,以及一个存储名字的vector容器names。然后,定义另一个vector容器tallerNames来存储比 Jay 身高高的人的名字。
接下来,使用一个for循环遍历names容器中的每个名字,并提示用户输入对应人员的身高。如果该人员的身高大于 Jay 的身高,则将其名字添加到tallerNames容器中。
完成遍历后,使用tallerNames.size()来获取比 Jay 身高高的人数,并将其存储在tallerCount变量中。然后,使用cout语句将比 Jay 身高高的人数输出到显示器上。
最后,使用另一个for循环遍历tallerNames容器中的每个名字,并使用cout语句将每个名字单独输出到显示器上,以显示比 Jay 身高高的人的名字。
原文地址: https://www.cveoy.top/t/topic/SN0 著作权归作者所有。请勿转载和采集!