I just got back from a programming contest, figured some people here might like to try out some new problems. I personally found them both very easy, so I'm not sure how much of a challenge they'll be.
I just got back from a programming contest, figured some people here might like to try out some new problems. I personally found them both very easy, so I'm not sure how much of a challenge they'll be.
problem #1's too big to fit here without making it unreadable, so it's hosted on my site:
http://thejefffiles.com/viewimage.ph...s/prob1pg1.jpg
http://thejefffiles.com/viewimage.ph...s/prob1pg2.jpg
Ugly brute force gets the two pretty quick:Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int compare(const void *a, const void *b) { const char *x = a, *y = b; return *x > *y ? 1 : *x < *y ? -1 : 0; } int main(void) { static const char key[] = "123456789"; unsigned long i; for (i = 0; i < 32767; ++i) { char buffer[16]; unsigned long i2 = i * i; int len = sprintf(buffer, "%lu%lu", i, i2); if ( len == 9 ) { qsort(buffer, len, sizeof *buffer, compare); if ( strcmp(buffer, key) == 0 ) { printf("%lu ** 2 == %lu\n", i, i2); fflush(stdout); } } } return 0; }
Last edited by Dave_Sinkula; 02-27-2005 at 01:02 PM. Reason: [1] Added 'len' to minimize qsorting. [2] Corrected buffer size (thanks).
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Impressive programming, but I can do it in one line.
Code:#include <windows.h> int main(void) { ShellExecute(NULL, TEXT("open"), TEXT("http://www.jobfood.com/mwBrainTeaser...er01.asp?Nr=13"), NULL, NULL, SW_SHOW); return 0; }