|
|
#include <cstdio> int ncall; int call[100]; void solve(int K) { if (!K) return; if (K % 2 == 0) { call[ncall] = ++ncall; if (K > 2) solve(K / 2); } else { call[ncall++] = -1; solve(K - 1); } } int main( void ) { // freopen( "p1278.in", "r", stdin ); int K; scanf( "%d", &K ); if (K > 1) solve(K); for (int i = 0; i < ncall; i++) printf( "CALL %d\n", call[i] < 0 ? ncall : call[i] ); printf( "BELL&RET\n" ); return 0; } For test #1, K = 4, and my code output: CALL 1 CALL 2 BELL&RET I think it's correct. See how it works (sample input 2): These are memory cells: 0:CALL 3 1:CALL 3 2:CALL 3 3:BELL&RET Before operatons stack is empty First the first command is executed. Stack becomes (1),IP-3 BELL&RET is executed. Then IP pop-ups to 1 (from stack). The second command is executed. Stack becomes (2) and so on. The result is 4 executed commands 'BELL&RET' Nice problem! The only hint you need is that the program written below will always return 2^N. 0. CALL 1 1. CALL 2 ... N-1. CALL N N. BELL&RET 1.Amount of calls "BELL&RET" must be equal to K. 2.Some tests, to check & understand problem :) 1 BELL&RET 2 CALL 1 BELL&RET 3 CALL 2 CALL 2 BELL&RET 4 CALL 1 CALL 2 BELL&RET 5 CALL 3 CALL 2 CALL 3 BELL&RET 6 CALL 1 CALL 3 CALL 3 BELL&RET 7 CALL 4 CALL 2 CALL 4 CALL 4 BELL&RET here is my code: [code deleted] Edited by moderator 28.07.2006 10:35 |
|
|