2014年6月24日 星期二

uva 516 Prime Land

數字都是以質因數表示。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int prime(int num){
    int ok,i;

    ok=1;
    for(i=2;i<=sqrt(num);++i)
        if(num%i==0){
            ok=0;
            break;
        }

    return ok;
}

int main(void){
    double base,factor;
    double number;
    int div;
    int counter,space;

    char c,cc;

    while(1){
        scanf("%lf%c",&base,&c);
        if(base==0)
            break;
        else{
            scanf("%lf%c",&factor,&cc);
            number = pow(base,factor);
            while(1){
                if(cc =='\n')
                    break;
                scanf("%lf%c%lf%c",&base,&c,&factor,&cc);
                number *= pow(base,factor);
            }
        }
        --number;

        div=number;
        space=1;
        while(number>1){
            if(prime(div)==1){
                counter=0;
                while((int)number%div==0){
                    ++counter;
                    number/=div;
                }
                if(space==1 && counter>=1){
                    printf("%d %d",div,counter);
                    space=0;
                }else if(counter>=1){
                    printf(" %d %d",div,counter);
                }
            }
            --div;
        }
        printf("\n");
    }


    return 0;
}

沒有留言:

張貼留言