// multiply.cpp // (More) Multiplication, MCPC 2014, Problem A // C++ solution by James Feher #include #include #include using namespace std; ifstream fin( "multiply.in" ); bool DEBUG = false; // Make sure to set to false on submission void printChars(char c, int num2print) { // INPUT : character to print, number of times to print it // OUTPUT : None // PURPOSE: Print char c num2print times // Make it easier to print spaces and lines for output int i; for (i=0; i> X >> Y; // Get the numbers to multiply while (X > 0) { DEBUG && cout << "Got Data -- X: " << X << "\tY: " << Y << endl; // determing the number of digits required for each number for (i=1, numXdigits=0; i<=X; i*=10) numXdigits++; for (i=1, numYdigits=0; i<=Y; i*=10) numYdigits++; // Now get the individual digits for (i=0, j=X; j>0; i++) { Xdigits[i] = j%10; j /= 10; } for (i=0, j=Y; j>0; i++) { Ydigits[i] = j%10; j /= 10; } // Do the same for the result result = X*Y; for (i=1, numRdigits=0; i<=result; i*=10) numRdigits++; for (i=0, j=result; j>0; i++) { Rdigits[i] = j%10; j /= 10; } DEBUG && cout << " Num digits X: " << numXdigits << "\tNum digits Y: " << numYdigits << "\tNum digits R: " << numRdigits << "\tResult: " << result << endl; // print top row cout << "+"; printChars('-', 3+4*numXdigits); cout << "+" << endl; // print X formatted appropriately cout << "| "; // start out with | and 3 spaces for (i=0; i=0; i--) { int topDigit, botDigit; // if top row see if the highest order digit is zero if (i==numYdigits-1) if ( (Xdigits[numXdigits-1]*Ydigits[i]) /10 <= 0 ) firstDigitZero = true; // print the top digits // first line or second line with zero as top digit if (i==numYdigits-1 || (i==numYdigits-2 && firstDigitZero)) cout << "| |"; else cout << "|/|"; for (j=numXdigits-1; j>=0; j--) { topDigit = Xdigits[j]*Ydigits[i] / 10; cout << topDigit << " /|"; } cout << " |" << endl; // now print the middle cout << "| |"; for (j=numXdigits-1; j>=0; j--) cout << " / |"; cout << Ydigits[i] << "|" << endl; // now print the bottom digits cout << "|"; if ((i == numYdigits-1) && firstDigitZero) cout << " |"; else cout << Rdigits[i+numXdigits] << "|"; for (j=numXdigits-1; j>=0; j--) { botDigit = Xdigits[j]*Ydigits[i] % 10; cout << "/ " << botDigit << "|"; } cout << " |" << endl; printLine(numXdigits); } // print Result formatted appropriately cout << "|"; // start out with | for (i=0; i> X >> Y; // Get the numbers to multiply } return 0; }