前几天去北京参加了“蓝桥杯”比赛,结果拿了二等奖,虽然还没达到我的目标,但这也算是一个比较美好的结局吧。为了准备这次比赛,积累了一些算法,也许看上去都很基础,很简单。但我相信“千里之行,始于足下”,我也相信,能拿到二等奖,没有基础是不可能的。
以下是我积累到的一些算法
1,求最大公约数与最大公倍数:
------------------------------------------------------------
//------------------------------------------辗转相除法(非递归)
public static int gcd1(int a , int b){ //求最大公约数
int temp = 0 ;
if(a<b){
temp = a ;
a = b ;
b = temp ;
}
while(a%b!=0){
temp = b ;
b = a%b ;
a = temp ;
}
return b ;
}
public static int gcd2(int a , int b){ //求最小公倍数
int temp = 0 ;
int x = a ;
int y = b ;
if(a<b){
temp = a ;
a = b ;
b = temp ;
}
while(a%b!=0){
temp = b ;
b = a%b ;
a = temp ;
}
return y*x/b ;
}
}
2,Java中的四舍五入法:
-------------------------------------------------------------------------
package org.jian.acm;
import java.text.DecimalFormat;
public class Main {
private static DecimalFormat df1 = new DecimalFormat("0.00") ;//直接保留两位小数
private static DecimalFormat df2 = new DecimalFormat("#.00") ;//如果整数位为0,直接去掉
private static DecimalFormat df3 = new DecimalFormat("0.##") ;//如果保留小数后还有0,则去掉这些0
public static void main(String[] args) {
double result = 0.70443800 ;
System.out.println(result);
System.out.println(df1.format(result));
System.out.println(df2.format(result));
System.out.println(df3.format(result));
}
}
JDK官方文档上的DecimalFormat的"0"和"#"的意思。
上面运算结果:
-----------------------------------------------------------------
0.704438
0.70
.70
0.7
3,判断素数:
----------------------------------------------------------------------------------------------------------------
public static Boolean Prime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
4,大数阶乘:
-----------------------------------------------------------------------------------------------------------
package org.jian.acm;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");// 为result赋初始值,为1
for (int i = 1; i <= 100; i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);// 调用自乘方法
}
System.out.println(result);
}
}
-----------------------------------------------------------------------------------------------------结果:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941
463976156518286253697920827223758251185210916864000000000000000000000000
5,求出一个数字的各个位数:
package org.jian.acm;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
long n = Long.MAX_VALUE ;
int a[] = sumDigits(n) ;
System.out.println("n:"+n);
System.out.println("n的各个位数:"+Arrays.toString(a));
}
public static int[] sumDigits(long n){
long temp = n ;
int num = 0 ;
while(temp!=0){
temp/=10 ;
num++ ;
}
int a[] = new int[num] ;
int sum=0;
int i = 0 ;
while(n>0){
int m=(int)(n%10);
a[i] = m;
n=n/10;
i++ ;
}
return a;
}
}
运算结果:-------------------------------------------------------------------------
n:9223372036854775807
n的各个位数:[7, 0, 8, 5, 7, 7, 4, 5, 8, 6, 3, 0, 2, 7, 3, 3, 2, 2, 9]
6,String切割:
-------------------------------------------------------------------------------------------------------------------
package org.jian.acm;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String str1 = "asa.tfwerew.qweqd.xcx" ;
String str2 = "asa tfwerew qweqd xcx" ;
String str3 = "asa tfwerew qweqd xcx" ;
String str4 = "asa>tfwerew>qweqd>xcx" ;
String strs1[] = str1.split("\\.") ;
String strs2[] = str2.split("\\s+") ;//+号表示一个或多个的意思
String strs3[] = str3.split("\\s") ;//\\s表示 空格,回车,换行等空白符,
String strs4[] = str4.split("\\>") ;
System.out.println(Arrays.toString(strs1));
System.out.println(Arrays.toString(strs2));
System.out.println(Arrays.toString(strs3));
System.out.println(Arrays.toString(strs4));
}
}
输出结果:---------------------------------
[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]
~注:
但"a.b.c".split(".");得不到预期的结果: a b c
所以必须要 "a.b.c".split("\\."); 用\\才行
7,JDK本身的进制转换:
------------------------------------------------------------------
package org.jian.acm;
public class Main {
public static void main(String[] args) {
int a = 200 ;
System.out.println("二进制:"+Integer.toBinaryString(a));
System.out.println("八进制:"+Integer.toOctalString(a));
System.out.println("十六进制:"+Integer.toHexString(a));
System.out.println("把进制相反方向转回来:");
System.out.println(Integer.parseInt("11001000", 2)); //11001000二进制转为十进制
System.out.println(Integer.parseInt("310", 8)); //310八进制转为十进制
System.out.println(Integer.parseInt("c8", 16)); //C8十六进制转为十进制
}
}
运算结果:------
二进制:11001000
八进制:310
十六进制:c8
把进制相反方向转回来:
200
200
200
----------------------------------------
BigInteger的进制转换:
System.out.println(new BigInteger("2131232132132132131221321312")
.toString(2)); // 化为二进制
System.out.println(new BigInteger("FFFFFFFFFFFFFFF", 16)); //十六进制转二进制
结果:
1101110001011101001111101010110001011101100111110111000000001101100011011001011011001100000
1152921504606846975