1分解质因数(5分)
题目内容:
每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数。比如,6可以被分解为2x3,而24可以被分解为2x2x2x3。
现在,你的程序要读入一个[2,100000]范围内的整数,然后输出它的质因数分解式;当读到的就是素数时,输出它本身。
输入格式:
一个整数,范围在[2,100000]内。
输出格式:
形如:
n=axbxcxd
或
n=n
所有的符号之间都没有空格,x是小写字母x。
输入样例:
18
输出样例:
18=2x3x3
时间限制:500ms内存限制:32000kb
import java.util.Scanner;
public class Main {
public static boolean isPrime(int temp) {
boolean isPrime = true;
for(int i = 2; i < temp; i++) {
if(temp % i == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
public static void Output(int temp) {
boolean flag = false;
while(true) {
if (isPrime(temp)) {
if (flag) {
System.out.print("x");
}
System.out.print(temp);
return;
} else {
for (int i = 2; i < temp; i++) {
if (isPrime(i)) {
if (temp % i == 0) {
if (flag) {
System.out.print("x");
}
System.out.print(i);
flag = true;
temp /= i;
break;
}
}
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.print(n + "=");
Output(n);
}
}
2完数(5分)
题目内容:
一个正整数的因子是所有可以整除它的正整数。而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数。例如6=1+2+3(6的因子是1,2,3)。
现在,你要写一个程序,读入两个正整数n和m(1<=n<m<1000),输出[n,m]范围内所有的完数。
提示:可以写一个函数来判断某个数是否是完数。
输入格式:
两个正整数,以空格分隔。
输出格式:
其间所有的完数,以空格分隔,最后一个数字后面没有空格。如果没有,则输出一个空行。
输入样例:
1 10
输出样例:
6
时间限制:500ms内存限制:32000kb
import java.util.Scanner;
public class Main{
public static boolean isPerfect(int temp){
boolean isPerfect = false;
int sum = 0;
for(int i = 1; i < temp; i++){
if(temp % i == 0){
sum += i;
}
}
if(sum == temp){
isPerfect = true;
}
return isPerfect;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
boolean flag = false;
for(int i = n; i <= m; i++){
if(isPerfect(i)){
if(flag){
System.out.print(" ");
}
System.out.print(i);
flag = true;
}
}
}
}