hdoj-1164-Eddy's research I【分解质因数】

Eddy‘s research I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7537 Accepted Submission(s): 4579

Problem Description

Eddy‘s interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can‘t write program, so Eddy has to ask intelligent you to help
him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

Input

The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

11
9412

Sample Output

11
2*2*13*181

Author

eddy

Recommend

JGShining | We have carefully selected several similar problems for you:
1215 1211 1299 1163 1128

#include<stdio.h>
int main(){
	int  x,i;
	while(~scanf("%d",&x)){
		i=2;
		while(1){
		    if(i==x) {
				printf("%d\n",i);
				break;
			}
			if(x%i==0) {
				printf("%d*",i);
				x/=i;
			}
			else {
				++i;
			}

		}
	}
	return 0;
}

刚开始不明白为什么可以直接进行++i,而不再考虑 i 是否为质数,之后才明白过来: 因为从2开始分解,一直分解到剩下的因子不再能够被2整除为止,才进行对下一个数的分解,而 对于合数,它总能够分解为几个素数的乘积;然而此合数的素数因子已经在之前都被完全提取出来了,所以对于x 是合数的情况,不可能再找到 x%i==0 (注意:此处的X已经不是最开始输入的那个x了),另外:每个素数之间又没有什么关系,先去分解哪个质数,其最终得到的质因子是完全一样的,所以也不用担心前面的分解结果会对后面的分解造成影响!!!

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj-1164-Eddy's research I【分解质因数】

时间: 2024-11-05 19:35:00

hdoj-1164-Eddy's research I【分解质因数】的相关文章

HDOJ 1164 Eddy&#39;s research I

[题意]:对一个数进行因式分解. [思路]:打表后一个一个除,最大数是65535,所以10000的质数范围苟用. [AC代码]: #include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <iomanip> using namespace std; #define MAX 10000 i

hdu 1164 Eddy&#39;s research I

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 #define MAX 65535 9 int prime[MAX+5]; 10 bool vis[MAX+5]; 11 int index=0; 1

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

HDU 1164 Eddy&#39;s research I【素数筛选法】

思路:将输入的这个数分成n个素数的相乘的结果,用一个数组存储起来.之后再输出就可以了 Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6633    Accepted Submission(s): 3971 Problem Description Eddy's interest is very ext

HDU-1164-Eddy&#39;s research I (分解质因数)

由于这道题目数据范围小,所以属于水题.可以采取暴力的做法来解决. 代码如下: #include"bits/stdc++.h" using namespace std; const int maxn=65535; bool tag[maxn+5]; vector<int>v;int n; bool judge(int m){ while(n%m==0){ if(n==m){ printf("%d\n",m); return true; } else pri

分解质因数模板

/*==================================================*| 分解质因数,可能有些地方需要改为long long \*==================================================*/ const int MAXN=100010; int prm[MAXN+1]; bool is[MAXN+1]; int getprm(int n){ int i, j, k = 0; int s, e = (int)(sqrt

java编程题 --分解质因数

package Solve; import java.util.Scanner; public class Solve { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入一个正整数:"); int num = scan.nextInt(); System.out.print(num + " = "

HDU 1165 Eddy&#39;s research II

Eddy's research II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3970    Accepted Submission(s): 1459 Problem Description As is known, Ackermann function plays an important role in the sphere

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a