Share a few algorithms, the classics may be a little bit, can only be considered a little bit of the beginner algorithm
Statement: Each problem thinking may be more than one, there may be many kinds, specific details please own Baidu, Thank you!
1. Exchange two numbers (try to use a variety of methods)
Analysis
- SWAP1, the most common use of a temp variable to exchange
- SWAP2, the sum of two numbers and then minus the value of B, then the remaining value is a, at which point the value is assigned to B, so the value is exchanged
- SWAP3, a number or the same number two times or the number
/** * @author Harry * 1.交换两个数(尽量用多种方法) */public class day01 { public static void swap1(int a,int b) { int temp = a; a = b; b = temp; System.out.println(a+","+b); } public static void swap2(int a,int b) { a = a + b; b = a - b; a = a - b; System.out.println(a + "," + b); } public static void swap3(int a,int b) { a = a^b; b = a^b; a = a^b; System.out.println(a + "," + b); } public static void main(String[] args) { swap1(1, 2); swap2(1, 2); swap3(1, 2); }}
2. Yang Hui Triangle
Analysis: There are many ways of thinking, the corresponding implementation code also has a lot of specific Baidu Bar, here only to show one! It is observed that the first and last values of each row are 1, and the remaining number is the number of the previous row and the first number of the previous row of the column.
/** * @author Harry * 2.杨辉三角 */public class day02 { public static void main(String[] args) { int MMax = 10; //打印行数 int arr[][] = new int[MMax+1][MMax+1]; //定义一个数组来装所有数 for (int i = 0; i < arr.length; i++) { for(int j=0;j<=i;j++) { if(j==0 || j==i ) { //将第一个和最后一个都赋值为1 arr[i][j] = 1; }else { //将上一行的本列数与上一行的本列前一个数相加得到此处的数字 arr[i][j] = arr[i-1][j] + arr[i-1][j-1]; } } } for (int i = 0; i < arr.length; i++) { for(int j=0;j<=i;j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } }}
3. Fibonacci Sequence
Analysis: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610 ... it is obvious that from the third number, the value of each number is the first two number of the and, the direct recursive solution, as for the students who do not understand recursion, more to see the principle of recursion, I am not much Said it.
/** * * @author Harry * 3.著名的菲波拉契(Fibonacci)数列 */public class day03 { public static int f(int n) { if(n==0) return 0; if(n==1||n==2) return 1; else return f(n-1) + f(n-2); } public static void main(String[] args) { int x = 20; for(int i=0; i<20; i++) System.out.println(f(i)); }}
4. Judging a palindrome string
Analysis: For example ABCDBCA and ABCDDBCA all belong to Palindrome string, the idea is simple, from both ends of the comparison is the same, as long as there is a different return false, the same return true
/** * @author Harry * 4.回文字符串 */public class day04 { public static void f(char[] ch) { for(int i=0;i<ch.length;i++) { if(ch[i] != ch[ch.length-i-1]) { System.out.println("不是回文字符串"); return; } } System.out.println("是回文字符串"); } public static void main(String[] args) { String str = "ABCDCBAA"; char[] ch = str.toCharArray(); f(ch); }}
5. Use Java to calculate 9223372036854775807 + 1 values
Analysis: Individuals use the simplest method to calculate this number, directly using a large integer type BigInteger
import java.math.BigInteger;/** * @author Harry * 5.使用Java 计算 9223372036854775807 + 1的值 */public class day05 { public static void main(String[] args) { BigInteger b = new BigInteger("9223372036854775807"); System.out.println(b.add(new BigInteger("1"))); }}
6. Angle Valley theorem
Angular Valley theorem: A natural number, if it is even, divide it by 2, and if it is odd, multiply it by 3 plus 1. After such a finite operation, the natural value 1 can be obtained. How many times can you get the natural number 1.
/** * @author Harry * 角谷定理,输入一个自然数,若为偶数,则把它除以2,若为奇数,则把它乘以* 3加1. */public class day06_02 { public static void main(String[] args) { int a = 3; int count = 0; while(a != 1) { if(a % 2 == 0) a /= 2; else a = a * 3 + 1; count++; } System.out.println(count + "次"); }}
7. Converting decimal into binary
Analysis: Convert decimal to Binary, two methods. (Hint: 1. Using the JDK to bring the method 2. Using the Every-binary method)
/** * @author Harry * 3.将十进制转换为二进制,两种方法。 * (提示:1.使用jdk自带方法 2.使用逢二进一方法) */public class day7 { public static void convert1(int a) { String str = Integer.toBinaryString(a); System.out.println(str); } public static void convert2(int a) { int n; String str = ""; while(a != 0) { n = a%2; a /= 2; str = n + str; str.concat(str); } System.out.println(str); } public static void main(String[] args) { int a = 100; convert1(a); convert2(a); }}
If there is the wrong place also please remind me, let me correct, lest fraught, if you have a better topic, also please comments in the comment area, will consider adding in the article!
Primary algorithm problem (code for Java)