Problem 4: Multiplication
The task is simple. Given 2 integers A and B, find A*B.
Input:
The first line of input contains the number of test cases T.
Each of the next T lines contains two integers A and B separated by spaces.
Output:
For each of the T cases, output A*B in a new line.
Constraints:
T<30
A=32-bit integer
0<=B<=20
Sample Input:
2
3 4
1 0
Output:
12
0
Score:
C: Number of non-whitespace characters
M: Number of * and / characters
L: Number of loops i.e. for, while and do-while
I: Number of if-else, ternary and goto
T=30000/(C + 3000*M + L^10 + 1000*I)
Solution:
The easy task of multiplication A and B can be performed by:
- Directly using A*B.
- B times addition of A.
- B times addition can be performed by a loop.
- Or, by use of a recursion:
a,b,s=0; multiply() { switch(b) case 0 :return; s+=a; b--; multiply(); }
Here, a*b is stored in s.
- Small loops can be replaced with switch statements, like if s=some int, and we know b<=4, then:
switch(b) { case 4:s+=a; case 3:s+=a; case 2:s+=a; case 1:s+=a; }
Since, no break have been used in case statements. So, whenever 'b' matches with a case, then all case statements following are executed.
Solutions through 2(ii) and 3 score better for the given constraints.
Submit your solution
You need to be logged in to submit a solution.
discussions
There is no discussion yet. Feel free to ask an question you might have regarding this problem
post a question
Loading...



