a010.
因數分解
| From: [61.217.32.174] |
發表日期
:
2011-01-09 02:44
#include <stdio.h>
#include <string.h>
int count(int x);
int check(int x,int y);
void even(int x);
void odd(int x);
int counter;
int main(){
int number,temp=1;
while(scanf("%d",&number)!=EOF){
counter=0;
if(count(number)==1){
printf("%d",number);
}
else{
if(number%2==0){
even(number);
}
else{
odd(number);
}
}
printf("\n");
}
}
int count(int x){
int temp;
for(temp=2;temp<=sqrt(x);temp++){
if(x%temp==0){
return 0;
}
}
return 1;
}
void odd(int x){ /*奇數*/
int temp,times=0,y=x;
for(temp=3;temp<=(y/2) ;temp++){
if(counter==0){
if(x%temp==0){
times=check(x,temp);
if(times!=1){
printf("%d^%d",temp,times);
x/=pow(temp,times);
counter++;
}
else{
printf("%d",temp);
x/=temp;
counter++;
}
}
}
else{
if(x%temp==0){
printf(" * ");
times=check(x,temp);
if(times!=1){
printf("%d^%d",temp,times);
x/=pow(temp,times);
}
else{
printf("%d",temp);
x/=temp;
counter++;
}
}
}
}
}
void even(int x){ /*奇數*/
int temp,times=0,y=x;
for(temp=2;temp<=(y/2);temp++){
if(counter==0){
if(x%temp==0){
times=check(x,temp);
if(times!=1){
printf("%d^%d",temp,times);
x/=pow(temp,times);
counter++;
}
else{
printf("%d",temp);
x/=temp;
counter++;
}
}
}
else{
if(x%temp==0){
printf(" * ");
times=check(x,temp);
if(times!=1){
printf("%d^%d",temp,times);
x/=pow(temp,times);
}
else{
printf("%d",temp);
x/=temp;
counter++;
}
}
}
}
}
int check(int x,int y){
int time=0;
do{
time++;
x/=y;
}
while(x%y==0);
return time;
}