USACO FEB08 Problem 'cowmult' Analysis

by Rob Kolstad

A straightforward ad hoc problem that requires implementation of a rule for multiplication. Required insight to complete this problem includes the ability to take apart the digits of numbers. This is easily accomplished by using the mod 10 operator ('% 10' in C, C++, and Java) to see the last digit of a number. The number can then easily be downsized by integer-dividing it by 10 (integer divides discard remainders).

My program uses a double-nested loop (which does require re-calculation of b's digits, but since we know there won't be more than 10 or so, this is not too efficient) to run through both a's digits and b's digits. The sum keeps a running total of the sum of the digit products.

#include <stdio.h>
#include <stdlib.h>

main () {
    FILE *fin = fopen ("cowmult.in", "r");
    FILE *fout = fopen ("cowmult.out", "w");
    int a, b, i, sum, dig1, dig2;
    fscanf (fin, "%d %d", &a, &b);
    sum = 0;
    for ( ; a ; a /= 10)
        for (i = b; i ; i /= 10)
            sum += (a % 10) * (i % 10);
    fprintf (fout, "%d\n", sum);
    exit (0);
}