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

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


分析:

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

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


首先看质因数分解模板:

//摘自《挑战程序设计竞赛》p118
map<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;  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值