【发布时间】:2011-09-17 17:52:02
【问题描述】:
我有三个一维数组。任务是将存在于三个数组中的每个数组中的数字存储在第四个数组中。这是我的解决方案,如您所见,这是不正确的。如果可能的话,我也对更快的算法感兴趣,因为它是 O(N3) 难度。
#include <stdio.h>
main(){
int a[5]={1,3,6,7,8};
int b[5]={2,5,8,7,3};
int c[5]={4,7,1,3,6};
int i,j,k;
int n=0;
int d[5];
for(k=0; k<5; k++){
for(j=0; j<5; j++){
for(i=0; i<5; i++){
if(a[i]==b[j] && b[j]==c[k])
{d[n]=a[i];
n++;}
else
d[n]=0;
}}}
//Iterate over the new array
for(n=0;n<5;n++)
printf("%d\n",d[n]);
return 0;
}
【问题讨论】: