判断素数
public boolean isPrimeNumber(int num) {
if(num == 2) {
return true;
} else if(num < 2 || num % 2 == 0) {
return false;
} else {
for(int i = 3; i < Math.sqrt(num); i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
判断完数
一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3。
public static boolean isPerfectNumber(int num) {
// 计算数字的因子之和
int sum = 0;
for(int i = 1; i < num; i++) {
if(num % i == 0) {
sum = sum + i;
}
}
// 进行对比
if (sum == num) {
return true;
}else{
return false;
}
}
判断回文数
正读倒读一样,这个数字就是回文数。例如 12321。
public static boolean isLoopNumber(int num) {
// 正数
String str1 = num + "";
// String str1 = Integer.toString(num);
// String str1 = String.valueOf(num);
// 倒数
StringBuilder str2 = new StringBuilder(str1);
str2.reverse();
int count = 0;
for(int i = 0; i < str1.length(); i++) {
if(str1.charAt(i) != str2.charAt(i)) {
System.out.println(str1 + "is not a loopNumber");
break;
}else {
count++;
}
}
if(count == str1.length()) {
System.out.println(str1 + "is a loopNumber");
}
}
判断水仙花数
在数论中,水仙花数(Narcissistic number),也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number) ,用来描述一个N位非负整数,其各位数字的N次方和等于该数本身。
// 例如:153 = 1^3 + 5^3 + 3^3。
public static boolean isNarcissisticNumber(int value) {
int temp = value;
int digits = 0;
// 判断value有几位数,保存在digits
while (temp > 0) {
digits++;
temp /= 10;
}
// 判断value各位数字的digits次方和是否等于该数本身
temp = value;
int sum = 0;
while (temp > 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
return sum == value;
}
字符串倒序
方法一和方法二都是利用已知的索引进行倒序,它们的算法很类似。方法三利用了JAVA中已有API的倒序方法(StringBuffer中的reverse())在API帮助文档里是这么说的:将此字符序列用其反转形式取代。
import java.lang.StringBuffer;
public class API_String {
public static void main(String[] args) {
String str = "abcd";
// 字符串倒序方法一
char[] ch = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = ch.length - 1; i >= 0; i--) {
sb.append(ch[i]);
}
System.out.println(sb.toString());
// 字符串倒序方法二
StringBuffer sbb = new StringBuffer();
for (int i = str.length() - 1; i >= 0; i--) {
sbb.append(str.charAt(i));
}
System.out.println(sbb.toString());
// 字符串倒序方法三
System.out.println(new StringBuffer(str).reverse().toString());
}
}
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//用来检测中文的正则表达式
String reg1 = "[\u4e00-\u9fa5]";
int count1 = 0;
//用来检测字母的正则表达式
String reg2 = "[a-zA-Z]";
int count2 = 0;
//用于统计空格数
int count3 = 0;
//用于统计数字个数
String reg4 = "[0-9]";
int count4 = 0;
// 将字符串转换为字符数组
char[] charArr = str.toCharArray();
String[] strArr = new String[charArr.length];
for(int i=0;i<charArr.length;i++)
{
strArr[i] =String.valueOf(charArr[i]) ;
if(strArr[i].matches(reg1)) {
count1++;
}
if(strArr[i].matches(reg2)) {
count2++;
}
if(strArr[i].matches(" ")) {
count3++;
}
if(strArr[i].matches(reg4)) {
count4++;
}
}
System.out.println("汉字的个数:"+count1);
System.out.println("字母的个数:"+count2);
System.out.println("空格的个数:"+count3);
System.out.println("数字的个数:"+count4);
}
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。
public static int sum(int num, int value) {
int temp = 0, sum = 0;
for (int i = 1; i <= num; i++) {
temp = temp * 10 + value;
sum = sum + temp;
}
return sum;
}
二分查找
R[low…high]是当前的查找区间,首先确定中间位置mid = (low + high) / 2,将待查找关键字k与R[mid]比较,若相等查找成功,若R[mid]>k,则新的查找区间为R[low…mid-1],若R[mid]
// 二分查找递归实现
public static int binSearch(int srcArray[], int start, int end, int key) {
int mid = (end - start) / 2 + start;
if (srcArray[mid] == key) {
return mid;
}
if (start >= end) {
return -1;
} else if (key > srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, key);
} else if (key < srcArray[mid]) {
return binSearch(srcArray, start, mid - 1, key);
}
return -1;
}
// 二分查找普通循环实现
public static int binSearch(int srcArray[], int key) {
int mid = srcArray.length / 2;
if (key == srcArray[mid]) {
return mid;
}
int start = 0;
int end = srcArray.length - 1;
while (start <= end) {
mid = (end - start) / 2 + start;
if (key < srcArray[mid]) {
end = mid - 1;
} else if (key > srcArray[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
斐波那契数列
又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)
/**
* 返回斐波那契数第n个值,n从0开始
* 实现方式,基于递归实现
*/
public static int getFib(int n) {
if(n < 0) {
return -1;
}else if(n == 0) {
return 0;
}else if(n == 1 || n ==2) {
return 1;
}else {
return getFib(n - 1) + getFib(n - 2);
}
}
/**
* 返回斐波那契数第n个值,n从0开始
* 实现方式,基于变量实现
*/
public static int getFib2(int n) {
if(n < 0) {
return -1;
}else if(n == 0) {
return 0;
}else if (n == 1 || n == 2) {
return 1;
}else {
int c = 0, a = 1, b = 1;
for(int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
/**
* 返回斐波那契数第n个值,n从0开始
* 实现方式,基于数组实现
*/
public static int getFib3(int n) {
if(n < 0){
return -1;
}else if(n == 0){
return 0;
}else if (n == 1 || n == 2) {
return 1;
}else {
int[] fibAry = new int[n + 1];
fibAry[0] = 0;
fibAry[1] = fibAry[2] = 1;
for(int i = 3; i <= n; i++) {
fibAry[i] = fibAry[i - 1] + fibAry[i - 2];
}
return fibAry[n];
}
}
欧几里得算法
两个整数的最大公因数(gcd)是同时整除二者的最大整数。
public static long gcd(long m, long n) {
while(n != 0) {
long rem = m % n;
m = n;
n = rem;
}
return m;
}
文件拷贝
// 多字节拷贝
public void fileCopy(String source, String target) throws IOException {
try (InputStream in = new FileInputStream(source)) {
try (OutputStream out = new FileOutStream(target)) {
byte[] buffer = new byte[4096];
int bytesToRead;
while((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
}
}
}
// 单字节拷贝
public static void fileCopyNIO(String source, String target) throws IOException {
try (FileInputStream in = new FileInputStream(source)) {
try (FileOutputStream out = new FileOutputStream(target)) {
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(4096);
while(inChannel.read(buffer) != -1) {
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
}
}
}
输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数。
/**
* 统计给定文件中给定字符串的出现次数
*
* @param filename 文件名
* @param word 字符串
* @return 字符串在文件中出现的次数
*/
public static int countWordInFile(String filename, String word) {
int counter = 0;
try (FileReader fr = new FileReader(filename)) {
try (BufferedReader br = new BufferedReader(fr)) {
String line = null;
while ((line = br.readLine()) != null) {
int index = -1;
while (line.length() >= word.length() && (index = line.indexOf(word)) >= 0) {
counter++;
line = line.substring(index + word.length());
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return counter;
}