1059 Prime Factors (25分)【质因数分解】

news/2024/7/3 1:19:33

1059 Prime Factors (25分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

解题思路:

这道题给我们一个int范围的整数n,按照从小到大的顺序输出其质因数分解的结果。这其实就是一道很普通的质因数分解板子题。我们先打好一个素数表,然后用一个结构体来保存质因子数。然后题目中说是int范围内的正整数进行质因子分解,因此我们的素数表大概开10^5就可以了。

 

#include<iostream>
#include<math.h>
using namespace std;
struct factor {
	int num;
	int cnt;
}fac[10];

int prime[100010], pNum = 0;

int isPrime(int n)   //判断是否是素数
{
	if (n <= 1)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;
	}
	return 1;
}

void primeTable()
{
	for (int i = 0; i < 100010; i++)
	{
		if (isPrime(i) == 1)
			prime[pNum++] = i;
	}
}

int main()
{
	primeTable();      //先打好表
	int n;
	int count = 0;     //记录质因子的个数
	cin >> n;
	if (n == 1)            //注意特殊情况
		cout << "1=1" << endl;
	else {
		cout << n << "=";
		for (int i = 0;prime[i] <= sqrt(n); i++)
		{
			if (n%prime[i] == 0)
			{
				fac[count].num = prime[i];
				fac[count].cnt = 0;
				while (n%prime[i] == 0)
				{
					n /= prime[i];
					fac[count].cnt++;
				}
				count++;          //质因子个数加1
			}
			if (n == 1)
				break;      //及时退出
		}
		if (n != 1)     //注意如果不能被根号n里的数整除,那么就一定有一个大于根号n的质因子
		{
			fac[count].num = n;
			fac[count++].cnt = 1;
		}
		//输出结果
		for (int i = 0; i < count; i++)
		{
			cout << fac[i].num;
			if (fac[i].cnt > 1)
				cout << "^" << fac[i].cnt;
			if (i<count-1)
				cout << "*";
		}
	}
	return 0;
}

 


http://www.niftyadmin.cn/n/4610384.html

相关文章

POJO百度百科

POJO(Plain Ordinary Java Object)简单的Java对象&#xff0c;实际就是普通JavaBeans&#xff0c;是为了避免和EJB混淆所创造的简称。 使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter setter方法的类,没有业务逻辑&#xff0c;有时可以作为…

找出一个只出现一次的字符

一&#xff0c;问题描述 给定一个字符串&#xff0c;找出一个 其中只出现一次的字符 如"abaccdeff" 只出现一次的字符有 b d e 二&#xff0c;问题分析 ①字符集是个常量 &#xff0c;字符只有那么多。比如ASCII 一共256个&#xff0c;比如 字母表一共只有26个…

ios 开源代码

1、开源代码 http://www.oschina.net/iOS/codingList/365/ios-buttonhttp://www.devdiv.com/iOS_iPhone-iOS6%E6%96%B0%E7%89%B9%E5%BE%81%EF%BC%9A%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99%E5%92%8C%E7%A4%BA%E4%BE%8B%E6%B1%87%E6%80%BB-thread-127965-1-1.htmlhttp://code4app…

Log4J配置文件详解(转载一)

2019独角兽企业重金招聘Python工程师标准>>> 网站要发布了&#xff0c;为了跟踪一些日志&#xff0c;需要用到log4j&#xff0c;于是就研究了一下log4j的配置 先贴自己用的一个配置源文件 log4j.properties log4j.rootLoggerDEBUG, CONSOLE, FILE ## for console …

1063 Set Similarity (25分)【set】

1063 Set Similarity (25分) Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers …

PostgreSQL 多国语言支持的实现

1、先了解&#xff1a;GNU gettext 2、以 pg_config 为例&#xff0c;打开 src/bin/pg_config/nls.mk # src/bin/pg_config/nls.mk CATALOG_NAME pg_config AVAIL_LANGUAGES cs de es fr it ja ko nb pl pt_BR ro ru sv ta tr zh_CN zh_TW GETTEXT_FILES pg_config.…

1060 Are They Equal (25分)【string】

1060 Are They Equal (25分) If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.12310​5​​ with simple chopping. Now given the number of significant digits on a machine …

Node.js中的HTTPS示例

需要openssl的支持&#xff0c; openssl本身不提供windows的安装程序&#xff0c;可以按照如下的步骤进行安装&#xff1a; (参考https://conetrix.com/Blog/how-to-install-openssl-on-windows-7&#xff0c;并复制到下面) How-to Install OpenSSL on Windows 7 Download and …