Easy
A little boy is studying arithmetics. He has just learned how to add two integers, written one below another, column by column. But he always forgets about the important part - carrying.
Given two integers, your task is to find the result which the little boy will get.
Note: The boy had learned from this site, so feel free to check it out too if you are not familiar with column addition.
Example
For param1 = 456 and param2 = 1734, the output should be
additionWithoutCarrying(param1, param2) = 1180.
456
1734
+_____
1180
The boy performs the following operations from right to left:
- 6 + 4 = 10 but he forgets about carrying the 1 and just writes down the 0 in the last column
- 5 + 3 = 8
- 4 + 7 = 11 but he forgets about the leading 1 and just writes down 1 under 4 and 7.
- There is no digit in the first number corresponding to the leading digit of the second one, so the boy imagines that 0 is written before 456. Thus, he gets 0 + 1 = 1.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] integer param1
A non-negative integer.
Guaranteed constraints:
0 ≤ param1 < 10^5. -
[input] integer param2
A non-negative integer.
Guaranteed constraints:
0 ≤ param2 < 6 · 10^4. -
[output] integer
- The result that the little boy will get by using column addition without carrying.
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
Solution
int additionWithoutCarrying(int param1,int param2)
{
int answer=0, N=0;
while(pow(10,++N)<(param1>param2?param1:param2));
N--;
while(N>=0)
{
answer+=(int)pow(10,N)*((param1/(int)pow(10,N)+param2/(int)pow(10,N))%10);
N--;
}
return answer;
}
'Codesignal' 카테고리의 다른 글
<Codesignal> Increase Number Roundness (0) | 2020.04.12 |
---|---|
<Codesignal> Apple Boxes (0) | 2020.04.12 |
<Codesignal> Lineup (0) | 2020.04.12 |
<Codesignal> Magical Well (0) | 2020.04.12 |
<Codesignal> Count Sum of Two Representations 2 (0) | 2020.04.12 |