JAVA分解质因子
/*题目
题目内容:
每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数。比如,6可以被分解为2x3,而24可以被分解为2x2x2x3。
现在,你的程序要读入一个[2,100000]范围内的整数,然后输出它的质因数分解式;当读到的就是素数时,输出它本身。
输入格式:
一个整数,范围在[2,100000]内。
输出格式:
形如:
n=axbxcxd
或
n=n
所有的符号之间都没有空格,x是小写字母x。
输入样例:
18
输出样例:
18=2x3x3
*/
import java.util.Scanner;
public class Main {
public static boolean IsPrime(int a)
{
boolean t=true;
for (int i =2;i<a;i++)
{
if(a%i==0)
{
t=false;//就不是素数了
break;
}
}
return t;
}
public static String myMethod(int a)
{
String Str="";
int count=1;
if(IsPrime(a))//如果输入的数字a是素数,就直接输出结果
{
Str=a+"="+a;
}
else//否则,继续
{
while(!IsPrime(a))//如果当前a的值不是素数就继续循环
{
for(int j=2;j<a;j++)
{
if(a%j==0&&count==1)
{
Str=a+"="+j+"";
a/=j;
count++;
break;//为了 a继续从2开始取余
}
else if(a%j==0&&count!=1)
{
Str=Str+"x"+j;
a/=j;
break;//为了 a继续从2开始取余
}
}
}
Str=Str+"x"+a;//如果A是素数就最后加上a
}
return Str;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
String result=myMethod(number);
System.out.print(result);
}
}
JAVA分解质因子的更多相关文章
- UVA 10780 Again Prime? No Time. 分解质因子
The problem statement is very easy. Given a number n you have to determine the largest power of m,no ...
- HDU 4497 GCD and LCM(分解质因子+排列组合)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...
- hdu6237 分解质因子
题意:给一堆石子,每次移动一颗到另一堆,要求最小次数使得,所有石子数gcd>1 题解:枚举所有质因子,然后找次数最小的那一个,统计次数时,我们可以事先记录下每堆石子余质因子 的和,对所有石子取余 ...
- NYOJ-476谁是英雄,分解质因子求约数个数!
谁是英雄 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 十个数学家(编号0-9)乘气球飞行在太平洋上空.当横越赤道时,他们决定庆祝一下这一壮举.于是他们开了一瓶香槟.不 ...
- Codeforces Round #828 (Div. 3) E2. Divisible Numbers (分解质因子,dfs判断x,y)
题目链接 题目大意 给定a,b,c,d四个数,其中a<c,b<c,现在让你寻找一对数(x,y),满足一下条件: 1. a<x<c,b<y<d 2. (x*y)%(a ...
- HDU 4135 Co-prime (容斥+分解质因子)
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. ...
- Minimum Sum LCM UVA - 10791(分解质因子)
对于一个数n 设它有两个不是互质的因子a和b 即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...
- BNU 13259.Story of Tomisu Ghost 分解质因子
Story of Tomisu Ghost It is now 2150 AD and problem-setters are having a horrified time as the ghost ...
- HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版
题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ...
随机推荐
- 增强MyEclipse提示功能
当我们使用MyEclipse编辑代码的时候按住“Ctrl+/”,就能获得代码提示,或者使用“.”的时候就会“点”出属性或方法等,而且提示还会有延迟. 如果我们想无论是按下“.”.“Ctrl+/”甚至是 ...
- VUE+WebPack前端游戏设计:实现物体的拖拽动态特效
- 通过Scanner从控制台获取数据
----------siwuxie095 Scanner类用于扫描从控制台输入的数据,可以接收字符串和基本数据类型的数据 Scanner类位于 java.util.Scanner 包中 Scanner ...
- Java-随机数工具类
import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.la ...
- 253. Meeting Rooms II 需要多少间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- 256. Paint House房屋染色
[抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...
- [C++] c language 23 keywords
c language keywords
- [SoapUI] 如何让某个步骤的Assertion失败之后继续执行后面的步骤
To continue tests executing after failed test step you need disable "Abort on error" optio ...
- tomcat启动报错:java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\activiti-explorer does not exist or is not a readable directory
java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\erp does not exist ...
- spring mvc---controller返回值
1.ModelAndView 类似servlet,之前例子在用 addObject setViewName 2.String a.表示返回逻辑视图名 spring mvc的视图解析器中设置.(在web ...