他說我在輸入261855的時候,答案會是:2 * 3^2 * 5 * 11 * 23^2 ,但我自己測試是正確的(3^2 * 5 * 11 * 23^2)
#include <iostream>
using namespace std;
int main()
{
int n=0;
int counter=0;
while(cin >> n)
{
for(int i=2;i<=n;i++)
{
while(n%i==0)
{
n=n/i;
counter++;
}
if(counter>1)
{
if(n!=1)
{
cout << i << "^" << counter << " " << "*" << " " ;
counter=0;
}
else
{
cout << i << "^" << counter << endl ;
break;
}
}
else if(counter==1)
{
if(n!=1)
{
cout << i << " " << "*" << " " ;
counter=0;
}
else
{
cout << i << endl ;
break;
}
}
}
}
return 0;
}
他說我在輸入261855的時候,答案會是:2 * 3^2 * 5 * 11 * 23^2 ,但我自己測試是正確的(3^2 * 5 * 11 * 23^2)
#include
using namespace std;
int main()
{
int n=0;
int counter=0;
while(cin >> n)
{
for(int i=2;i<=n;i++)
{
while(n%i==0)
{
n=n/i;
counter++;
}
if(counter>1)
{
if(n!=1)
{
cout << i << "^" << counter << " " << "*" << " " ;
counter=0;
}
else
{
cout << i << "^" << counter << endl ;
break;
}
}
else if(counter==1)
{
if(n!=1)
{
cout << i << " " << "*" << " " ;
counter=0;
}
else
{
cout << i << endl ;
break;
}
}
}
}
return 0;
}
我是這樣寫
#include <iostream> #include <iomanip> using namespace std; int main() { int a,b,c; while(cin>>a) { b=2; while(b<=a) { c=0; while(a%b==0) { a=a/b; c=c+1; } if(c>0) { cout<<b; if ( c > 1 ) cout << "^" << c ; if ( a > 1 ) cout << " * " ; } b++; } cout<<endl; } }