Thread: Programming Contest--The Problems

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    Programming Contest--The Problems

    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.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    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

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM
  2. New Monthly Contest!
    By PJYelton in forum Contests Board
    Replies: 50
    Last Post: 03-22-2005, 08:27 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  4. Intercollegiate programming contest problems
    By questionsForYou in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-13-2003, 03:22 AM