コード例 #1
0
ファイル: LAB5.c プロジェクト: evanyc15/School_Programming
/*3*/void sortQuick(int randomArray[],int left,int right,int*compares,int*moves)
{
	int pivot,sortLeft,sortRight;

	if((right - left) >= 0){
		medianLeft(randomArray,left,right,compares,moves);
		pivot = randomArray[left];
		sortLeft = left+1;
		sortRight = right;
		while(sortLeft <= sortRight){
			while(*compares+=1,randomArray[sortLeft] >= pivot){ //Determines ascending or descending
				sortLeft = sortLeft+1;;
			}
			while(*compares+=1,randomArray[sortRight] < pivot){ //Determines ascending or descending
				sortRight = sortRight-1;;
			}
			if(sortLeft <= sortRight){
				exchange(randomArray,sortLeft,sortRight);
                *moves+=1;
				sortLeft = sortLeft+1;
				sortRight = sortRight-1;;
			}
		}
		randomArray[left] = randomArray[sortLeft-1];
        *moves+=1;
		randomArray[sortLeft-1] = pivot;
        *moves+=1;
		if(left < sortRight)
			sortQuick(randomArray,left,sortRight-1,compares,moves);
		if(sortLeft < right)
			sortQuick(randomArray,sortLeft,right,compares,moves);
	}
	else
		sortInsertion(randomArray,left,right); //not done with insertion yet, not descending
}
コード例 #2
0
ファイル: contact.c プロジェクト: nanAdair/USACO
int main()
{
    FILE *fin = fopen("contact.in", "r");
    FILE *fout = fopen("contact.out", "w");
    int i, j, begin, end, top, record[200001], count, res[13][4096];
    fscanf(fin, "%d %d %d", &begin, &end, &top);
    
    char buffer[1024];
    count = 0;
    while (fscanf(fin, "%s", buffer) > 0) {
        for (i = 0; i < strlen(buffer); i++)
            record[count++] = buffer[i] - '0';
    }
    
    for (i = 0; i < count; i++) {
        for (j = begin; j <= end; j++) {
            if (i + j > count)
                break;
            int temp = formNumber(record+i, j);
            res[j][temp]++;
        }
    }
    
    struct mark contact[5000];
    count = 0;
    for (i = begin; i <= end; i++) {
        for (j = 0; j < 4096; j++) {
            if (res[i][j] != 0) {
                contact[count].length = i;
                contact[count].value = j;
                contact[count++].count = res[i][j];
            }
        }
    }
    
    sortInsertion(contact, count);
    
    int next, num;
    i = j = 0;
    next = 1;
    while (i < top && j < count) {
        if (next) {
            fprintf(fout, "%d\n", contact[j].count);
            next = 0;
            num = 0;
        }
        char output[13];
        formString(contact[j].length, contact[j].value, output);
        fprintf(fout, "%s", output);
        num++;
        if (contact[j+1].count != contact[j].count) {
            i++;
            next = 1;
            fprintf(fout, "\n");
        }else {
            if (num == 6) {
                fprintf(fout, "\n");
                num = 0;
            }else 
                fprintf(fout, " ");
        }
        j++;
    }
    
    fclose(fin);
    fclose(fout);
}