/* A very simple binary search function on an ordered array. */ int belongs( int el, int *v, int low, int high ) { /* Variable containing the index of the middle element. */ int mid; if ( high < low || low > high ) { /* Element not found or invalid initial input of low and high * variables. */ mid = -1; return mid; } else { /* Check if array is ordered within every function call. */ assert( isArrayOrdered( v, low, high ) ); mid = ( low + high ) / 2; if ( v[mid] == el ) return mid; /* v[mid : 1 : high] > el && v[low : 1 : mid - 1] <= el */ else if ( v[mid] > el ) return ( belongs( el, v, low, mid - 1 ) ); /* v[low : 1 : mid ] < el && v[mid + 1 : 1 : high] >= el */ else if ( v[mid] < el ) return ( belongs( el, v, mid + 1, high ) ); } /* This part of the function is never reached. */ return -1; }
void fighting(const PUnit* a, const PUnit* b, PCommand &cmd) //单位a对单位b发起进攻 { Operation op; op.id = a->id; vector<const PSkill*> useSkill; for (int i = 0; i < a->skills.size(); ++i) { const PSkill* ptr = &a->skills[i]; if (ptr->isHeroSkill() && a->canUseSkill(ptr->typeId)) { PUnits ptr_foe; infectedBySkill(*INFO, a->id, ptr->typeId, ptr_foe); //寻找可用技能 if (belongs(b->id, ptr_foe) || !strcmp(ptr->name,"Hide") || !strcmp(ptr->name,"PowerUp")) useSkill.push_back(ptr); } } if (useSkill.size()) //策略:优先使用技能 { const PSkill* ptr = useSkill[rand()%useSkill.size()]; op.type = ptr->name; if (ptr->needTarget()) op.targets.push_back(b->pos); } else if (dis2(a->pos, b->pos) <= a->range) //判断单位b是否在攻击范围内 { op.type = "Attack"; op.targets.push_back(b->pos); } else { op.type = "Move"; findShortestPath(*MAP, a->pos, b->pos, blocks, op.targets); } cmd.cmds.push_back(op); }
void fightAlone(const PUnit* ptr, const PPlayerInfo &info, PCommand &cmd) { info.findUnitInArea(ptr->pos, ptr->view, nearBy); //寻找附近的人 fixPos[ptr->id] = EMPTYPOS; for (int i = 0; i < nearBy.size(); ++i) { if (unitEvaluation(nearBy[i]->name) > ptr->level || nearBy[i]->camp == ptr->camp || nearBy[i]->pos == Bow_pos[0] || nearBy[i]->pos == Bow_pos[3] || nearBy[i]->findBuff("Reviving")) //删除等级过高的人,忽略友军,剔除复活者,删除不可到达的野怪 { nearBy.erase(nearBy.begin()+i); --i; } } if (nearBy.size()) //策略:若周边有弱小对手则进行攻击 { const PUnit* ptr_foe = fixId[ptr->id]!=EMPTYID ? belongs(fixId[ptr->id], nearBy) : NULL; //判断之前是否锁定过目标 if (!ptr_foe) { ptr_foe = nearBy[rand()%nearBy.size()]; fixId[ptr->id] = ptr_foe->id; //若之前未锁定过目标,随机选定目标 } fighting(ptr, ptr_foe, cmd); //与目标打斗 } else { findFoes(ptr, cmd); //若附近没有敌军,在地图中寻找一个 } }
// UnionMember // // void UnionMember:: traverse (Type& m) { pre (m); belongs (m); name (m); post (m); }
// Attribute // // void Attribute:: traverse (Type& a) { pre (a); belongs (a); name (a); post (a); }
/**************************************************************** * Name : computeAIC_AC * * Function: compute absolute inter-connectivity and absolute * * closeness of two nodes. * * Input : struct node * node0 -- pointer to node0. * * struct node * node1 -- pointer to node1. * * float * aic -- pointer to the return value for * * absolute inter-connectivity. * * float * ac -- pointer to the return value for * * absolute closeness. * * Output : float * ****************************************************************/ static int computeAIC_AC(struct node * node0, struct node * node1, float * aic, float * ac){ int count, i, j; count = 0; (* aic) = 0.0; (* ac) = 0.0; /* count the edges from node0 */ for(i=0; i<node0->numberPoints; i++){ for(j=0; j<points[node0->points[i]].length; j++){ if(belongs(node1, points[node0->points[i]].edges[j].pointNO)>0){ (* aic) = (* aic) + points[node0->points[i]].edges[j].similarity; count++; }/* end if */ }/* end for j */ }/* end for i */ /* printf("!!! aic is %f,", *aic);*/ /* count the edges from node1 */ for(i=0; i<node1->numberPoints; i++){ for(j=0; j<points[node1->points[i]].length; j++){ if(belongs(node0, points[node1->points[i]].edges[j].pointNO)>0){ (* aic) = (* aic) + points[node1->points[i]].edges[j].similarity; count++; }/* end if */ }/* end for j */ }/* end for i */ /* printf(" aic is %f !!!", *aic);*/ /* printf("count is %d, ", count);*/ if(count>0) { (* ac) = (* aic)/((float)count); } else { (*ac) = 0; (* aic) = 0; } if(*ac > 10000 || *aic > 10000){ printf("ac is %e, aic is %e, count is %d \n", (*ac), (*aic), count); } return 1; }/* end computeAIC_AC */
// ReadAttribute // // void ReadAttribute:: traverse (Type& a) { pre (a); belongs (a); name (a); get_raises (a); post (a); }
//displayWord(); //leave(); // loadDictionnary(); // saveDictionnary(); int belongs(Dictionnary d, char* word){/*Marche partiellement.*/ printf("Lettre courante du dico %c, lettre du mot %c", d->car, word[0]); if(word[0] != '*'){ if(d==NULL){ return 0; } if(d->car == word[0]){ word++; return belongs(d->left, word); } if(word[0] < d->car){ return 0; } if(word[0] > d->car){ return belongs(d->right, word); } } return d->car == word[0]; }
//reassign mouths in m to m1 and m2 inline void splitSingleMouth (se_m* m1,se_m* m,const c_diagonal& dia,const Vector2d& n) { const Point2d& o=dia.getV1()->getPos();//.v[0]; //for each mouth in m typedef list<se_mouth*>::iterator MIT; for( MIT im=m->mouth.begin();im!=m->mouth.end();im++ ){ se_mouth * mt=*im; const Point2d& p1=mt->mouth.front(); const Point2d& p2=mt->mouth.back(); se_m* target=belongs(m1,m1,p1,p2); //mouth mt is not intersected by the cutting line if( target!=NULL ){ /////////////////////////////////////////////////////////////////// // Assign the mouth to m1 or m2 if( mt->connect[0]==m ) mt->connect[0]=target; else mt->connect[1]=target; target->mouth.push_back(mt); continue; } /////////////////////////////////////////////////////////////////// //mouth mt is intersected with the cutting line //split a mouth into two and assign the mouth to m1 or m2 double u=intersect(p1,p2,o,n); Point2d newp; for(int i=0;i<2;i++) newp[i]=(1-u)*p1[i]+u*p2[i]; double dot=(p1-o)*n; const Point2d& up=(dot>0)?p1:p2; const Point2d& down=(dot>0)?p2:p1; double u_d=(up-newp).normsqr(); double d_d=(down-newp).normsqr(); m1->mouth.push_back(mt); //if( u_d>d_d ) m1->mouth.push_back(mt); //else m2->mouth.push_back(mt); } }
int main(){ std::unordered_map<int, std::vector<int>> equiv_class; std::vector<unsigned long> primes = pe::PrimeSieve<30000>::make_vector(); for (auto i : primes) { equiv_class[i].push_back(i); for (auto j : primes) { if(belongs(j, equiv_class[i])){ equiv_class[i].push_back(j); if(equiv_class[i].size() == 5) { long ans = std::accumulate(equiv_class[i].begin(), equiv_class[i].end(), 0); std::cout << ans << std::endl; return 0; } } } } return 0; }
void UnionMember:: belongs (Type& m) { belongs (m, edge_traverser ()); }
void ReadAttribute:: belongs (Type& a) { belongs (a, edge_traverser ()); }
/** * This is the interactive menu. It is launched if the number of given args is == 1. * @param tab the array containing the parsed data. * @param tab_len the length of the array * @param alphab the pointer to the Liste which will contain the alphabetically-sorted data */ static void interactive_menu(Cellule* tab, int tab_len, Liste* alphab) { char choice = 0; char fname[256]; char mot[32]; printf("Index v%s - Menu interactif\n\n", version); printf("Entrez le nom du fichier à analyser: "); if(scanf("%s", fname) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ FILE* tmp; if((tmp = fopen(fname, "r")) == NULL) { fprintf(stderr, "Error: file %s cannot be found.", fname); exit(1); } fclose(tmp); parse_file(tab, tab_len, fname, alphab); while(choice != 'q' && choice != EOF) { printf("Choix\n" "\n" "a - teste l'appartenance d'un mot au fichier\n" "p - affiche les positions de mot dans le fichier\n" "P - affiche les phrases contenant mot dans le fichier\n" "l - affiche l'intégralité ddes mots (triés) présents dans le texte\n" "d - affiche l'ensemble des mots du texte ayant pour préfixe mot\n" "D - sauvegarde dans un fichier la liste des mots, avec leurs positions\n" "q ou ctrl+d - quitter\n" "\n" "Que voulez vous faire?\n" ); choice = getchar(); if(choice != EOF) getchar(); /* removes the \n */ #ifdef DEBUG printf("Choice: %c (%d)", choice, choice); #endif if(choice == 'a'){ printf("Entrez le mot à rechercher: "); if(scanf("%s", mot) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ if(belongs(mot, tab, tab_len)) { printf("%s appartient à %s\n", mot, fname); } else printf("%s n'appartient pas à %s\n", mot, fname); } else if(choice == 'p') { printf("Entrez le mot à rechercher: "); if(scanf("%s", mot) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ print_positions(mot, tab, tab_len); } else if(choice == 'P') { printf("Entrez le mot à rechercher: "); if(scanf("%s", mot) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ print_sentences_containing_word(mot, fname, tab, tab_len); } else if(choice == 'l') { print_alphabetical(*alphab); } else if(choice =='d') { printf("Entrez le mot à rechercher: "); if(scanf("%s", mot) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ print_all_from_prefix(mot, *alphab); } else if(choice == 'D') { char fsave[256]; printf("Entrez le nom de fichier dans lequel sauvegarder: "); if(scanf("%s", fname) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } getchar(); /* removes the \n */ save_positions_to_file(fsave, *alphab); } else if(choice == 'q' || choice == EOF) { printf("Quit.\n"); } else printf("Not a valid choice: %c\n", choice); } }
int main(int argc, char *argv[]) { const char* optstring = ":ha:p:P:ld:D:"; const struct option lopts[] = { {"help", no_argument, NULL, 'h'}, {"appartient", required_argument, NULL, 'a'}, {"positions", required_argument, NULL, 'p'}, {"phrases", required_argument, NULL, 'P'}, {"liste", no_argument, NULL, 'l'}, {"prefixe", required_argument, NULL, 'd'}, {"tofile", required_argument, NULL, 'D'}, {NULL, no_argument, NULL, 0} }; int val,index=-1; int last_val = val; char* last_optarg; while (EOF!=(val=getopt_long(argc,argv,optstring,lopts,&index))) { char msg[32]; if (index==-1) sprintf(msg,"short option -%c",val); else sprintf(msg,"long option --%s",lopts[index].name); if(val == 'h') { print_help(); exit(0); } last_val = val; last_optarg = optarg; #ifdef DEBUG printf("Option %c with opt %s recieved\n", val, optarg); #endif index=-1; } Cellule tab[N]; int i = 0; for(; i<N; ++i) { tab[i].valeur = NULL; tab[i].suivant = NULL; } Liste alphab = NULL; if(argc == 1) { interactive_menu(tab, N, &alphab); exit(0); } if(optind < argc) { parse_file(tab, N, argv[optind], &alphab); if(last_val == 'a') { int res = belongs(last_optarg, tab, N); if(res) printf("Word %s is in file %s",last_optarg, argv[optind]); else printf("Word %s is not in file %s", last_optarg, argv[optind]); } else if(last_val == 'p') { print_positions(last_optarg, tab, N); } else if(last_val == 'P') { print_sentences_containing_word(last_optarg, argv[optind], tab, N); } else if(last_val == 'l') { print_alphabetical(alphab); } else if(last_val =='d') { print_all_from_prefix(last_optarg, alphab); } else if(last_val == 'D') { save_positions_to_file(last_optarg, alphab); } } else { fprintf(stderr, "Error: not enough arguments\n"); print_help(); } #ifdef DEBUG print_tab(tab, N); #endif for(i = 0; i<N; ++i) liste_free(tab[i].suivant, 0x3); liste_free(alphab, 0x0); return 0; }
Returns& returns () const { return dynamic_cast<Returns&> (belongs ()); }
/* May 30th, Weinan: one problem I have just found is that I need to * establish a kind of checking table to connect my global vertex * number with local vertex number to be used in the HMETIS_PartRecursive * function calling. */ static int cutNode(struct node * source, struct node * left, struct node * right, int ub){ int nvtxs, nhedges; int * vwgts = NULL; int * eptr = NULL; int * eind = NULL; int * hewgts = NULL; int nparts; int ubfactor; int * options = NULL; int * part = NULL; int * edgecut = NULL; int tmp, i, j, k, l, tmpNO, flag, found, group0, group1, index0, index1; /* this is the table used to correspond global vertice number with * local vertice number. */ int * checkTable; /* number of vertices */ nvtxs = source->numberPoints; checkTable = (int *)calloc(nvtxs, sizeof(int)); if(checkTable == NULL) { printf("cannot allocate memory for checkTable! \n"); return -1; } /* load the vertice's number into the checkTable */ for(i=0; i<nvtxs; i++){ checkTable[i] = source->points[i]; }/* end for */ /* number of edges */ tmp=0; for(i=0; i<nvtxs; i++){ for(j=0; j<points[source->points[i]].length; j++){ /* decide a point whether belongs to a node */ if(belongs(source, points[source->points[i]].edges[j].pointNO)==1) /* !!!!!! */ tmp++; }/* end for j*/ }/* end for i*/ nhedges = tmp; /* weight of the vertices, because * the vertices are not weighted, so * according to p13 of the hMETIS manual, * vwgts can be NULL. */ vwgts = NULL; /* eptr and eind */ /* because all my edges are of * size 2, so it makes things * easier. */ tmp = nhedges+1; eptr = (int *)calloc(tmp, sizeof(int)); if(eptr == NULL) { printf("cannot allocate memory for eptr! \n"); return -1; } for(i=0; i<tmp; i++) eptr[i]=i*2; /* when loading eind, need to check the checkTable */ eind = (int *)calloc(2*tmp, sizeof(int)); if(eind == 0) { printf("cannot allocate memory for eind! \n"); return -1; } k = 0; for(i=0; i<nvtxs; i++){ for(j=0; j<points[source->points[i]].length; j++){ /* decide a point whether belongs to a node */ if(belongs(source, points[source->points[i]].edges[j].pointNO)==1){ /* eind[k] = source->points[i];*/ eind[k] = i; k++; /* eind[k] = points[source->points[i]].edges[j].pointNO; */ tmpNO = points[source->points[i]].edges[j].pointNO; flag = 0; for(l=0; l<nvtxs; l++){ if (tmpNO == checkTable[l]){ flag = 1; found = l; break; } } if(flag == 1) eind[k] = found; else { printf("some thing wrong with checkTable! \n"); return -1; }/* end if...else... */ k++; }/* end if */ }/* end for j*/ }/* end for i*/ if(k != 2*nhedges){ printf("some thing wrong with eind! \n"); return -1; }/* end if */ /* hewgts: an array of size nhedges that stores the weight * of the hyperedges. */ hewgts = (int *)calloc(nhedges, sizeof(int)); if(hewgts == 0) { printf("cannot allocate memory for hewgts! \n"); return -1; } k = 0; for(i=0; i<nvtxs; i++){ for(j=0; j<points[source->points[i]].length; j++){ /* decide a point whether belongs to a node */ if(belongs(source, points[source->points[i]].edges[j].pointNO)==1){ /*!!!!!! here I have to do a cast becasue now similarity is a double */ hewgts[k] = (int)(points[source->points[i]].edges[j].similarity*DIAG); k++; }/* end if */ }/* end for j*/ }/* end for i*/ if(k != nhedges){ printf("some thing wrong with hewgts! \n"); return -1; }/* end if */ /* nparts: number of desired partitions */ nparts = 2; /* ubfactor: relative imbalance factor */ ubfactor = ub; /* options */ /* note that is options[0]=0, * then default values are used. */ options = (int *)calloc(9, sizeof(int)); if(options == 0) { printf("cannot allocate memory for options! \n"); return -1; } options[0] = 0; /* part */ part = (int *)calloc(nvtxs, sizeof(int)); if(part == 0) { printf("cannot allocate memory for part! \n"); return -1; } /* edgecut */ edgecut = (int *)calloc(1, sizeof(int)); if(edgecut == 0) { printf("cannot allocate memory for edgecut! \n"); return -1; } HMETIS_PartRecursive(nvtxs, nhedges, vwgts, eptr, eind, hewgts, nparts, ubfactor, options, part, edgecut); group0 = 0; group1 = 0; for(i=0; i<nvtxs; i++){ if(part[i] == 0) group0++; else if(part[i] == 1) group1++; else { printf("something wrong with part. \n"); return -1; }/* end if..else...*/ }/* end for */ left->numberPoints = group0; right->numberPoints = group1; left->points = (int *)calloc(group0, sizeof(int)); if(left->points == NULL) { printf("cannot allocate memory for left->points \n"); return -1; } right->points = (int *)calloc(group1, sizeof(int)); if(right->points == NULL) { printf("cannot allocate memory for right->points \n"); return -1; } left->left = NULL; left->right = NULL; right->left = NULL; right->right = NULL; index0 = 0; index1 = 0; for(i=0; i<nvtxs; i++){ if(part[i] == 0) { left->points[index0] = checkTable[i]; index0++; } else if(part[i] == 1) { right->points[index1] = checkTable[i]; index1++; } else { printf("something wrong with part. \n"); return -1; }/* end if..else...*/ }/* end for */ if(index0!=group0 || index1!=group1) { printf("some thing wrong with index0-1. \n"); return -1; } free(vwgts); free(eptr); free(eind); free(hewgts); free(options); free(part); free(edgecut); free(checkTable); return 1; }/* end cutNode */