#include <iostream> #include <vector> #include <utility> using namespace std; vector< pair<int, int> > factors; int n;
n = 0; cout << " x = "; cin >> x; int x_original = x; int div = 2; int nrd = 0; while (x % 2 == 0) { nrd++; x /= 2; } if (nrd > 0) { factors.push_back(make_pair(2, nrd)); }
div = 3; while (x > 1) { nrd = 0; while (x % div == 0) { x /= div; nrd++; } if (nrd > 0) { factors.push_back(make_pair(div, nrd)); } div += 2; }
bool first = true; vector< pair<int, int> >::iterator it; cout << endl << x_original << " = "; for (it = factors.begin(); it != factors.end(); it++) { if (first == false) { cout << " * "; } else { first = false; } cout << (*it).first << "^" << (*it).second; }
#include <iostream> #include <vector> #include <utility> using namespace std; vector< pair<int, int> > factors; int n; int main(int argc, char** argv) { int x; n = 0; cout << " x = "; cin >> x; int x_original = x; int div = 2; int nrd = 0; while (x % 2 == 0) { nrd++; x /= 2; } if (nrd > 0) { factors.push_back(make_pair(2, nrd)); } div = 3; while (x > 1) { nrd = 0; while (x % div == 0) { x /= div; nrd++; } if (nrd > 0) { factors.push_back(make_pair(div, nrd)); } div += 2; } bool first = true; vector< pair<int, int> >::iterator it; cout << endl << x_original << " = "; for (it = factors.begin(); it != factors.end(); it++) { if (first == false) { cout << " * "; } else { first = false; } cout << (*it).first << "^" << (*it).second; } cout << endl; return 0; }
x = 120 120 = 2^3 * 3^1 * 5^1