PAT 1059. Prime Factors

反正知道了就是知道,不知道也想不到,很快

#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

inline void print_prime_k(long long p, long long k) {
       printf("%lld", p);
       if (k > 1) {
          printf("^%lld", k);      
       }
}

int main() {
    long long n, on;
    scanf("%lld", &n);
    on = n;
    vector<long long> ps;
    vector<long long> count;
    
    long long i = 2;
    long long last = 0;
    while (n > 1) {
        while (n % i == 0) {
              n = n/i;
              if (i != last) {
                 ps.push_back(i);
                 count.push_back(0);
                 last = i;             
              }
              count.back()++;
        }
        i++;
    }
    
    int len = ps.size();
    if (len > 0) {
       printf("%lld=", on);
       print_prime_k(ps[0], count[0]);
    } else {
      printf("%lld=%lld", on, on);       
    }
    for (int i=1; i<len; i++) {
        printf("*");
        print_prime_k(ps[i], count[i]);
    }
    system("pause");
    return 0;    
}


考虑输入是1的情况输出要为1=1

posted @ 2014-11-12 19:47  卖程序的小歪  阅读(144)  评论(0编辑  收藏  举报