Answer the question
In order to leave comments, you need to log in
How to split a number into array elements in c++?
It is necessary to split the input number into array elements so that each element is a digit of the number...
informatics.mccme.ru/mod/statements/view3.php?id=2...
This is a link to the problem I need to solve. I plan to solve it in the following way. Split two input numbers into arrays. Then then find all paired elements and enclose them in an array. sorting in descending order, simply output the array. I would like to get an answer to the question posed, or an alternative solution..^_^
Answer the question
In order to leave comments, you need to log in
What you want to do:
#include <iostream>
using namespace std;
const int N = (int)1e5+1;
string s;
int a[N];
int main()
{
cin >> s;
int idx = 0;
for (int i = 0; i < s.length(); i++) {
a[idx] = s[i] - '0';
idx++;
}
for (int i = 0; i < idx; i++) {
cout << a[i] << ' ';
}
return 0;
}
#include <iostream>
using namespace std;
const int N = (int)1e5+1;
string s;
int a[N];
int main()
{
cin >> s;
int idx = 0;
for (int i = 0; i < s.length(); i++) a[idx++] = s[i] - '0';
for (int i = 0; i < idx; i++) cout << a[i] << ' ';
return 0;
}
#include <iostream>
using namespace std;
const int N = (int)1e5+1;
string s;
vector <int> a[N];
int main()
{
cin >> s;
for (int i = 0; i < s.length(); i++) a.push_back(s[i] - '0');
for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
return 0;
}
First, there is no way to work with digits in C++, the minimum size is char. Secondly, the user input comes in the form of a string (array of char), numbers have nothing to do with it at all (characters can be sorted just like numbers).
You do not need to read numbers, especially since 100,000 characters is long arithmetic. You just need to read the input character by character. The task is simple.
Considering that a 100000-digit number is allowed in the condition, it is not so easy to work with it as with a number in C ++. The problem is solved by counting digits as characters of input strings. For each of the two input strings, you need to count the number of each digit. You will get two arrays of 10 elements. Then, starting from 9, you need to output digits in an amount equal to the minimum of the two values of the corresponding array cells.
For example:
280138
798081
Первый массив:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
| 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 2 | 0 |
Второй массив:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
| 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 1 |
Минимумы:
| 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+---+---+
| 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
Результат:
8810
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question