เป็นไปได้อย่างไรที่จะนับจำนวนองค์ประกอบในอาเรย์ที่ตรงตามเงื่อนไขบางประการ (C++)

ฉันเป็นมือใหม่ในภาษา C++ และต้องการความช่วยเหลือในคำถามพื้นฐาน ฉันมีชุดข้อมูล (อาร์เรย์) และงานคือการนับจำนวนองค์ประกอบที่ตรงตามเงื่อนไขที่กำหนด

บริษัทจัดเก็บอายุและเงินเดือนของพนักงาน เราต้องเขียนโปรแกรมที่บอกคุณว่ามีคนอายุ L เกิน มีเงินเดือนน้อยกว่า M กี่คน

ป้อนข้อมูล

จำนวนคนงานในบรรทัดแรกของอินพุตมาตรฐาน (0≤N≤100) ขีดจำกัดอายุ (1≤L≤100) และขีดจำกัดเงินเดือน (1≤M≤2,000,000) และต่ำกว่าคืออายุของบุคคลหนึ่งคนต่อบรรทัด (1≤ K≤100) และเงินเดือน (1≤F≤2,000,000)

เอาท์พุต

ในเอาต์พุตมาตรฐานบรรทัดเดียว ต้องเขียนผู้ที่มีอายุ L เกินและมีเงินเดือนน้อยกว่าจำนวนคนงาน M

#include <iostream>
using namespace std;
int main()
{
    int N;
    int K;
    int L;
    int F;
    int M;
    cin >> N >> K >> L >> F >> M;
    int arr[N];
    for (int i=0; i<N; ++i)
    {
        cin >> arr[i];
    }
    int DB=0;
    for (int i=0; i<N; ++i)
 {
                 for (int DB; K>L && F<M; DB=DB+1)
                    {


                    }
    }
    cout << DB << endl;
    return 0;
}

ฉันพยายามแก้ไขปัญหาโดยใช้ for-loops เห็นได้ชัดว่ามีข้อผิดพลาดพื้นฐานในโค้ด คุณช่วยฉันแก้ปัญหาได้ไหม? โค้ดข้างต้นเป็นวิธีการที่ดีหรือมีวิธีแก้ปัญหาที่ดีกว่ามากหรือไม่

ขอบคุณสำหรับความช่วยเหลือล่วงหน้า


person Beginner009    schedule 02.10.2020    source แหล่งที่มา
comment
สวัสดี ยินดีต้อนรับสู่ Stack Overflow! คุณสามารถชี้แจงอินพุตหรือจัดเตรียมตัวอย่างอินพุตได้หรือไม่?   -  person lawruble13    schedule 02.10.2020
comment
เมื่อคุณไม่ทราบวิธีดำเนินการ ก็มักจะเป็นความคิดที่ดีที่จะแบ่งปัญหาออกเป็นขั้นตอน ตัวอย่างเช่น คุณสามารถนับจำนวนองค์ประกอบในอาร์เรย์ได้หรือไม่? ไม่ ไม่ใช่คำตอบเล็กๆ น้อยๆ ของ N ฉันหมายถึงการตั้งค่าการวนซ้ำของคุณ นับจำนวนองค์ประกอบและเพิ่มความคิดเห็น \\ TO DO: I need to check a condition before counting this element.   -  person JaMiT    schedule 02.10.2020
comment
ไลบรารีมาตรฐาน C++ มีอัลกอริทึม count_if() ในส่วนหัว <algorithm> ที่นับจำนวนองค์ประกอบในช่วงที่ตรงตามเงื่อนไขที่ให้มา   -  person Peter    schedule 02.10.2020


คำตอบ (2)


นั่นเป็นวิธีที่สร้างสรรค์ในการแก้ไขปัญหาอย่างแน่นอน! วิธีที่ตรงไปตรงมากว่าในการเข้าถึงสิ่งนี้คือการดูแต่ละองค์ประกอบ และตรวจสอบว่าตรงกันหรือไม่ ดังนี้:

#include <iostream>
using namespace std;
int main(){
  int numWorkers, ageLimit, salaryLimit, matchCount=0;
  cin >> numWorkers >> ageLimit >> salaryLimit;
  for (int i = 0; i < numWorkers; i++){
    int age, salary;
    cin >> age >> salary;
    if (age > ageLimit && salary < salaryLimit){
      matchCount++;
    }
  }
  cout << matchCount << endl;
  return 0;
}
person lawruble13    schedule 02.10.2020

นี่คือวิธีการ โปรดทราบว่านี่เป็นเพียงตัวอย่างตามความคิดเห็นในโพสต์ของคุณ

#include <iostream>
#include <vector>
#include <algorithm>

// you need a way to capture the information of age and salary

class Employee
{
public:
  Employee(int age, int salary) : m_age(age), m_salary(salary) 
  {}
  int Salary() const { return m_salary; }
  int Age() const { return m_age; }
private:
  int m_age{0};
  int m_salary{0}; 
};


int main()
{
  // an array of the employees with age and salary, avoid using native arrays
  std::vector<Employee> employees{{21,10000},{22,12000},
                                  {54,54500},{62,60000}, 
                                  {32,32000}};
  // some salary limit or use cin to read in it
  auto salaryLimit = 33000;

  // use count_if to count the number of employees under salary limit
  auto nrOfEmployees = std::count_if(employees.begin(), employees.end(), 
                       [=](const Employee& e){return e.Salary() < salaryLimit;});
  std::cout << nrOfEmployees << std::endl;

  return 0;
}

หากคุณต้องการลองใช้รหัส

https://onlinegdb.com/Sy-qCXN8v

person AndersK    schedule 02.10.2020