HDU 1164 Eddy's research I(素数积)

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

题意:将一个数化成素数的积。

#include <cmath>
#include <iostream>
using namespace std;
bool isprime(int n) {
	if(n < 2) return false;
	for(int i = 2; i <= sqrt(n); i++) {
		if(n%i==0) return false;
	}
	return true;
}
int main(){
	int x, t, k, num[100];
	while(cin>>x) {
		t = x;
		k = 0;
		while(t>1) {
			for(int i = 2; i <= x; i++) {
				if(isprime(i) && (t%i==0)) {
					num[k++] = i;
					t /= i;	
					break;
				}
			}	
		}
		for(int i = 0; i < k-1; i++) {
			cout<<num[i]<<"*";
		}
		cout<<num[k-1]<<endl;
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/adusts/article/details/80677308
今日推荐