sbshso 于 2016.02.15 21:09 提问

/*输入两个整型数组，假设数组大小为7，输出不是两个数组共有的元素。

5个回答

caozhy      2016.02.15 21:21

5 6 7 8 9 0 Press any key to continue

caozhy      2016.02.15 21:20
`````` #include <stdio.h>

int main(int argc, char* argv[])
{
char a[7]={1,2,3,4,5,6,7};
char b[7]={1,2,3,4,8,9,0};
char c[20];
int x = -1;
int i, j;
for (i = 0; i < 7; i++)
{
int f = 0;
for (j = 0; j < 7; j++)
{
if (a[i] == b[j]) { f = 1; break; }
}
if (!f)
{
c[++x] = a[i];
}
}
for (j = 0; j < 7; j++)
{
int f = 0;
for (i = 0; i < 7; i++)
{
if (a[i] == b[j]) { f = 1; break; }
}
if (!f)
{
c[++x] = b[j];
}
}
for (i = 0; i <= x; i++) printf("%d ", c[i]);
return 0;
}
``````
github_27756899   2016.02.16 23:08

char *p = a[i]; //改为: char * p = a+i;
p = a[j]; //改为:p = a+j;

github_27756899   2016.02.16 23:12

char *p = c[i]; //改为: char * p = c+i;
p = c[j]; //改为:p = c+j;
printf("%3d",a[i]); //改为：printf("%3c",c[i]);

lm_whales   2016.02.16 00:43

int diffab(char *a,ichar*b,char *c,int m.int n){
int i,j;
char *p=c;
for(i=0;i=m;i++){
for(j=0;j<n;j++){
if(a[i]==b[j]){
break;
}
if(j==n) *p++=a[i];
}
for(i=0;i=n;i++){
for(j=0;j<m;j++){
if(a[j]==b[i]){
break;
}
if(j==m) *p++=b[i];
}
return p-c;
}

int main()
{
char a[]={1 ,2 ,3, 4 ,5, 6 ,7};
char b[]={1,2,3,4,6, 8, 9, 0},
char c[14]={};
int n =diffab(a,b,c,7,7);
for(int i=0;i<n;i++)
cout <<c[i]<<" ";
}
cout <<endl;
return 0;
}