我编写了这段代码,用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;
}
答案 0 :(得分:3)
你没有将'i'初始化为0,因此它占用了一些垃圾值而没有执行while循环。
改变int i,j = 0; to int i = 0,j = 0;
答案 1 :(得分:1)
更改此printf ("%i", secondPost[j++]);
到
printf ("%d", secondPost[j++]);