C++ 中從兩個連結串列中計數乘積等於給定值的配對


給定兩個連結串列,任務是使用連結串列的整數元素形成配對,使得它們的乘積等於給定的值,例如 k。連結串列是一系列資料結構,透過連結連線在一起。

輸入

vector v_1 = {5, 7, 8, 10, 11},.
vector v_2 = {6, 4, 3, 2, 0} , int k = 20

輸出

Count of pairs from two linked lists whose product is equal to a given value k are: 2

解釋

The pairs which can be formed using the given linked lists are: (5, 6) = 30(not equals to k), (5, 4) = 20(equals to k), (5, 3) = 15(not equals to k), (5, 2) = 10(not equals to k), (5, 0) = 0(not equals to k), (7, 6) = 42(not equals to k), (7, 4) = 28(not equals to k), (7, 3) = 21(not equals to k), (7, 2) = 14(not equals to k), (7, 0) = 0(not equals to k), (8, 6) = 48(not equals to k), (8, 4) = 32(not equals to k), (8, 3) = 24(not equals to k), (8, 2) = 16(not equals to k), (8, 0) = 0(not equals to k), (10, 6) = 60(not equals to k), (10, 4) = 40(not equals to k), (10, 3) = 30(not equals to k), (10, 2) = 20(not equals to k), (10, 0) = 0(not equals to k), (11, 6) = 66(not equals to k), (11, 4) = 44(not equals to k), (11, 3) = 3(not equals to k), (11, 2) = 22(not equals to k), (11, 0) = 0(not equals to k). So, clearly there are 2 pairs which are equal to the given product.

輸入

vector v_1 = {2, 3, 5, 6},.
vector v_2 = {6, 4, 3} , int k = 9

輸出

Count of pairs from two linked lists whose sum is equal to a given value k are: 1

解釋

The pairs which can be formed using the given linked lists are: (2, 6) = 12(not equals to k), (2, 4) = 8(not equals to k), (2, 3) = 6(not equals to k), (3, 6) = 18(not equals to k), (3, 4) = 12(not equals to k), (3, 3) = 9(equals to k), (5, 6) = 30(not equals to k), (5, 4) = 20(not equals to k), (5, 3) = 15(not equals to k), (6, 6) = 36(not equals to k), (6, 4) = 24(not equals to k), (6, 3) = 18(not equals to k),. So, clearly there is 1 pair which is equal to the given sum.

下面程式中使用的方案如下

  • 輸入 k 的值和整數型別的值到兩個向量中,以便我們可以將向量傳遞給形成連結串列。

  • 建立一個函式,該函式將使用作為引數傳遞給函式的向量來建立一個連結串列。

  • 遍歷迴圈直到向量的長度,並建立類指標物件

    ListNode

    • 遍歷迴圈,直到 ptr->next 不等於 NULL,並將 ptr 設定為 ptr->next

    • 在 ptr->next 中設定 vector[i]

    • 返回開始

  • 建立一個函式,該函式將返回與給定乘積匹配的配對的數量。

    • 取一個臨時變數 count 並將其設定為 0

    • 建立兩個指標物件,即 *first_list 用於第一個連結串列,*second_list 用於第二個連結串列。

    • 從第一個列表的起始指標開始迴圈,直到列表為空。

    • 在迴圈內部,從第二個列表的起始指標開始另一個迴圈,直到列表為空。

    • 在迴圈內部,檢查 IF (first_list->data * second_list->data) == k 則將 count 增加 1

    • 返回 count

  • 列印結果。

示例

即時演示

#include<bits/stdc++.h>
using namespace std;
class ListNode{
public:
   int data;
   ListNode *next;
   ListNode(int data){
      this->data = data;
      next = NULL;
   }
};
ListNode *CreateList(vector v){
   ListNode *start = new ListNode(v[0]);
   for (int i = 1; i < v.size(); i++){
      ListNode *ptr = start;
      while (ptr->next != NULL){
         ptr = ptr->next;
      }
      ptr->next = new ListNode(v[i]);
   }
   return start;
}
int product_pair(ListNode *start_1, ListNode *start_2, int k){
   int count = 0;
   ListNode *first_list , *second_list;
   for (first_list = start_1; first_list != NULL; first_list = first_list->next){
      for (second_list = start_2; second_list != NULL; second_list = second_list->next){
         if ((first_list->data * second_list->data) == k){
            count++;
         }
      }
   }
   return count;
}
int main(){
   vector<int> v_1 = {5, 7, 8, 10, 11};
   ListNode* start_1 = CreateList(v_1);
   vector v_2 = {6, 4, 3, 2, 0};
   ListNode* start_2 = CreateList(v_2);
   int k = 30;
   cout<<"Count of pairs from two linked lists whose product is equal to a given value k are: "<<product_pair(start_1, start_2, k);
}

輸出

如果我們執行以上程式碼,它將生成以下輸出:

Count of pairs from two linked lists whose product is equal to a given value k are: 2

更新於: 2020-11-02

93 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.