int main() { int m,k; int i; long long last = 0; scanf("%d%d", &m, &k); for(i=1; i<=m; ++i) { heap[i-1].x = m-(i-1); heap[i-1].y = m; } while(k>0) { #ifdef DEBUG printheap(m); #endif if(last == (long long)heap[0].x*heap[0].y) { addnext(0, &m); relax(0, &m); continue; } printf("%lld\n", (long long)heap[0].x*heap[0].y); last = (long long)heap[0].x*heap[0].y; addnext(0, &m); relax(0, &m); k--; } return 0; }
void HEAP_test() { HEAP heap; HEntry entry,*pentry; int *tmp,i; VISITCTX sorted_ctx; VASSERT( !HEAP_init( &heap, 10, sizeof(HEntry), HEntry_compare) ); tmp = shuffle(TEST_SIZE); #ifdef SHOW_RESULTS printheap(&heap); #endif for(i=0;i<TEST_SIZE;i++) { entry.Key = tmp[i]; sprintf(entry.Name,"N:%03d",tmp[i]); HEAP_push( &heap, (unsigned char *) &entry, sizeof(entry) ); #ifdef SHOW_RESULTS printheap(&heap); #endif VASSERT( HEAP_check(&heap) ); } free(tmp); #ifdef SHOW_RESULTS printf("pop!\n"); #endif VASSERT( HEAP_check(&heap) ); VASSERT( HEAP_size(&heap) == TEST_SIZE ); i = -1; while( (pentry = (HEntry *) HEAP_top( &heap )) != 0 ) { #ifdef SHOW_RESULTS printheap(&heap); printf("->%d\n",pentry->Key); #endif VASSERT(pentry->Key > i); sprintf(entry.Name,"N:%03d",pentry->Key); VASSERT( pentry->Key >= i); VASSERT( strcmp(pentry->Name,entry.Name) == 0); i = pentry->Key; HEAP_pop( &heap); } VASSERT(HEAP_check(&heap)); sorted_ctx.entry = 0; HEAP_foreach_sorted( &heap, heap_visitor_check_sorted, &sorted_ctx); HEAP_free(&heap); }
// shows a prompt to the user, gets an input line, // calls parseCommand, then determines and calls // the appropriate function for the command void promptUser() { for (;;) { char *line = NULL; char **argv = NULL; size_t line_size = 0; size_t n = 0; printf("prompt>"); getline(&line, &line_size, stdin); int argc = parseCommand(line, &n, &argv); free(line); // printf("%d tokens parsed\n", argc); if (argc > 0) { // int i; // for (i = 0 ; i < argc ; i++) { // printf("argv %d: %s\n", i, argv[i]); // } if (!strcmp(argv[0], "allocate")) { // first check that there's an appropriate number of arguments if (argc == 2) { // int allocate(int number_of_bytes) int number_of_bytes = atoi(argv[1]); if (number_of_bytes > 0) allocate(number_of_bytes); else printf("Cannot allocate < 1 byte.\n"); } else { printf("Usage: allocate [number_of_bytes]\n"); } } else if (!strcmp(argv[0], "free")) { if (argc == 2) { // void deallocate(int block_number) int block_number = atoi(argv[1]); if (block_number > 0) deallocate(block_number); else printf("Block number can not be less than 1.\n"); } else { printf("Usage: free [block_number]\n"); } } else if (!strcmp(argv[0], "blocklist")) { // void blocklist() blocklist(); } else if (!strcmp(argv[0], "writeheap")) { if (argc == 4) { // void writeheap(int the_block_number, char CTW, int copies) int the_block_number = atoi(argv[1]); char CTW = argv[2][0]; int copies = atoi(argv[3]); if ( (the_block_number > 0) && (copies > 0) ) writeheap(the_block_number, CTW, copies); else if (the_block_number < 1) printf("Block number can not be less than 1.\n"); else printf("Must write at least 1 byte.\n"); } else { printf("Usage: writeheap [block_number] [char] [amount]\n"); } } else if (!strcmp(argv[0], "printheap")) { if (argc == 3) { // void printheap(int block_number, int number_of_bytes) int block_number = atoi(argv[1]); int number_of_bytes = atoi(argv[2]); if ( (block_number > 0) && (number_of_bytes) > 0) printheap(block_number, number_of_bytes); else if (block_number < 1) printf("Block number can not be less than 1.\n"); else printf("Must read at least 1 byte.\n"); } else { printf("Usage: printheap [block_number] [amount]\n"); } } else if (!strcmp(argv[0], "quit")) { // void quit() quit(); break; } else { printf("Invalid Command\n"); } } if (argc != -1) { int i; for (i = 0 ; i < argc ; i++) { free(argv[i]); } } free(argv); } }