likexx
BAN USERHercules is almost right, but he is using C++. If using C++, problem is much simpler because you can just push the file pointer position into a stack or whatever.
My idea is similar. To print the last N lines, we need a int[N] buffer. The buffer is used to store the file pointer location (ftell(...)). Remember the problem of "find the 5th item in a linked list in reverse order"? this is the same.
long pos[10];
1. read each char until reach "\r\n" (or just '\r')
save the current position into pos[i];
2. repeat 1, keep the buffer filled with the last 10 "\r\n" positions
(this can be optimized to avoid shifting the whole array by using some tricks.)
3. when reach EOF, fseek to the pos[0] and print out the last 10 lines.
// psuedo code
bool Match(char token)
{
char c;
switch( c=ReadToken() )
{
case '{':
case '[':
case '(':
if(Match(c))
return Match(token);
return false;
case '}':
return (token=='{');
case ']':
return (token=='[');
case ')':
return (token=='(');
case END:
return false;
default:
return Match(token);
}
}
For mask:
if (mask[i]==0)
MEM[i]=MEM[i]; //doesn't change at all;
Regardless what the original value in the memory is, we first send 0xFF to it, read the dada to temp1, then send 0x00 again, read the data out to temp2. The bits haven't changed in temp1 and temp2 are 0s in the mask (we are not comapring temp1 and temp2, but the value before writing and after).
I think the quesion description has problems. It is impossible for "ANY" 2 numbers sum up to another one in the same array".
- likexx January 04, 2008