交叉点代码,用于打印两个数组(过帐列表)的交叉点

intersection code that prints the intersection of two arrays ( posting lists )

本文关键字:交叉点 数组 列表 两个 代码 用于 打印      更新时间:2023-10-16

我写这段代码是为了使用dev c打印两个数组(发布列表)的交集++当我运行程序时,没有打印任何内容的问题你能帮忙吗

我需要知道问题出在哪里如果我想用cout而不是printf呢?

#include <iostream>
#include <stdio.h>
using namespace std;
// Intersecting two postings lists (a “merge” algorithm) 
// and I will assume that the two posting listst are sorted Ascendingly
// I will suppose that the first posting list is an array wich 
// have n elements I wil name it fistPost
// I will suppose that the second posting list is an array wich have
// m elements I will name it secondPost
int main()
{
    int firstPost[] ={3,5,7,8,13,15,30,34};
    int secondPost[]={1,5,7,9,11,15,20,34,35};
    int i,j=0;
    int n = sizeof(firstPost)/sizeof(firstPost[0]);
    int m = sizeof(secondPost)/sizeof(secondPost[0]);
    while(i<n && j<m)
    { 
        if (firstPost[i]<secondPost[j])
            i++;
        else if (firstPost[i]>secondPost[j])
            j++;
        else  if (firstPost[i]=secondPost[j])
        {
            printf ("%i", secondPost[j++]);
            i++;      
        }
    }        
    system("PAUSE");
    return 0;
}

您还没有将'i'初始化为0,因此它接受了一些垃圾值,并且没有执行while循环。

改变int i,j=0;到int i=0,j=0;

更改此printf ("%i", secondPost[j++]);

printf ("%d", secondPost[j++]);