+ 1
problem
hi guys have any idea how to do this problem? Requirement Read from the keyboard two natural numbers A and B. Insert the number B in the middle of the number A. For example if we read A = 1234 and B = 77 then the result obtained will be N = 127734. Input data On the first line will be the natural numbers A and B. Output data The result obtained after insertion will be displayed. Restrictions and specifications 0 <A, B <1,000,000 It is guaranteed that A has an even number of digits. Example Input data Output data 123456 9 1239456 1234 77 127734
4 Answers
+ 1
Andrei Macovei
As A has an even number so equally divide this number and put B between these divided numbers.
0
I think Martin Taylor's solution is the best approach.
In case it is of interest, you can manipulate the values mathematically, too.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long a, b;
cin >> a >> b;
cout << a << "\n" << b << endl;
//half of a's order of magnitude
float ashift = pow(10, (floor(log10(a))+1)/2);
//digits in right half of a
float amod = a%(long)ashift;
//b's order of magnitude
float bshift = pow(10, floor(log10(b)) + 1)
/* Assemble the pieces:
1. Remove the lower half of digits in a.
2. Shift a leftward by number of digits in b.
3. Add back in the lower half of a.
*/
cout << (long)((a - amod)*bshift + b*ashift + amod << endl;
return 0;
}
https://code.sololearn.com/cIqeBY5OaBX2/?ref=app