C++ 搜索数组中的特定数字并计算它出现的次数
我有一个从0到100岁的年龄数组。您一直输入年龄,直到输入sentinel数据(-99) 然后我需要计算每个年龄段出现的次数 示例输出:C++ 搜索数组中的特定数字并计算它出现的次数,c++,arrays,loops,for-loop,C++,Arrays,Loops,For Loop,我有一个从0到100岁的年龄数组。您一直输入年龄,直到输入sentinel数据(-99) 然后我需要计算每个年龄段出现的次数 示例输出: 5岁的人数为3人 8岁的人数是1 9岁的人数是1 10岁的人数是1 17岁的人数为1人 20岁的人数为2人 100岁的人数是1 但是,当我执行此操作时,我的输出如下所示: 5岁的人数是3 10岁的人数是1 100岁的人数是1 20岁的人数为2人 5岁的人数是3 8岁的人数是1 20岁的人数为2人 5岁的人数是3 9岁的人数是1 17岁的人数是1 我需要修复它,
5岁的人数为3人
8岁的人数是1
9岁的人数是1
10岁的人数是1
17岁的人数为1人
20岁的人数为2人
100岁的人数是1 但是,当我执行此操作时,我的输出如下所示: 5岁的人数是3
10岁的人数是1
100岁的人数是1
20岁的人数为2人
5岁的人数是3
8岁的人数是1
20岁的人数为2人
5岁的人数是3
9岁的人数是1
17岁的人数是1 我需要修复它,使它只显示5的一次,等等 这是我的密码:
#include <iostream>
using namespace std;
const int MAXSIZE = 100;
int ages[MAXSIZE];
int findNumOfAges(int ages[], int pos, int i);
int main()
{
int pos = 0;
int age;
for(int count = 0; count < MAXSIZE; count++)
{
ages[count] = 0;
}
do
{
cout << "Please input an age from one to 100, put -99 to stop." << endl;
cin >> age;
if (age >=0 && age <=100)
{
ages[pos] = age;
}
pos++;
}while(age != -99);
for(int i = 0; i < pos; i++)
{
int numOfAges;
numOfAges = findNumOfAges(ages, pos, i);
cout << "The number of people age " << ages[i] << " is " << numOfAges << endl;
}
}
int findNumOfAges(int ages[], int pos, int i)
{
int numOfAges = 0;
for(int j = 0; j < pos; j++)
{
if(ages[i] == ages[j])
{
numOfAges += 1;
}
}
return numOfAges;
}
#包括
使用名称空间std;
常数int MAXSIZE=100;
整数年龄[MAXSIZE];
int findNumOfAges(int ages[],int pos,int i);
int main()
{
int pos=0;
智力年龄;
对于(int count=0;count=0&&age此循环的第一个
do
{
cout << "Please input an age from one to 100, put -99 to stop." << endl;
cin >> age;
if (age >=0 && age <=100)
{
ages[pos] = age;
}
pos++;
}while(age != -99);
也可以从一开始就使用std::map,而不是数组。:)
比如说
#include <algorithm>
//,,,
int findNumOfAges( const int ages[], int pos, int i)
{
const int *first = std::find( ages, ages + i, ages[i] );
if ( first != ages + i ) return 0;
return ( std::count( first, ages + pos, ages[i] ) );
}
#include <map>
//...
std::map<int, int> m;
do
{
cout << "Please input an age from one to 100, put -99 to stop." << endl;
cin >> age;
if (age >=0 && age <=100)
{
++m[age];
}
} while( age != -99 );
#包括
//...
std::map m;
做
{
年龄;
如果(年龄>=0&&age此循环的第一个
do
{
cout << "Please input an age from one to 100, put -99 to stop." << endl;
cin >> age;
if (age >=0 && age <=100)
{
ages[pos] = age;
}
pos++;
}while(age != -99);
也可以从一开始就使用std::map,而不是数组。:)
比如说
#include <algorithm>
//,,,
int findNumOfAges( const int ages[], int pos, int i)
{
const int *first = std::find( ages, ages + i, ages[i] );
if ( first != ages + i ) return 0;
return ( std::count( first, ages + pos, ages[i] ) );
}
#include <map>
//...
std::map<int, int> m;
do
{
cout << "Please input an age from one to 100, put -99 to stop." << endl;
cin >> age;
if (age >=0 && age <=100)
{
++m[age];
}
} while( age != -99 );
#包括
//...
std::map m;
做
{
年龄;
如果(age>=0&&age,则需要第二个数组
首先获取ages数组中的所有ages(您已经这样做了)
然后制作第二个大小为100的int数组
遍历ages数组中的每个整数,直到达到-99
对于每个年龄,使用年龄作为第二个int数组的索引。
在该索引的第二个数组中添加一个
然后遍历第二个数组
如果其索引处的值为零,则不执行任何操作。
如果该值大于零,则打印:
“年龄人口数”您需要第二个数组
首先获取ages数组中的所有ages(您已经这样做了)
然后制作第二个大小为100的int数组
遍历ages数组中的每个整数,直到达到-99
对于每个年龄,使用年龄作为第二个int数组的索引。
在该索引的第二个数组中添加一个
然后遍历第二个数组
如果其索引处的值为零,则不执行任何操作。
如果该值大于零,则打印:
“适龄人口数量”它必须使用数组吗?你能使用STL容器吗?是的,它必须使用数组不需要存储所有的年龄,只需要为每个可能的年龄设置一个计数器数组,为每个输入的年龄设置一个合适的计数器…它必须使用数组吗?你能使用STL容器吗?是的,它必须使用数组不需要存储所有年龄,只需为每个可能的年龄设置一组计数器,并为每个输入的年龄设置相应的计数器。。。