将一个合数分解质因数输出。
C++:
#include<iostream>
using namespace std;
bool ISSS(int p) {
for (int q = 2; q <= sqrt(p); q++) {
if (p % q == 0) {
return false;
}
}
return true;
}
void FJZYS(int a){
if (ISSS(a) == true){
cout << a << " ";
}
else{
for (int i = 2; i <= sqrt(a); i++){
if (a%i == 0){
cout << i << " ";
return FJZYS(a / i);
}
}
}
}
int main(){
int a;
cin >> a;
FJZYS(a);
}
JAVA:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dm;
import java.util.Scanner;
/**
*
* @author Lenovo
*/
public class DM {
/**
* @param args the command line arguments
*/
public static boolean ISSS(int p) {
for (int q = 2; q <= Math.sqrt(p); q++) {
if (p % q == 0) {
return false;
}
}
return true;
}
public static int FJZYS(int a) {
if (ISSS(a) == true) {
System.out.print(a + " ");
return 0;
} else {
for (int i = 2; i <= Math.sqrt(a); i++) {
if (a % i == 0) {
System.out.print(i + " ");
FJZYS(a / i);
break;
}
}
return 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
FJZYS(number);
}
}
来源:CSDN
作者:RY2017_Gaoxusheng
链接:https://blog.csdn.net/RY2017_Gaoxusheng/article/details/104212903