Beispiel #1
0
/* Relink all lights that are so marked. */
void relink_light_sources(boolean ghostly, struct level *lev)
{
    char which;
    unsigned nid;
    light_source *ls;

    for (ls = lev->lev_lights; ls; ls = ls->next) {
	if (ls->flags & LSF_NEEDS_FIXUP) {
	    if (ls->type == LS_OBJECT || ls->type == LS_MONSTER) {
		if (ghostly) {
		    if (!lookup_id_mapping((long)ls->id, &nid))
			impossible("relink_light_sources: no id mapping");
		} else
		    nid = (long) ls->id;
		if (ls->type == LS_OBJECT) {
		    which = 'o';
		    ls->id = find_oid(nid);
		} else {
		    which = 'm';
		    ls->id = find_mid(lev, nid, FM_EVERYWHERE);
		}
		if (!ls->id)
		    impossible("relink_light_sources: cant find %c_id %d",
			       which, nid);
	    } else
		impossible("relink_light_sources: bad type (%d)", ls->type);

	    ls->flags &= ~LSF_NEEDS_FIXUP;
	}
    }
}
int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0;i < n;i ++){
            scanf("%d", &arr[i]);
        }
        printf("%d\n", find_mid(arr, 0, n-1, n / 2));
    }
    return 0;
}
int find_mid(int arr[], int left, int right, int x)
//下标从零开始
{
    if(left >= right){
        return arr[left + x];
    }
    int mid = arr[left];
    int i = left;
    int j = right;
    while(i < j){
        while(i < j && arr[j] >= mid) j--;
        arr[i] = arr[j];
        while(i < j && arr[i] <= mid) i++;
        arr[j] = arr[i];
    }
    arr[j] = mid;
    if(i - left == x)
        return arr[i];
    if(i - left < x)
        return find_mid(arr, i + 1, right, x - (i - left + 1));
    else
        return find_mid(arr, left, i - 1, x);
}
Beispiel #4
0
int splithalf(SLink **head, SLink **ahead, SLink **bhead)
{
	//take local reference to traverse through the link
	SLink *temp, *temp2, *temp3;
	temp = *head;
	
	//get the mid_value of the given list that needs to be split
	int mid_value, i;
	mid_value = find_mid(*head);
	
	//check if list is empty
	if (mid_value == EMPTYLIST)
	{
		return EMPTYLIST;
	}
	
	//if head has only one element,
	if((*head) -> link == NULL)
	{
		*ahead = *head;
		*bhead = NULL;
		return SUCCESS;
	}		
	//if mid_value is > 0
	else
	{
		//iterate to the mid of a list and copy this into alist
		while(temp)
		{
			if(temp -> data == mid_value)
			{
				break;
			}
			insert_at_last(ahead, temp -> data); 
			temp = temp -> link;
		}
		temp3 = temp;
		
		// blist will be updated with temp2 which has second half
		*bhead = temp3; 	
		return SUCCESS;
	}
	
	return FAILURE;
}
/*
 * This function is called every turn.
 * It makes the regions age, if necessary and calls the appropriate
 * callbacks when needed.
 */
void
run_regions(struct level *lev)
{
    int i, j, k;
    int f_indx;

    /* End of life ? */
    /* Do it backward because the array will be modified */
    for (i = lev->n_regions - 1; i >= 0; i--) {
        if (lev->regions[i]->ttl == 0) {
            if ((f_indx = lev->regions[i]->expire_f) == NO_CALLBACK ||
                (*callbacks[f_indx]) (lev->regions[i], 0))
                remove_region(lev->regions[i]);
        }
    }

    /* Process remaining lev->regions */
    for (i = 0; i < lev->n_regions; i++) {
        /* Make the region age */
        if (lev->regions[i]->ttl > 0)
            lev->regions[i]->ttl--;
        /* Check if player is inside region */
        f_indx = lev->regions[i]->inside_f;
        if (f_indx != NO_CALLBACK && hero_inside(lev->regions[i]))
            (void)(*callbacks[f_indx]) (lev->regions[i], 0);
        /* Check if any monster is inside region */
        if (f_indx != NO_CALLBACK) {
            for (j = 0; j < lev->regions[i]->n_monst; j++) {
                struct monst *mtmp =
                    find_mid(lev, lev->regions[i]->monsters[j], FM_FMON);

                if (!mtmp || DEADMONSTER(mtmp) ||
                    (*callbacks[f_indx]) (lev->regions[i], mtmp)) {
                    /* The monster died, remove it from list */
                    k = (lev->regions[i]->n_monst -= 1);
                    lev->regions[i]->monsters[j] = lev->regions[i]->monsters[k];
                    lev->regions[i]->monsters[k] = 0;
                    --j;        /* current slot has been reused; recheck it
                                   next */
                }
            }
        }
    }
}