A recent written test involved adding two large numbers, one for Soundnet and one for Trend Micro, of which trend Micro was more difficult because it involved decimals. Here’s a summary. In general, what I do is I take the input as a string and add up each bit of the string.
Sound network subject
1. The subject
Input: 1 5 Output: 1+11+111+1111+11111 The result is that the first input digit a is between 0 and 9, and the second digit N represents the highest number of digits composed of a
2. The train of thought
The core part of this problem is the code for adding two strings.
Case 1: Add strings of equal length in two steps:
(1) Add each bit of the two strings from the lowest to the highest. (2) If the last carry is 1, add 1 to the head of the resulting string
Case 2: Add strings of equal length in three steps:
(1) For shorter string lengths, each bit of the result needs to be obtained by str1 and STR2 plus the carry sum (2) For longer strings than shorter strings, each bit of the result should be obtained by str1(assuming str1 is longer) plus the carry sum (3). Finally, if the carry is 1, You need to add 1 to the head of the result string
3. Code
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string sumOfStr(string str1, string str2)
{
string sum;
int carry=0;
int len1 = str1.size();
int len2 = str2.size();
int maxlen = max(len1,len2);
int minlen = min(len1,len2);
string maxstr = (len1 >= len2) ? str1 : str2;
// Start at the tail and add
for (int i = 0; i < minlen; i++)
{
int num1 = str1[len1 - 1 - i]-'0';
int num2 = str2[len2 - 1 - i]-'0';
int temp = num1 + num2 + carry;
if (temp >= 10)
{
int remain = temp % 10;
carry = 1;
sum.push_back((remain+'0'));
}
else
{
int remain = temp;
carry = 0;
sum.push_back((remain + '0')); }}for (int i = minlen; i < maxlen; i++)
{
int num = maxstr[maxlen - 1 - i] - '0';
int temp = num + carry;
if (temp >= 10)
{
int remain = temp % 10;
carry = 1;
sum.push_back((remain + '0'));
}
else
{
int remain = temp;
carry = 0;
sum.push_back((remain + '0')); }}if(carry==1)
sum.push_back('1');
reverse(sum.begin(), sum.end());
return sum;
}
int main(a)
{
int a, n;
cin >> a >> n;
string add = to_string(a);
string temp;
int i = n;
string sum;
while (i--)
{
temp = temp + add;
sum = sumOfStr(sum,temp);
}
cout << sum << endl;
return 0;
}
Copy the code
Trend Micro topic
1. The subject
For the sum of two long not negative, requirements: no loss of accuracy of the first are not meaningless 0 not appears meaningless decimal point Shall not be used except character operation function of the input: two nonnegative number, each number meet: contains only the Numbers 0 to 9, 0 or 1 a decimal point Length is not more than 200 does not need to wrong output: the sum of two Numbers
2. The train of thought
First count the number of decimal digits in each string. If they are integers, they are 0
Add 0 to the end of a string with a smaller decimal part, and add the difference between the two decimal parts
Removes the decimal point from two strings
Call the string addition function described above
Add a decimal point to the result so that the decimal part of the result is the same length as the previous longest decimal part
The case of removing redundant zeros and decimal points from the tail
3. Code
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void deleletTheZeroAndPoint(string & result, int pointLen)
{
// Delete the extra 0
for (int i = 0; i < pointLen; i++)
{
if (result[result.size() - 1] != '0')
break;
else if (result[result.size() - 1] = ='0')
result.erase(result.end()- 1);
}
// Delete extra points
if(result[result.size()- 1] = ='. ')
result.erase(result.end() - 1);
return;
}
void renewTheStr(string & str1, string & str2, int pointLen1, int pointLen2)
{
/ / add 0
if (pointLen1 > pointLen2)
{
int i = pointLen1 - pointLen2;
while (i--)
{
str2.push_back('0'); }}else if (pointLen1 < pointLen2)
{
int i = pointLen2 - pointLen1;
while (i--)
{
str1.push_back('0'); }}else
;
// If there is a decimal point, remove it
int maxLen = max(pointLen1, pointLen2);
if(pointLen1 ! =0)
str1.erase(str1.end() - maxLen - 1);
if(pointLen2 ! =0)
str2.erase(str2.end() - maxLen - 1);
return;
}
int getThePointBit(string str)// Get two decimal parts of the string
{
int len = 0;
bool signal = false;
for (int i = 0; i < str.size(); i++)
{
if (signal)
len++;
if (str[i] == '. ')
signal = true;
}
return len;
}
string sumOfStr(string str1, string str2)
{
string sum;
int carry=0;
int len1 = str1.size();
int len2 = str2.size();
int maxlen = max(len1,len2);
int minlen = min(len1,len2);
string maxstr = (len1 >= len2) ? str1 : str2;
// Start at the tail and add
for (int i = 0; i < minlen; i++)
{
int num1 = str1[len1 - 1 - i]-'0';
int num2 = str2[len2 - 1 - i]-'0';
int temp = num1 + num2 + carry;
if (temp >= 10)
{
int remain = temp % 10;
carry = 1;
sum.push_back((remain+'0'));
}
else
{
int remain = temp;
carry = 0;
sum.push_back((remain + '0')); }}for (int i = minlen; i < maxlen; i++)
{
int num = maxstr[maxlen - 1 - i] - '0';
int temp = num + carry;
if (temp >= 10)
{
int remain = temp % 10;
carry = 1;
sum.push_back((remain + '0'));
}
else
{
int remain = temp;
carry = 0;
sum.push_back((remain + '0')); }}if(carry==1)
sum.push_back('1');
reverse(sum.begin(), sum.end());
return sum;
}
int main(a)
{
string str1, str2;
getline(cin, str1);
getline(cin, str2);
// Get two decimal parts of the string
int pointLen1, pointLen2;
pointLen1 = getThePointBit(str1);
pointLen2 = getThePointBit(str2);
int maxPointLen = max(pointLen1, pointLen2);
// Add 0 to the end of the short decimal part of the character and remove two string decimal points
renewTheStr(str1,str2,pointLen1,pointLen2);
// Add strings
string result = sumOfStr(str1,str2);
// Add decimal point
if(maxPointLen ! =0)
result.insert(result.end()-maxPointLen,'. ');
// Delete the redundant '0' and decimal point at the end
if(maxPointLen ! =0)
deleletTheZeroAndPoint(result,maxPointLen);
cout << result << endl;
return 0;
}
Copy the code
Test inputs
Both are integers:
An integer and a decimal:
Both are decimals, with no useless zeros at the end:
Both are decimals with useless zeros and dots at the end:
Both are decimals with useless zeros at the end:
This problem is rather tedious and requires attention to many details.