Solutions to Day 1 Problems


All Ireland Schools Programming Competition. How to enter Time Table Rules DCU Computers Guidelines Sample Problems Sample Problems IOI


Problem A

#include < stdio.h> 
#include < stdlib.h> 
#include < string.h> 
#include < ctype.h> 
 
char *iname = "day1a.dat";
char *oname = "day1a.sol";
 
#define MAX_STRING_LEN 80
 
char * get_line(FILE * fptr, char *str)
{
   char *ptr = str;
   int c = getc(fptr);
 
   while(c != '\n' & &  c != EOF & &  ptr - str <  MAX_STRING_LEN)
      {
      *ptr++ = c;
      c = getc(fptr);
      }
 
   *ptr = 0;
 
   if (c == EOF & &  *str == 0) str = NULL;
 
   return str;
}
 
void out_line(FILE * fptr, char *ptr)
{
   int i = 0;
 
   while(*ptr)
      {
      fputc(*ptr++, fptr);
      i++;
      if (i % 40 == 0)
         fputc('\n', fptr);
      }
}
 
void uncompress(char *in_ptr, char * out_ptr)
{
   int num;
 
   while(*in_ptr) 
      {
      if (isdigit(*in_ptr))
         {
         /* Find the number */
         num = 0;
         while(isdigit(*in_ptr))
            num = num * 10 + *in_ptr++ - '0';
 
         while(num--)
            *out_ptr++ = *in_ptr;   /* Stick in the next characters */
 
         if (*in_ptr) in_ptr++; /* Finished with this character, move on */
         }
      else
         *out_ptr++ = *in_ptr++;   /* No digit =>  straightforward copy */
      }
   *out_ptr = 0;
}
 
main()
{
   FILE *finptr, *foutptr;
   char instring[MAX_STRING_LEN];
   char outstring[MAX_STRING_LEN];
 
   finptr = fopen(iname, "r");
 
   if (finptr == NULL)
      {
      printf("Can't find the file %s\n", iname);
      exit(1);
      }
 
   foutptr = fopen(oname, "w");
 
   if (foutptr == NULL)
      {
      printf("Can't open the %s\n", oname);
      exit(1);
      }
 
   get_line(finptr, instring);
 
   uncompress(instring, outstring);
 
   out_line(foutptr, outstring);
 
   fclose(finptr);
   fclose(foutptr);
 
   return 0;
}

Problem B

Note that this program contains a error.
#include < stdio.h> 
#include < stdlib.h> 
 
char *iname = "day1b.dat";
char *oname = "day1b.sol";
 
void out_num(int num)
{
   FILE *fptr = fopen(oname, "w");
 
   if (fptr == NULL)
      {
      printf("Can't open the %s\n", oname);
      exit(1);
      }
   else
      {
      fprintf(fptr, "%d\n", num);
      printf("%d\n", num);
      }
}
 
/*
**      C function to get a number from the keyboard
*/
int get_num(void)
{
   int num = 0;
   int c;
   FILE *finptr;
 
   finptr = fopen(iname, "r");
 
   if (finptr == NULL)
      {
      printf("Can't find the file %s\n", iname);
      exit(1);
      }
 
   while(!isdigit(c = fgetc(finptr)))
      ;
 
   num = c - '0';      /* Put in the first digit */
 
   while(isdigit(c = fgetc(finptr)))
      num = 10 * num + c - '0';   /* add in the remaining digits */
 
   return num;
}
 
main()
{
   unsigned long fact = 1;
   unsigned int n, i;
 
   n = get_num();
 
   /* Calculate the factorial */
   for (i = 2; i < = n; i++)
      {
      fact = fact * i;
 
      while (fact % 10 == 0)
         fact = fact / 10;
 
      fact = fact % 100;
      }
 
   out_num((int) fact % 10);
 
   return 0;
}

Problem C

/*
**      Solution to prob 1C
*/
#include < stdio.h> 
#include < stdlib.h> 
 
#define FALSE 0
#define TRUE !FALSE      /* not FALSE */
 
#define MAX_SIZE 10
 
int size;
 
int pattern[MAX_SIZE][MAX_SIZE];
int transform[MAX_SIZE][MAX_SIZE];
 
void read_array(FILE *finptr, int array[MAX_SIZE][MAX_SIZE])
{
   int x, y;
   char temp;
 
   for (y = 0; y <  size; y++)
      {
      for (x = 0; x <  size; x++)
         {
         fscanf(finptr, "%c", & temp);
         if (temp == '.')
            array[x][y] = 0;
         else if (temp == 'X')
            array[x][y] = 1;
         else
            {
            printf("Error reading input\n");
            exit(1);
            }
         }
 
      fscanf(finptr, "%c", & temp);      /* Use up the new line */
      }
}
/*
**
*/
void get_input(void)
{
   int x, y;
   char temp;   /* To hold the input */
 
   FILE * finptr;
 
   finptr = fopen("day1c.dat", "r");
 
   if (finptr == NULL)
      {
      printf("Can't find the file day1c.dat\n");
      exit(1);
      }
 
   fscanf(finptr, "%d", & size);
   fscanf(finptr, "%c", & temp);      /* Use up the new line */
 
   printf("Read the file, the number is %d\n", size);
 
   read_array(finptr, pattern);
 
   fscanf(finptr, "%c", & temp);      /* Use up the new line */
 
   read_array(finptr, transform);
 
   fclose(finptr);
}
 
/*
**      Print the array
**
**      Useful function to visually check that every thing is OK
*/
void print_array(int array[MAX_SIZE][MAX_SIZE])
{
   int x, y;
 
   for (y = 0; y <  size; y++)
      {
      for (x = 0; x <  size; x++)
         printf("%d", array[x][y]);
      printf("\n");
      }
}
 
void copy(int s[MAX_SIZE][MAX_SIZE], int d[MAX_SIZE][MAX_SIZE])
{
   int x, y;
 
   for(x = 0 ;x <  size; x++)
      for(y = 0 ;y <  size; y++)
         d[x][y] = s[x][y];
}
 
void vertically_reflect(int array[MAX_SIZE][MAX_SIZE])
{
   int x, y;
   int temp[MAX_SIZE][MAX_SIZE];
 
   copy(array, temp);
 
   for(x = 0 ;x <  size; x++)
      for(y = 0 ;y <  size; y++)
         array[x][size - y - 1] = temp[x][y];
}
 
isequal(int s[MAX_SIZE][MAX_SIZE], int d[MAX_SIZE][MAX_SIZE])
{
   int x, y;
   int equal = TRUE;      /* Assume equal until proved otherwise */
 
   for(x = 0 ;x <  size; x++)
      for(y = 0 ;y <  size; y++)
         if (s[x][y] != d[x][y])
            equal = FALSE;
 
   return equal;
}
 
void rotate90(int array[MAX_SIZE][MAX_SIZE])
{
   int x, y;
   int temp[MAX_SIZE][MAX_SIZE];
 
   copy(array, temp);
 
   for(x = 0 ;x <  size; x++)
      for(y = 0 ;y <  size; y++)
         array[size - y - 1][x] = temp[x][y];
}
 
rotated(int s[MAX_SIZE][MAX_SIZE], int d[MAX_SIZE][MAX_SIZE])
{
   int angle;
 
   int temp[MAX_SIZE][MAX_SIZE];
 
   copy(s, temp);
 
   /* For each angle, rotate the pattern and check if equal to transformation */
   for(angle = 90; angle < =270; angle += 90)
      {
      rotate90(temp);
      if (isequal(temp, d))
         break;      /* Found the rotation, leave the for loop */
      }
 
   if (angle >  270) angle = FALSE;   /* No valid rotation */
 
   return angle;
}
 
main()
{
   FILE *output;
   int rotation;
 
   output = fopen("day1c.sol", "w");
   if (output == NULL)
      {
      printf("Error opening day1c.sol\n");
      exit(1);
      }
 
   get_input();
 
   printf("The pattern is\n");
   print_array(pattern);
   printf("The Transform is\n");
   print_array(transform);
 
   /* Must check all possibilities */
   if (isequal(pattern, transform))
      fprintf(output, "NOT TRANSFORMED.\n");
   else if (rotated(pattern, transform))
      {
      rotation = rotated(pattern, transform);
      fprintf(output, "ROTATED %d DEGREES.\n", rotation);
      }
   else
      {
      /* None of the above, check if it could have been a vertical */
      /* reflection followed by a rotation */
      vertically_reflect(pattern);
      if (isequal(pattern , transform))
         fprintf(output, "REFLECTED.\n");
      else if (rotated(pattern, transform))
         {
         rotation = rotated(pattern, transform);
         fprintf(output, "REFLECTED AND ROTATED %d DEGREES.\n", rotation);
         }
      else
         /* Nothing left, can't be transformed */
         fprintf(output, "IMPROPER TRANSFORMATION.\n");
      }
 
   return 0;
}