如何用c语言求两数组之差
就是用c语言求两数组差集,比如,a={1,2,3,4},b={1,3,5,6},则a与b的差集为c=a-b={2,4}.我迫切希望哪位仁兄能帮我一把,本人感激不尽!...
就是用c语言求两数组差集,比如,a={1,2,3,4},b={1,3,5,6},则a与b的差集为c=a-b={2,4}.我迫切希望哪位仁兄能帮我一把,本人感激不尽!
展开
2个回答
展开全部
算法很简单,一次循环,分别相减。
代码如下:
#include <stdio.h>
#define SIZE 4
int a[SIZE] = {1, 2, 3, 4};
int b[SIZE] = {1, 3, 5, 6};
int i;
int main(){
for(i=0;i<SIZE;i++){
printf("a(%d) - b(%d) = %d \n",i,i,a[i]-b[i]);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#define SIZE 4
int main()
{
int a[SIZE] = {1, 2, 3, 4};
int b[SIZE] = {1, 3, 5, 6};
int c[SIZE];
int i, j, *p;
for(i = 0, j = 0; i < SIZE; ++i)
{
for(p = b; p != b + SIZE; ++p)
{
if(a[i] == *p)break;
}
if(p == b + SIZE)
c[j++] = a[i];
}
printf("a - b = { ");
for(i = 0; i < j; ++i)
{
printf("%d ", c[i]);
}
printf("}");
}
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int size = 4;
int a[size] = {1, 2, 3, 4};
int b[size] = {1, 3, 5, 6};
int c[size];
int* p1 = set_difference(a, a + size, b, b + size, c);
for(int* p2 = c; p2 != p1; ++p2)
{
cout << *p2 << ' ';
}
}
#define SIZE 4
int main()
{
int a[SIZE] = {1, 2, 3, 4};
int b[SIZE] = {1, 3, 5, 6};
int c[SIZE];
int i, j, *p;
for(i = 0, j = 0; i < SIZE; ++i)
{
for(p = b; p != b + SIZE; ++p)
{
if(a[i] == *p)break;
}
if(p == b + SIZE)
c[j++] = a[i];
}
printf("a - b = { ");
for(i = 0; i < j; ++i)
{
printf("%d ", c[i]);
}
printf("}");
}
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int size = 4;
int a[size] = {1, 2, 3, 4};
int b[size] = {1, 3, 5, 6};
int c[size];
int* p1 = set_difference(a, a + size, b, b + size, c);
for(int* p2 = c; p2 != p1; ++p2)
{
cout << *p2 << ' ';
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询