user-0 提问: 这个代码怎么优化啊（集合）

回复:
user: user-0
content: 我初学c语言,不知道STL是什么。题目似乎还要加查询这一项，还有集合中的数的个数无法判断 我用我仅有的知识又改了点 #include <stdio.h> int main() { 	int s1[100],s2[100]; 	int i,j,k,l,m,n,h; 	printf("请输入两个集合中各有多少个数："); 	scanf("%d%d",&n,&m); 	printf("请输入第一个集合："); 	for(i=0;i<n;i++) 	{ 		scanf("%d",&s1[i]); 	} 	printf("第一个集合为："); 	for(i=0;i<n;i++) 	{ 		printf("%d ",s1[i]); 	} 	printf("\b\n"); 	printf("请输入第二个集合："); 	for(i=0;i<m;i++) 	{ 		scanf("%d",&s2[i]); 	} 	printf("第二个集合为："); 	for(i=0;i<m;i++) 	{ 		printf("%d ",s2[i]); 	} 	printf("\b\n请输入要查询的数："); 	scanf("%d",&l); 	for(i=0;i<n;i++) 	for(j=0;j<m;j++) 		{ 		if(l==s1[i]) 		{ 			h=1; 			break; 		} 		if(l==s2[j]) 		{ 			h=0; 			break; 		} 		} 	if(h==1) 		printf("%d为第一个集合中第%d个数",l,i+1); 	if(h==0) 		printf("%d为第二个集合中第%d个数",l,j+1); 	else 		printf("查无此数！"); 	 	printf("\b\n交集为："); 	for(i=0;i<n;i++) 	{ 		for(j=0;j<m;j++) 		{ 			if(s1[i]==s2[j]) 			printf("%d ",s1[i]); 		} 	} 	printf("\b\n"); 	printf("并集为："); 	for(i=0;i<n;i++) 	{ 		printf("%d ",s1[i]); 	} 		for(j=0;j<m;j++) 		{ 			for(k=0;k<n;k++) 			{ 				if(s1[k]==s2[j]) 				{ 					h = 1; 					break; 				} 				else	 					h = 0;	 			}  			if(h==0) 			{ 				printf("%d ",s2[j]); 				h = 1; 			} 		} 	printf("\b\n"); 	printf("差集为："); 	for(i=0;i<n;i++) 		{ 			for(k=0;k<m;k++) 			{ 				if(s2[k]==s1[i]) 				{ 					h = 1; 					break; 				} 				else 				{ 					h = 0; 				}	 			} 			if(h==0) 			{ 				printf("%d ",s1[i]); 			} 		} 		printf("\t"); 		printf("或者为："); 		for(j=0;j<m;j++) 		{ 			for(k=0;k<n;k++) 			{ 				if(s1[k]==s2[j]) 				{ 					h = 1; 					break; 				} 				else 					h = 0;	 			} 			if(h==0) 				printf("%d ",s2[j]); 		} 		printf("\b\n"); 	return 0; }

user: user-1
content: 呵呵，直接调用STL算法，最优化了。要求的STL中都有

user: user-0
content: 题目中的查询是什么意思

user: user-2
content: #include <stdio.h> //#include <stdlib.h> //#include <time.h>  #define MAX_SETS_LEN     10   static void init_sets(int *arr, int len) {     int i;     //srand(time(NULL));     printf("Please input %d integer number, then enter: ", len);     for (i = 0; i < len; i++)         //arr[i] = rand() % 100;         scanf("%d", arr+i); } static void init_zero_sets(int *arr, int len) {     int i;      for (i = 0; i < len; i++)         arr[i] = 0; }  static void print_sets(int *arr, int len) {     int i;      for (i = 0; i < len; i++) {         if (!arr[i])             break;         printf("%d\t", arr[i]);     }     putchar(10); }  void get_intersection_sets(int *set1, int *set2, int *inters_sets) {     int i, j, k = 0;      for (i = 0; i < MAX_SETS_LEN; i++) {         for (j = 0; j < MAX_SETS_LEN; j++) {             if (set1[i] == set2[j])                 inters_sets[k++] = set1[i];         }     } }   void get_union_sets(int *set1, int *set2, int *union_sets) {     int i, j, k = 0;      for (i = 0; i < MAX_SETS_LEN; i++) {         union_sets[k++] = set1[i];     }     int cnt = k;     for (i = 0; i < MAX_SETS_LEN; i++) {         for (j = 0; j < cnt; j++)             if (union_sets[j] == set2[i])                 break;         if (j == cnt)             union_sets[k++] = set2[i];     } }  void get_diff_sets(int *set1, int *set2, int *diff_sets) {     int i, j, k = 0;      for (i = 0; i < MAX_SETS_LEN; i++) {         for (j = 0; j < MAX_SETS_LEN; j++)             if (set1[i] == set2[j])                 break;         if (j == MAX_SETS_LEN)             diff_sets[k++] = set1[i];     } }   int main(int argc, const char *argv[]) {     int arr_set1[MAX_SETS_LEN], arr_set2[MAX_SETS_LEN];      int inters_sets[MAX_SETS_LEN], union_sets[2*MAX_SETS_LEN], diff_sets[MAX_SETS_LEN];      init_sets(arr_set1, MAX_SETS_LEN);     init_sets(arr_set2, MAX_SETS_LEN);     init_zero_sets(inters_sets, MAX_SETS_LEN);     init_zero_sets(union_sets, 2 *MAX_SETS_LEN);     init_zero_sets(diff_sets, MAX_SETS_LEN);     printf("Print first sets: \n");     print_sets(arr_set1, MAX_SETS_LEN);     printf("Print Second sets: \n");     print_sets(arr_set2, MAX_SETS_LEN);      get_intersection_sets(arr_set1, arr_set2, inters_sets);     get_union_sets(arr_set1, arr_set2, union_sets);     get_diff_sets(arr_set1, arr_set2, diff_sets);      printf("Intersection sets list: \n");     print_sets(inters_sets, MAX_SETS_LEN);     printf("Union sets list: \n");     print_sets(union_sets, 2 * MAX_SETS_LEN);     printf("different sets list: \n");     print_sets(diff_sets, MAX_SETS_LEN);     return 0; }  我写的demo,参考一下吧.如有问题,及时指出来吧； 建议不要把所有功能放到主函数里,可以尝试写成小函数然后主函数调用.

user: user-3
content: 仅供参考：#include <algorithm> #include <iostream> #include <functional> #include <cstring> using namespace std; int main() {     char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;     char *Vowels   = "aeiou" ;     char *AlphaNum = "0123456789abcdef" ;     char result[45] ;     char *last ;      int lenA  = strlen(Alphabet) ;     int lenV  = strlen(Vowels  ) ;     int lenAN = strlen(AlphaNum) ;      cout << "Alphabet = " << Alphabet << endl ;     cout << "Vowels   = " << Vowels   << endl ;     cout << "AlphaNum = " << AlphaNum << endl ;      cout << "\nusing non-predicate versions" << endl ;      //non-predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //non-predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      cout << "\nusing predicate versions" << endl ;      //predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result  , less<char>()) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result  , less<char>()) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result  , less<char>()) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result  , less<char>()) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      return 0 ; }