PATA1059题解

//
//  main.cpp
//  PATA1059
//
//  Created by Phoenix on 2018/2/15.
//  Copyright © 2018年 Phoenix. All rights reserved.
//

#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 100010;
int prime[maxn], factor[50];
int num = 0;

void findPrime() {
        for(int i = 2; i < maxn; i++) {
        bool flag = true;
        for(int j = 2; j <= sqrt(i); j++) {
            if(i % j == 0) {
                flag = false;
                break;
            }
        }
        if(flag == true) prime[num++] = i;
    }
}

int main(int argc, const char * argv[]) {
    findPrime();
    for(int i = 0; i < 100; i++) {
        //printf("%d ", prime[i]);
    }
    int n, k = 0, j = 0;
    scanf("%d", &n);
    if(n == 1) printf("1=1\n");
    else{
        printf("%d=", n);
        while(n > 1 && k < num) {
            if(n % prime[k] == 0) {
                factor[j++] = prime[k];
                n /= prime[k];
            } else{
                k++;
            }
        }
        if(n > 1) factor[j++] = n;
        int t = 0;
        while(factor[t+1] == factor[t]) {
            t++;
        }
        if(t == 0) printf("%d", factor[t]);
        else printf("%d^%d", factor[0], t + 1);
        t++;
        while(t < j) {
            int j = 0;
            while(factor[t+1] == factor[t]) {
                j++;
                t++;
            }
            if(j == 0) printf("*%d", factor[t]);
            else printf("*%d^%d", factor[t], j+1);
            t++;
        }
    }
    
    return 0;
}

你可能感兴趣的:(PAT)