Bob is a noob mathematician and he is not very comfortable with the addition of numbers. He is unaware of the concept of carrying that the addition of 2 numbers possesses. He every time forgets to take a carry while performing addition.
For example: suppose he adds 5 and 8 so he writes only 3 instead of 13. Similarly while adding 18 and 223 he writes it as 231.
So basically he ignores the carry every time. Your task is to compute the error in his addition algorithm.
Error is defined as the absolute difference between Bob's answer and the actual answer which was expected.
Example:
Input: n = 5, m = 8
Output: 10
Approach
C++
#include <bits/stdc++.h>using namespace std;long long additionErrors(long long n, long long m){long long ans = n + m;long long ans1 = 0, rem = 1;while (n || m){long long x = n % 10 + m % 10;n = n / 10;x = x % 10;m = m / 10;ans1 += x * rem;rem = rem * 10;}return ans - ans1;}int main(){long long n = 5, m = 8;cout << additionErrors(n, m) << "\n";return 0;}
No comments:
Post a Comment