1、 如何判断一个字符串中某个字符出现的次数?
(1) for循环
public static void main(String[] args) { String str = "ABC123ABC"; char searchChar = 'B'; int count = 0; char[] charArray = str.toCharArray(); for (char item : charArray) { if (item == searchChar) { count++; } } System.out.println("字符" + searchChar + "出现的次数为:" + count); }
(2) replace() string.length()
(3) Java两个有序数组合并为一个有序数组
public class MyClass { static void merge(int[] a, int[] b) { int l = a.length + b.length; int[] c = new int[l]; int i = 0; int j = 0; int x = 0; while ((i < a.length) || (j < b.length)) { if ((i == a.length) && (j < b.length)) { c[x++] = b[j++]; } else if ((i < a.length) && (j == b.length)) { c[x++] = a[i++]; } else if (a[i] <= b[j]) { c[x++] = a[i++]; } else if (a[i] > b[j]) { c[x++] = b[j++]; } } for (int d : c) { System.out.print("\t" + d); } } public static void main(String[] args) throws java.lang.Exception { int[] a = { 1, 3, 5, 6, 15 }; int[] b = { 1, 3, 4, 5, 7, 9 }; merge(a, b); } }
(5) 1000以内的质数并且个位十位百位加起来等于偶数
public class MyClass { static void find(int i) { int t=0; for(int k=1;k<=1000;k++){ if(i%k!=0) t++; if(t==998){ int j=i; int result=0; int sum=0; do{ result=i%10; sum+=result; i=i/10; } while(i!=0); if(sum%2==0){ System.out.print(j+"\t"); } } }} public static void main (String[] args) throws java.lang.Exception{ for(int i=0;i<=1000;i++) { if(i==1) continue; find(i); } } }
(6) 打印杨辉三角
public class MyClass { public static void main(String args[]) { int[][] a = new int[10][10]; for (int i = 0; i <10 ; i++) { a[i][0]=1; } for (int i = 1; i <10 ; i++) { for (int j = 1; j <10 ; j++) { a[i][j] = a[i-1][j-1] + a[i-1][j]; } } for (int i = 0; i <10 ; i++) { for (int j = 0; j <=i ; j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } } }
(7) 第一个人10岁,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大
public class MyClass { static int findAge(int i) { int age = 0; if (i == 1) { age = 10; } else { age = findAge(i - 1) + 2; } return age; } public static void main(String[] args) throws java.lang.Exception { System.out.print(findAge(8)); } }
(8) 冒泡排序
public class MyClass { public static void main(String[] args) { int[] a = { 1, 5, 9, 7, 8, 6, 1, 3, 2, 4, 8 }; for (int i = 0; i < (a.length - 1); i++) { for (int j = 0; j < (a.length - 1 - i); j++) { if (a[j] > a[j + 1]) { int temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } for (int x = 0; x < a.length; x++) { System.out.print(a[x]+"\t"); } } }
(9) 快速排序
public class MyClass { static void f(int[] a, int start, int end) { if (start < end) { int left = start; int right = end; int temp = a[start]; while (left < right) { while ((left < right) && (a[right] > temp)) { right--; } if (left < right) { a[left] = a[right]; left++; } while ((left < right) && (a[left] <= temp)) { left++; } a[right] = a[left]; } a[left] = temp; f(a, start, left); f(a, right + 1, end); } } public static void main(String[] args) throws java.lang.Exception { int[] a = { 12, 64, 58, 79, 310, 21, 98, 416, 351, 54 }; f(a, 0, a.length - 1); for (int d : a) { System.out.print("\t" + d); } } }
(10) 递归二分查找
public class MyClass { static int find(int[] i, int key, int low, int high) { int middle = (low + high) / 2; if (i[middle] > key) { return find(i, key, low, middle - 1); } else if (key > i[middle]) { return find(i, key, middle + 1, high); } else { return middle; } } public static void main(String[] args) throws java.lang.Exception { int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20 }; System.out.println(find(a, 4, 0, a.length)); } }
(11) 十进制转二进制,并且计算1有多少位
public class Main { static void find(int x) { int result = 0; String sum = ""; do { result = x % 2; sum = result + sum; x = x / 2; } while (x != 0); System.out.println(sum); String sum1 = sum.replaceAll("1", ""); System.out.println("1的位数:" + (sum.length() - sum1.length())); } public static void main(String[] args) { find(9); } }
(12) 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法
public class Main { static int JumpFloor(int target) { if (target == 1) { return 1; } if (target == 2) { return 2; } int first = 1; int second = 2; int sum = 0; for (int i = 2; i < target; i++) { sum = first + second; first = second; second = sum; } return sum; } public static void main(String[] args) { System.out.println(JumpFloor(5)); } }
(13)版本号比较
import java.io.*; class test { public static void main (String[] args) throws java.lang.Exception { String a="1.20.1.3"; String b="1.20.10.3"; String[] c=a.split("\\."); String[] d=b.split("\\."); if(c.length>d.length){ for(int i=0;i<d.length;i++){ if(Integer.parseInt(c[i])>Integer.parseInt(d[i])){ System.out.print("最新:"+a); break; }else{ System.out.print("最新:"+b); break; } } }else{ for(int i=0;i<c.length;i++){ if(Integer.parseInt(c[i])>Integer.parseInt(d[i])){ System.out.print("最新:"+a); break; }else{ System.out.print("最新:"+b); break; } } } } }
拓展:
一、正反三角
import java.io.*; class test { public static void main (String[] args) throws java.lang.Exception { int n=6; for(int i=1;i<=n;i++){ for(int j=n; i<=j; j--) System.out.print(" "); for(int j=1; j<=i; j++) System.out.print(i); System.out.println(); } for(int i=n-1;i>=1;i--){ for(int j=n; i<=j; j--) System.out.print(" "); for(int j=1; j<=i; j++) System.out.print(i); System.out.println(); } } }
二、棋盘矩形问题
import java.io.*; class test { public static void main (String[] args) throws java.lang.Exception { //规模 长 宽 int n=1; int m=3; //矩形个数: int rectangle=(m*(m+1)/2)*(n*(n+1)/2); //正方形个数 if(n<m) { //这里交换顺序,以便后面的for遍历最小 int temp=n; n=m; m=temp; } int square=m*n; for(int i=1;i<=m;i++) square+=(m-i)*(n-i); System.out.println("矩形个数:"+rectangle); System.out.print("正方形个数:"+square+","+"长方形个数:"+(rectangle-square)); } }
三、未完待续