Programming

Just another WordPress.com weblog

CT211-หาเซตผลลัพธ์ A Union B และ A Intersects B

แสดงความเห็นโดย จั่น บน กรกฎาคม 14, 2008

#include <stdio.h>
#include <iostream.h>
#include <conio.h>

void main(){

clrscr();

int i = 0, j = 0;
int a[5] = {1,2,3,5,7};
int b[7] = {2,4,5,6,7,8,9};
char ma[5] = {’0′,’0′,’0′,’0′,’0′};

//Find position of each element of Set A that match
//element of Set B
for(i=0; i<5; i++){

j = 0;

while(j < 6 && a[i] != b[j]){

j++;

}

if(a[i] == b[j]){

ma[i] = ‘1′;

}

}

//Output A Intersects B
cout << “A intersects B = “;

for(i=0; i<5; i++){

if(ma[i] == ‘1′){

cout << a[i] << ” “;

}

}
cout << “\n”;
//End Output A Intersects B

//Output A Union B
cout << “A Union B = “;
//Step 1) Output All elements of Set B
for(j=0; j<7; j++){

cout << b[j] << ” “;

}
//Step 2) Output All elements of Set A
//that is not duplicated with Set B
for(i=0; i<5; i++){

if(ma[i] == ‘0′){

cout << a[i] << ” “;

}

}
//End Output A Union B

getch();

}

ใส่ความเห็น

XHTML: คุณสามารถใช้แท็กเหล่านี้ได้: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>