158
推荐
#include
查看全部
编辑于 2019-02-12 20:22:30
回复(82)
86
a, res = int(input()), []
for i in range(2, a // 2 + 1):
while a % i == 0:
a = a / i
res.append(i)
print(" ".join(map(str, res)) + " " if res else str(a) + " ")
编辑于 2018-07-17 21:15:23
回复(52)
72
好多人没明白这个问题的意思,其实就是让你把输入的整数因式分解,只不过因子必须都是质数
例如:180 = 2 * 2 * 3 * 3 * 5;90 = 2 * 3 * 3 * 5;而不是找出所有的质数因子
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
long number = 0;
while(scanner.hasNextLong())
{
number = scanner.nextLong();
isPrimerFactors(number);
}
}
private static void isPrimerFactors(long num)
{
long number = num;
while(number != 1)
{
for(int i = 2; i <= number ;i++)
{
if(number % i == 0)
{
number /= i;
System.out.print(i + " ");
break;
}
}
}
}
}
发表于 2016-07-27 10:25:09
回复(39)
39
再简洁不过的C++的方式,共三种方法:
#include
#include
using namespace std;
int main()
{
long int input;
//方法一
//while (cin >> input)
//{
//while (input != 1)
//{
//for (int i = 2; i <= input; i++)
//{
//if (input % i == 0)
//{
//input /= i;
//cout << i << ' ';
//break; //只要能被i整除,i总是从2开始
//}
//}
//}
//}
//方法二
//while (cin >> input)
//{
//for (int i = 2; i <= input; i++)
//{
只要能被i整除,i总是从2开始
//if (input%i == 0)
//{
//input /= i;
//cout << i << " ";
//i = 1;//经i++之后 i又变为2开始
//}
//}
//}
//方法三
while (cin >> input)
{
for (int a = 2; a<= sqrt(input); a++)
{
//此处是while,把a整除结束才可加1
while (input%a == 0)
{
cout << a << ' ';
input = input / a;
}
}
if (input>1) cout << input << ' ';
}
//system("pause");
return 0;
}
编辑于 2016-07-04 21:47:33
回复(13)
9
#include
using namespace std;
int main()
{
long n;
while(cin>>n)
{
for(int i = 2;i <= n;i++)//从2开始
{
while(n%i==0)//每找到一个因数,除尽为止。找到的因数一定是质数
{
cout<
n /= i;
}
}
}
}
发表于 2017-08-14 16:29:57
回复(5)
9
#include
using namespace std;
int main()
{
long num;
while(cin >> num){
if(num == 1){
cout << num << endl;
continue;
}
for(int i = 2; i <= num; ++i){
if(num%i == 0){
num = num/i;
cout << i << " ";
i--;//遇到一个质数,那么该质数可能会被整除多次
}
}
cout << endl;
}
return 0;
}
发表于 2016-07-22 22:37:50
回复(4)
6
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long data = sc.nextLong();
for (int i = 2; i < data + 1; i++) {
while (data % i == 0) {
System.out.print(i + " ");
data = data / i;
}
}
}
}
发表于 2018-03-02 19:34:35
回复(8)
8
//利用回归简单一些
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
long num = scan.nextLong();
canculate(num);
}
}
public static void canculate(long num){
for(long i=2;i<=num;i++){
if(num%i==0){
System.out.print(i+" ");
canculate(num/i);
return;
}
}
}
}
发表于 2018-03-11 21:56:30
回复(8)
4
#include
int main(void)
{
long int a;
int i;
printf("Input a positive number:a\n");
scanf("%d",&a);
for (i=2;i<=a;i++)
{
while (a != i)
{
if (a%i == 0)
{
printf("%d ",i);
a = a/i;
}
else
break;
}
}
printf("%d ",a);
printf("\n");
return 0;
}
发表于 2016-08-30 15:07:46
回复(3)
4
//返回string类型,需要处理一下
#include
#include
#include
using namespace std;
string getResult(long ulDataInput);
bool IsPrinumber(int value);
int main()
{
long data;
cin >> data;
string str = getResult(data);
string substr = str.substr(0,str.length()-1);
cout << str << endl;
return 0;
}
string getResult(long ulDataInput)
{
string result="";
if (ulDataInput>1)
{
for (int i=2;i<=ulDataInput;i++)
{
if (0 == ulDataInput%i)
{
if (IsPrinumber(i))
{
stringstream s;
s<
result +=s.str();
result += " ";
ulDataInput = ulDataInput/i;
break;
}
}
}
result = result+getResult(ulDataInput);
}
return result;
}
bool IsPrinumber(int value)
{
bool flag = true;
for (int j=2;j
{
if (0 == value%j)
{
flag =false;
break;
}
}
return flag;
}
发表于 2016-03-24 16:57:13
回复(0)
12
import sys
while True:
try:
number = long(sys.stdin.readline())
i = 2
while number != 1:
while number % i == 0:
print i,
number = number / i
i = i + 1
print "",
except:
break
感谢嘻嘻女神曾经给我的鼓励!
编辑于 2016-08-05 19:57:15
回复(6)
10
import java.util.Scanner;
public class PrimeFactor{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
long l = in.nextLong();
System.out.print(getResult(l));
}
}
static String getResult(long ulDataInput){
StringBuilder str = new StringBuilder();
if(ulDataInput <= 1)
return "请输入大于1的正整数";
while(ulDataInput != 1){
for(int i = 2;i <= ulDataInput;i++){
if(ulDataInput % i == 0){
str.append(i + " ");
ulDataInput = ulDataInput / i;
break;
}
}
}
return str.toString();
}
}
发表于 2016-07-13 23:07:26
回复(9)
3
好像就我很傻还要用质因子去分解一个整数。。。。。
(说下原理,上面评论区为啥不判断是不是质因子:因为从2开始除这个数,2如果能除尽,就用2除(换言之就轮不到4,8,10等来除);2不能除,那用3去除,3能除尽就除,除不尽i继续递增;假如i递增到4了(同志们想想是不是直接将4pass了?因为4如果能除尽,那么它前面的2就已经去除了,根本轮不到4除),i再增到5,能除尽就除,除不尽用下一个6去除,同理,肯定pass,因为6如果能除尽,前面的3早就除了,轮不到6去除。。。由此可知,得到的因子自然就是质因子了!)
不过自己也写了判断素数的方法,就列上来吧,代码如下:
(其实下面代码里面判断素数的方法,和遍历vector的方法,还是比较不错的)
#include
#include
using namespace std;
bool isPrime(int x)
{
if (x <= 3)
return x > 1;
if (x%6 != 1 && x%6 != 5)
return false;
for (int i = 5; i*i <= x; i += 6)
{
if (x%i == 0 || x%(i+2) == 0)
return false;
}
return true;
}
int main()
{
int num;
while (cin >> num)
{
vector prime;
while (!isPrime(num))
{
for (int i = 2; i
if (isPrime(i) && num%i == 0)
{
prime.push_back(i);
num /= i;
break;
}
}
prime.push_back(num);
for (auto x: prime)
cout <
cout <
}
return 0;
}
发表于 2020-09-03 16:15:15
回复(0)
3
/*需要注意的是输出的最后一个质数因子也需要以空格结束*/
#include
using namespace std;
void trial_divisio_fac(int n){
int a=2;
while(a*a<=n){
while(n%a==0){
cout<
n=n/a;
}
a++;
}
if(n>1) cout<
}
int main(){
int n;
cin>>n;
trial_divisio_fac(n);
return 0;
}
发表于 2016-03-30 23:06:21
回复(1)
4
#include
using namespace std;
const int N = 1e5 + 10;
int n;
int cnt[N], p[N];
void divide(int n) {
int num = 0;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0)
p[++num] = i; //从小到大记录质数,第num个质数为i
while (n % i == 0) n /= i, cnt[num]++;
}
if (n > 1) p[++num] = n, cnt[num]++;
//若最后除出来的数大于1,说明是个质数
for (int i = 1; i <= num; ++i)
for (int j = 1; j <= cnt[i]; ++j)
cout << p[i] << ' ';
}
int main() {
cin >> n;
divide(n);
}
编辑于 2019-05-26 21:54:38
回复(0)
4
s = eval(input())
while(s != 1):
i = 2
while True:
if(s % i == 0):
print(i, end = ' ')
s = s / i
break
i = i + 1
发表于 2017-07-15 14:14:21
回复(3)
2
num= int(input())
for i in range(2, num+1):
while num % i == 0:
num = num/ i
print(i,end=' ')
发表于 2020-11-10 11:22:31
回复(2)
2
#include
#include
using namespace std;
int main()
{
long num;
while (cin >> num)
{
long oldnum = num;
for (long i = 2; i<=sqrt(oldnum); ++i)
{
while (num%i == 0)
{
cout << i << " ";
num = num / i;
}
}
if (num > 1)
cout << num << " ";
}
return 0;
}
发表于 2017-06-15 14:39:54
回复(0)
2
#include
#include
#include
#include
using namespace std;
int main()
{
long ulDataInput;
//scanf("%ld",&ulDataInput);
while(cin>>ulDataInput)
{
string result;
stringstream ss;
long tempData;
for(int i=2;i<=ulDataInput;i++)
{
while(!(tempData=ulDataInput%i))
{
ss<
ulDataInput=ulDataInput/i;
}
}
getline(ss,result);
//result.pop_back();
cout<
//printf("%s\n",result.c_str());
//system("PAUSE");
}
return 0;
}
发表于 2016-07-07 21:04:00
回复(0)
3
//阿西吧,JS调半天说我输入有错误,原来最后一个数要接空格,搞得我特意trim了一下
var readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var n;
var i;
var j;
var raw = [];
raw[0] = 2;
rl.on('line', function(line) {
n = parseInt(line.trim());
for (i = raw[raw.length - 1] + 1; i <= n; i++) {
for (j = 0; j < raw.length; j++) {
if (i % raw[j] === 0) break;
}
if (j === raw.length) raw.push(i);
else continue;
}
var result = [];
for (i = 0; i < raw.length; i++) {
while (n % raw[i] === 0) {
n = n / raw[i];
result.push(raw[i]);
result.push(" ");
}
}
result = result.join("");
console.log(result);
});
发表于 2016-08-08 01:46:01
回复(0)
4
题目并没有说输入有多组测试用例!
而且输出的最后一个因子后面为什么还会有空格?
发表于 2016-08-03 23:14:19
回复(1)