Article Directory
1. Topic
Enter two non-negative decimal integers A and B (≤ 2 30 − 1) (≤2^(30) −1)(≤2. 3 0 -. 1), the output A + B of the D (1 <D≤10) binary number.
Input format:
The input gives 3 integers A, B, and D in sequence in one line.
Output format:
Output the D number of A+B.
Input example 1:
123 456 8
Output sample 1:
3.2.1
Input example 2:
14.1.28 10.16.27
Output sample 2:
1103
2. Ideas and precautions
analysis:
- The conversion process of hexadecimal is to continuously divide the hexadecimal and take the modulus, and store it in the array
- Possible reasons that are partially correct: No special judgment on sum=0
- You can also use a container such as string or vector to store, and finally reverse, that is, reverse the order of all the elements.
Three, AC code
(1) Array storage
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int M, N, p;
int sum = 0, i = 0;
int ans[100] = {
0 };
cin >> M >> N >> p;
sum = M + N;
if(sum==0)
{
printf("0\n");
return 0;
}
while (sum != 0)
{
ans[i] = sum % p;
sum /= p;
i++;
}
for (int k = i-1; k >= 0; k--)
{
cout << ans[k];
}
}
(2) Vector container
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int convert(int k, int p);
int main()
{
vector<int>ans;
vector<int>::iterator it;
int M, N, p, sum = 0;
cin >> M >> N >> p;
sum = M + N;
if (sum == 0) //特判
{
cout << "0" << endl;
}
else
{
while (sum != 0)
{
ans.push_back(sum % p);
sum /= p;
}
reverse(ans.begin(), ans.end()); //最后逆序输出
for (it = ans.begin(); it != ans.end();++it)
{
cout << *it;
}
}
}
(3) stirng to store the hexadecimal number
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int convert(int k, int p);
int main()
{
int M, N, p, sum = 0;
cin >> M >> N >> p;
sum = M + N;
if (sum == 0)
{
cout << "0" << endl;
return 0;
}
else
{
string str ="";
while (sum != 0)
{
to_string(sum % p);
str=str+ to_string(sum % p);;
sum /= p;
}
reverse(str.begin(), str.end());
cout << str << endl;
}
}
The above three are acceptable.