int main(int argc, char *argv[]) {
int i = 2,j = 0,k = 0,m = 1,num;
int a[100];
while(scanf("%d",&num)!=EOF && num>1 && num<=1000000){
while(num>i){
for(i=2;i<=sqrt(num);i++){
k=0;
if(num%i==0){
k=1;
break;
}
}
if(k==1){
while(num%i==0){
a[j]=i;
if(a[j]==a[j-1]){
m++;
}
j++;
num=num/i;
}
if(m>1){
printf("%d^%d",i,m);
m=1;
}
else{
printf("%d",i);
}
if(num/i!=1){
printf(" * ");
}
else{
printf("\n");
break;
}
}
else{
printf("%d\n",num);
break;
}
}
}
return 0;
}
測試999997時,只顯示757 到底哪裡出錯了....
#include <iostream>
#include <cmath>
using namespace std ;
int main() {
int in0 ;
while (cin >>in0 ){
for (int i=2 ;i<=sqrt(in0);i++ ){
if (in0%i==0){
cout <<i ;
int i2 =0 ;
while (in0%i==0){
i2++ ;
in0/= i;
}
if (in0==1){
cout <<"^" <<i2 <<endl ;
}
else if (i2>1 ){
cout <<"^" <<i2 <<" * ";
}
else if (i2==1&in0>1){
cout <<" * " ;
}
}
}
if (in0>1)
cout <<in0 <<endl ;
}
}