用java编写一个程序,提示用户输入一个正整数,然后以降序显示其最小的因子。
答案:2 悬赏:60 手机版
解决时间 2021-03-25 06:51
- 提问者网友:放下
- 2021-03-24 19:45
用java编写一个程序,提示用户输入一个正整数,然后以降序显示其最小的因子。
最佳答案
- 五星知识达人网友:酒者煙囻
- 2021-03-24 20:34
package com.leejiliang.HomeworkDemo2;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MinimalElements {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个整数:");
int inner = input.nextInt();
Stack stack = getElements(inner);
System.out.println(inner + "的所有最小银子为:" + stack);
}
public static Stack getElements(int val) {
Stack stack = new Stack();
int i;
for (i = 2; i <= val;)// 注意是小于等于
{
if (val % i == 0) {
stack.getList().add(i);
val /= i;// 记得每次进行除i
} else
i++;// 因为求的是最小因子,所以只有在i小的时候不能除尽,才进行i加一的操作,
}
return stack;
}
}
class Stack {
List list = new ArrayList();
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
String s = "{";
for (Integer i : list) {
s += i + ",";
}
return s.substring(0, s.length() - 1) + "}";
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MinimalElements {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个整数:");
int inner = input.nextInt();
Stack stack = getElements(inner);
System.out.println(inner + "的所有最小银子为:" + stack);
}
public static Stack getElements(int val) {
Stack stack = new Stack();
int i;
for (i = 2; i <= val;)// 注意是小于等于
{
if (val % i == 0) {
stack.getList().add(i);
val /= i;// 记得每次进行除i
} else
i++;// 因为求的是最小因子,所以只有在i小的时候不能除尽,才进行i加一的操作,
}
return stack;
}
}
class Stack {
List
public List
return list;
}
public void setList(List
this.list = list;
}
@Override
public String toString() {
String s = "{";
for (Integer i : list) {
s += i + ",";
}
return s.substring(0, s.length() - 1) + "}";
}
}
全部回答
- 1楼网友:千夜
- 2021-03-24 22:09
//如有问题可QQ联系我:1609299076
import java.util.Scanner;
import java.util.Stack;
public class MinimalElements {
private static Stack getElements(int val) {
Stack result = new Stack();
int prime = 2;
while (val > 1) {
while (!isPrime(prime)) {
prime++;
}
while (val % prime == 0) {
result.push(prime);
val = val / prime;
}
prime++;
}
return result;
}
private static boolean isPrime(int num) {
boolean flag = true;
for(int i = 2; i < num; i++){
if(num % i == 0){
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
System.out.println("请输入要计算的整数: ");
Scanner scanner = new Scanner(System.in);
int toComputed = scanner.nextInt();
Stack elements = getElements(toComputed);
while (!elements.isEmpty()) {
System.out.println(elements.pop());
}
}
}
import java.util.Scanner;
import java.util.Stack;
public class MinimalElements {
private static Stack
Stack
int prime = 2;
while (val > 1) {
while (!isPrime(prime)) {
prime++;
}
while (val % prime == 0) {
result.push(prime);
val = val / prime;
}
prime++;
}
return result;
}
private static boolean isPrime(int num) {
boolean flag = true;
for(int i = 2; i < num; i++){
if(num % i == 0){
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
System.out.println("请输入要计算的整数: ");
Scanner scanner = new Scanner(System.in);
int toComputed = scanner.nextInt();
Stack
while (!elements.isEmpty()) {
System.out.println(elements.pop());
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯