コード例 #1
0
int main(int argc, const char * argv[])
{
	int T = 0;
	scanf("%d", &T);
	
	while (T >= 1) {
		int N = 0;
		scanf("%d", &N);
		
		Ship ships[N];
		
		char shipDescription[MAXLENGTH];
		makeEmptyArray(shipDescription, MAXLENGTH);
		
		int shipIndex = 0;
		int fix = 0;
		int Case = 1;
		while (fgets(shipDescription, MAXLENGTH, stdin)) {
			
			if (!fix) fgets(shipDescription, MAXLENGTH, stdin);
			
			fix++;
			
			char *desc = shipDescription;
			int space = 0;
			
			while ('\0' != *desc) {
				char val[MAXLENGTH];
				makeEmptyArray(val, MAXLENGTH);
				int idx = 0;
				
				while (!isspace(*desc)) {
					val[idx] = *desc;
					
					desc++;
					idx++;
				}
				
				if (isspace(*desc)) {
					desc++;
				}
				
				char *value = val;
				int v = atoi(value);
				
				switch (space) {
					case 0:
						ships[shipIndex].x = v;
						break;
					case 1:
						ships[shipIndex].y = v;
						break;
					case 2:
						ships[shipIndex].z = v;
						break;
					case 3:
						ships[shipIndex].p = v;
						break;
						
					default:
						break;
				}
				
				space++;
			}
			
			if (1 == N) break;
			
			N--;

			shipIndex++;
		}
		
		printf("Case #%d: ", Case);
		int powers[N];
		int idx = 0;
		for (int k = 0; k < N; k++) {
			Ship cruiser = ships[k];
			
			int exception = k;
			
			for (int h = 0; h < N; h++) {
				if (exception != h) {
					int power = calculatePowerOfCruiser(cruiser.x, cruiser.y, cruiser.z, ships[h].x, ships[h].y, ships[h].z, ships[h].p);
					
					powers[idx] = power;
					
					idx++;
				}
			}
		}
		
		int min = powers[0];
		for (int k = 0 ; k <= idx; k++) {
			if (min < powers[k]) {
				min = powers[k];
			}
		}
		printf("%d\n", min);
		
		T--;
		Case++;
	}
	
    return 0;
}
コード例 #2
0
int main()
{
	int data[] = {85, 80, 40, 30, 10, 70, 110}; // P141	
	int buildHeapData[] = {150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130};
	BinaryHeap bh;	
	int size;
	int i;	
	int capacity;
	Distance tempDisStruct;
	int *indexOfVertexInHeap;
	
	printf("\n\t=== test for inserting the binary heap with {85, 80, 40, 30, 10, 70, 110} in turn ===\n");
	capacity = 14;
	bh = initBinaryHeap(capacity);
	size = 7;	
	
	tempDisStruct = makeEmptyDistance(); 
	indexOfVertexInHeap = makeEmptyArray(size);

	for(i = 0; i < size; i++) 
	{
		tempDisStruct->distance = data[i];
		tempDisStruct->vertexIndex = i;
		insert(tempDisStruct, bh, indexOfVertexInHeap);
	}	
	printBinaryHeap(bh);
	printIndexOfVertexInHeap(bh->size, indexOfVertexInHeap);

	printf("\n\t=== test for inserting the binary heap with element {100, 20, 90} in turn ===\n");
	
	tempDisStruct->distance = 100;
	tempDisStruct->vertexIndex = size;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	tempDisStruct->distance = 20;
	tempDisStruct->vertexIndex = size+1;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	tempDisStruct->distance = 90;
	tempDisStruct->vertexIndex = size+2;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	printIndexOfVertexInHeap(bh->size, indexOfVertexInHeap);

	printf("\n\t=== test for inserting the binary heap with 5 ===\n");	
	tempDisStruct->distance = 5;
	tempDisStruct->vertexIndex = size+3;
	insert(tempDisStruct, bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);

	printf("\n\t=== test for 3 deletings towards the minimum in binary heap ===\n");
	deleteMin(bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);
	deleteMin(bh, indexOfVertexInHeap);		
	printBinaryHeap(bh);
	deleteMin(bh, indexOfVertexInHeap);	
	printBinaryHeap(bh);
}