寫個for迴圈(2<i<n+1),裡面套while(一直除for迴圈的i,每除一次讓一個變數(這裡叫j)++,直到不整除),for迴圈跳出來後,將i和j分別丟進兩個vector裡,cout時一組組輸出:
int main()
{
int a, b, c, d, n;
vector<int> v,p;
while (cin >> a)
{
for (int i = 0; i <= a-2; i++)
{
b = 0;
if (a % (i + 2) == 0)
{
v.push_back(i + 2);
while (a % (i + 2) == 0)
{
a /= (i + 2);
b++;
}
p.push_back(b);
}
}
for (int i = 0; i < v.size() - 1; i++)
{
if (p[i] > 1)
{
cout << v[i] << "^" << p[i] << " * ";
}
else
cout << v[i] << " * ";
}
if (p[p.size() - 1] > 1)
{
cout << v[v.size() - 1] << "^" << p[p.size() - 1];
}
else
cout << v[v.size() - 1];
}
}