题解 | #大整数的因子#
大整数的因子
http://www.nowcoder.com/practice/3d6cee12fbf54ea99bb165cbaba5823d
给大家一个完整的大数除法模板,而且返回了余数。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> div(vector<int> &A, int b, int &r)
{
vector<int> C;
r = 0;
for (int i = A.size() - 1; i >= 0; --i)
{
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0)
C.pop_back();
return C;
}
int main()
{
string c;
while (cin >> c && c != "-1")
{
vector<int> a, ans;
for (int i = c.size() - 1; i >= 0; --i)
a.push_back(c[i] - '0');
for (int i = 2; i <= 9; ++i)
{
int r = 0;
auto res = div(a, i, r);
if (r == 0)
ans.push_back(i);
}
if (ans.empty())
cout << "none";
else
for (int i = 0; i < ans.size(); ++i)
cout << ans[i] << " ";
cout << endl;
}
return 0;
}