poj1365 整数分解(质因数分解)

来源:互联网 发布:windows xp 自动登录 编辑:程序博客网 时间:2024/05/17 02:45

题意:给出一个数x的质因数表达式,请你算出x-1的质因数表达式


分析:

首先求出x,然后对x-1进行整数分解(质因数分解),整数分解可以直接套模板。

在处理过程中,发现用int定义x和y时,用pow函数运算时会造成数据丢失,因此用double定义。


首先看质因数分解模板:

//摘自《挑战程序设计竞赛》p118map<int,int> prime_factor(int n){map<int,int> res;for (int i = 2; i*i <= n; ++i){while (n%i == 0){res[n]++;n /= i;}}if(n != 1) res[n] = 1;return res;}

然后是代码:

/*@Filename: code.cpp@Version:  1.0@Author:   wyl6@Email:    17744454343@163.com*/#include <cstdio>  #include <cstdlib>  #include <iostream>  #include <stack>  #include <queue>  #include <algorithm>  #include <cstring>  #include <string>  #include <cmath>  #include <vector>  #include <bitset>  #include <list>  #include <sstream>  #include <set>  #include <map>#include <functional>  using namespace std;    #define INF 0x3f3f3f3f  #define MAXN 1000 typedef long long ll;  string s;double val,ans;double x,y;int times;void prime_factor(int n){//cout << n << endl;map<int,int> res;map<int,int> ::reverse_iterator j;for(int i = 2; i*i <= n; i++){while(n%i == 0){res[i]++;n /= i;}}if(n != 1) res[n] = 1;for (j = res.rbegin(); j != res.rend(); j++)cout << j->first << " " << j->second << " " ;cout << endl;}int main()  {      while(getline(cin,s))    {    val = 0;    times = 0;    ans = 1;    for (int i = 0; ; ++i)    {    if (s[i] == ' ' || s[i] == '\0'){    if(val == 0) return 0;    if(times%2 == 0) x=val;    else{    y = val;    ans *= pow(x,y);    //cout << ans << endl;    }    if(s[i] == '\0') break;    val = 0;    times++;    }    else{val = val*10 + s[i]-'0';//cout << val << endl;    }    }    //cout << ans << endl;   prime_factor((int)ans-1);    }    return 0;  }  


原创粉丝点击