【问题标题】:search array for specific number and count how many times it appears搜索特定数字的数组并计算它出现的次数
【发布时间】:2014-03-06 21:11:12
【问题描述】:

我有一个从 0 到 100 的年龄数组。你一直输入年龄,直到输入哨兵数据(-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 一次,等等。

这是我的代码:

    #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;
}

【问题讨论】:

  • 一定要用数组吗?你可以改用 STL 容器吗?
  • 是的,它必须使用数组
  • 无需存储所有年龄,只需为每个可能的年龄设置一组计数器,并为每个输入的年龄设置适当的计数器...

标签: c++ arrays loops for-loop


【解决方案1】:

首先是这个循环

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);

无效。即使输入的数字不属于可接受的范围,您也会增加 pos。

它应该如下所示

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;

    }

} while( age != -99 && pos < MAXSIZE );

同样在函数中你应该检查元素是否已经被计算在内。 我会这样写

int findNumOfAges( const int ages[], int pos, int i)
{
   int numOfAges = 0;

   int j = 0;
   while ( j < i && ages[i] != ages[j] ) j++;

   if ( j == i )
   {
       for( ; j < pos; j++ )
       {
          numOfAges += ages[i] == ages[j];
       }
   }

   return numOfAges;
}

在 main 中,您必须检查返回值。如果它等于 0,那么你必须跳过这个元素,因为它已经被计算在内了。

您也可以根据标准算法编写函数。例如

#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] ) );  
}

另一种方式是使用std::map

在这种情况下,您只需从数组构建地图

#include <map>

//...

std::map<int, int> m;

for ( int i = 0; i < pos; i++ ) ++m[ages[i]];

for ( const auto &p : m )
{
   std::cout << p.first << '\t' << p.second << std::endl;
}

你也可以从一开始就使用 std::map 而不是数组。:)

例如

#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 );

【讨论】:

    【解决方案2】:

    你需要第二个数组。

    首先获取年龄数组中的所有年龄。 (你做到了。)

    然后创建第二个大小为 100 的 int 数组。

    遍历年龄数组的每个整数,直到达到 -99。

    对于每个年龄,使用年龄作为第二个 int 数组的索引。 将一个添加到该索引处第二个数组中的任何内容。

    然后遍历第二个数组。

    如果其索引处的值为零,则不执行任何操作。 如果该值大于零,则打印: “年龄的人数”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2016-03-19
      相关资源
      最近更新 更多