热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

来自两个链表的偶对数量,乘积等于给定值

来自两个链表的偶对数量,乘积等于给定值原文:https://ww

来自两个链表的偶对数量,乘积等于给定值

原文:https://www.geeksforgeeks.org/count-pairs-from-two-linked-lists-whose-product-is-equal-to-a-given-value/

给定两个链表(可以排序或不排序),它们的大小分别为n1n2。 给定值X。问题是要计算两个列表中乘积等于给定值x的所有对。

注意:该对必须具有每个链表中的一个元素。

示例

Input : list1 = 3->1->5->7
list2 = 8->2->5->3
X = 10
Output : 1
The pair is: (5, 2)
Input : list1 = 4->3->5->7->11->2->1
list2 = 2->3->4->5->6->8-12
X = 9
Output : 1
The pair is: (3, 3)

一种简单的方法是使用两个循环从两个链表中选择元素,并检查该对的乘积是否等于给定值 X。 计算所有这样的对并打印结果。

下面是上述方法的实现:

C++


// C++ program to count all pairs from both the
// linked lists whose product is equal to
// a given value
#include
using namespace std;
/* A Linked list node */
struct Node {
    int data;
    struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    /* put in the data */
    new_node->data = new_data;
    /* link the old list to the new node */
    new_node->next = (*head_ref);
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
// Function to count all pairs from both the linked lists
// whose product is equal to a given value
int countPairs(struct Node* head1, struct Node* head2, int x)
{
    int count = 0;
    struct Node *p1, *p2;
    // Traverse the 1st linked list
    for (p1 = head1; p1 != NULL; p1 = p1->next) {
        // for each node of 1st list
        // Traverse the 2nd list
        for (p2 = head2; p2 != NULL; p2 = p2->next) {
            // if sum of pair is equal to 'x'
            // increment count
            if ((p1->data * p2->data) == x)
                count++;
        }
    }
    // required count of pairs
    return count;
}
// Driver Code
int main()
{
    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
    // create linked list1 3->1->5->7
    push(&head1, 7);
    push(&head1, 5);
    push(&head1, 1);
    push(&head1, 3);
    // create linked list2 8->2->5->3
    push(&head2, 3);
    push(&head2, 5);
    push(&head2, 2);
    push(&head2, 8);
    int x = 10;
    cout << "Count = " << countPairs(head1, head2, x);
    return 0;
}


Java


// Java program to count all pairs from both the
// linked lists whose product is equal to
// a given value
class GFG 
{
    /* A Linked list node */
    static class Node 
    {
        int data;
        Node next;
    };
    // function to insert a node at the
    // beginning of the linked list
    static Node push(Node head_ref, int new_data) 
    {
        /* allocate node */
        Node new_node = new Node();
        /* put in the data */
        new_node.data = new_data;
        /* link the old list to the new node */
        new_node.next = head_ref;
        /* move the head to point to the new node */
        head_ref = new_node;
        return head_ref;
    }
    // Function to count all pairs from both the linked lists
    // whose product is equal to a given value
    static int countPairs(Node head1, Node head2, int x)
    {
        int count = 0;
        Node p1, p2;
        // Traverse the 1st linked list
        for (p1 = head1; p1 != null; p1 = p1.next) 
        {
            // for each node of 1st list
            // Traverse the 2nd list
            for (p2 = head2; p2 != null; p2 = p2.next)
            {
                // if sum of pair is equal to 'x'
                // increment count
                if ((p1.data * p2.data) == x) 
                {
                    count++;
                }
            }
        }
        // required count of pairs
        return count;
    }
    // Driver Code
    public static void main(String[] args) 
    {
        Node head1 = null;
        Node head2 = null;
        // create linked list1 3.1.5.7
        head1 = push(head1, 7);
        head1 = push(head1, 5);
        head1 = push(head1, 1);
        head1 = push(head1, 3);
        // create linked list2 8.2.5.3
        head2 = push(head2, 3);
        head2 = push(head2, 5);
        head2 = push(head2, 2);
        head2 = push(head2, 8);
        int x = 10;
        System.out.print("Count = " + countPairs(head1, head2, x));
    }
}
// This code is contributed by Rajput-Ji


C


// C# program to count all pairs from both the
// linked lists whose product is equal to
// a given value
using System;
class GFG 
{
    /* A Linked list node */
    class Node 
    {
        public int data;
        public Node next;
    };
    // function to insert a node at the
    // beginning of the linked list
    static Node push(Node head_ref, int new_data) 
    {
        /* allocate node */
        Node new_node = new Node();
        /* put in the data */
        new_node.data = new_data;
        /* link the old list to the new node */
        new_node.next = head_ref;
        /* move the head to point to the new node */
        head_ref = new_node;
        return head_ref;
    }
    // Function to count all pairs from both the linked lists
    // whose product is equal to a given value
    static int countPairs(Node head1, Node head2, int x)
    {
        int count = 0;
        Node p1, p2;
        // Traverse the 1st linked list
        for (p1 = head1; p1 != null; p1 = p1.next) 
        {
            // for each node of 1st list
            // Traverse the 2nd list
            for (p2 = head2; p2 != null; p2 = p2.next)
            {
                // if sum of pair is equal to 'x'
                // increment count
                if ((p1.data * p2.data) == x) 
                {
                    count++;
                }
            }
        }
        // required count of pairs
        return count;
    }
    // Driver Code
    public static void Main(String[] args) 
    {
        Node head1 = null;
        Node head2 = null;
        // create linked list1 3->1->5->7
        head1 = push(head1, 7);
        head1 = push(head1, 5);
        head1 = push(head1, 1);
        head1 = push(head1, 3);
        // create linked list2 8->2->5->3
        head2 = push(head2, 3);
        head2 = push(head2, 5);
        head2 = push(head2, 2);
        head2 = push(head2, 8);
        int x = 10;
        Console.Write("Count = " + countPairs(head1, head2, x));
    }
}
// This code is contributed by Rajput-Ji

输出

Count = 1

时间复杂度O(N ^ 2)




如果您喜欢 GeeksforGeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 GeeksforGeeks 主页上,并帮助其他 Geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • c语言\n不换行,c语言printf不换行
    本文目录一览:1、C语言不换行输入2、c语言的 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
author-avatar
mobiledu2502860057
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有