文章目录

  • 一. 前言
  • 二. 正文
  • 1. 计算二进制中1的个数
  • 2. 求水仙花
  • 3. 求斐波那契数列的第n项。
  • 3.1 递归
  • 3.2 迭代
  • 4. 输出1234各位之和(递归实现)
  • 5. 汉诺塔问题
  • 6. 只出现一次的数字
  • 7. 多数元素
  • 8. 存在连续三个奇数的数组
  • 9. 验证回文串
  • 三. 错题积累
  • 3.1 System 不是关键字
  • 3.2 java程序中永远不会执行的语句在程序中会报错
  • 3.3 main 不是关键字
  • 3.4 static修饰类的变量或者类的方法,不能修饰方法的局部变量
  • 3.5 import不能导入一个具体的包,只能导入一个具体的类
  • 3.6 this 只能在成员方法中使用,表示当前对象的引用
  • 四. 持续更新中........


一. 前言

对做的java题算法改进,每道题先列出第一次的算法,然后列出改进的代码

二. 正文

1. 计算二进制中1的个数

public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();
       int count = 0;
        for (int i = 0; i < 32; i++) {
            if(((n>>>i) & 1) == 1) {
                count++;
            }
        }
        System.out.println(count);
}
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int count = 0;
    while (n != 0) {//如果移动的过程当中是0了,就结束循环
        if((n & 1) != 0) {
            count++;
        }
        n = n >>> 1;//注意这里要用无符号右移
    }
    System.out.println(count);

}
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int count = 0;
    while (n != 0) {
        n = n & (n-1);
        count++;
    }
    System.out.println(count);
}

2. 求水仙花

求出0~n之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本 身,如;153=13+53+3^3,则153是一个“水仙花数“。)

public static void main(String[] args) {
    System.out.println("输入一个三位数,求0到该数的水仙花数");
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    for (int i = 100; i <= a ; i++) {
        int hund = i / 100;
        int decade = i /10 % 10;
        int unit = i % 10;
        if (hund * hund *hund + decade * decade * decade + unit * unit * unit == i){
            System.out.print(i + "  ");
        }
    }
}

思维打开:

public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int n = scanner.nextInt();
        for (int i = 0; i <= n ; i++) {
            int count = 1;
            int tmp = i;
            int sum = 0;
            //判断i是否为水仙花数
            //1. 求判断数字的位数
            while(tmp/10 != 0)
            {
                count++;
                tmp = tmp/10;
            }
            //2. 计算每一位的次方和
            tmp = i;
            while(tmp != 0)
            {
                sum += Math.pow(tmp%10, count);
                //这里写成sum = sum + Math.pow(tmp%10, count);
                //会报错,因为pow函数返回值类型为double
                tmp = tmp/10;
            }
            //3. 判断
            if(sum == i)
                System.out.println(i);
        }
}

3. 求斐波那契数列的第n项。

3.1 递归

public static Test{
		 public static int Fib(int n){
        if (n == 1 || n == 2){
            return 1;
        }else{
            return Fib(n - 1) + Fib(n - 2);
        }
    }
    public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    while (scan.hasNextInt()){
        int n = scan.nextInt();
        System.out.println(Fib(n));
    		}
    	}
	}
}

3.2 迭代

public static int Fib1(int n){
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <=n ; i++){
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f2;
    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextInt()){
            int n = scan.nextInt();
            System.out.println(Fib1(n));
        }
    }

4. 输出1234各位之和(递归实现)

一开始解题时设了一个sum变量来储存相加的和,但是达不到题目的效果,看到不设sum变量也可以求出解感觉自己就是个傻逼

public static int print(int n){
        if (n / 10 > 0){
            return n % 10 + print(n / 10);  //这里的思路
        }else{
            return n;
        }
    }
    public static void main(String[] args){
        System.out.println(print(1234));
    }

5. 汉诺塔问题

java纠错试题 java程序改错题技巧_开发语言

import java.util.Scanner;
public class Test {
    //汉诺塔问题
    public static void hanio(int n, char pose1, char pose2, char pose3){
        //n为盘子个数
        //pose1 为起始位置
        //pose2 为转接位置
        //pose3 为目标位置
        if (n == 1){
            move(pose1, pose3);
            return;
        }
        hanio(n - 1, pose1, pose3, pose2); //将n - 1个盘子从pose1通过pose3移动到pose2上
        move(pose1, pose3);   //将pose1上面剩下的盘子(最大)移动到pose3上
        hanio(n - 1, pose2, pose1, pose3);//将pose2上面的n-1个盘子通过pose1移动到pose上
    }
    public static void move(char pose1, char pose2){
        //pose1 为起始位置
        //pose2 为目标位置
        System.out.print(pose1 + "---->" + pose2 + " ");
    }
    public static void main(String[] args){
        hanio(1, 'A', 'B', 'C');
        System.out.println();
        hanio(2, 'A', 'B', 'C');
        System.out.println();
        hanio(3, 'A', 'B', 'C');
    }
}

6. 只出现一次的数字

//给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
public static void main(String[] args) {
    int[] array = {4, 1, 2, 1, 2};
    for (int i = 0; i < array.length; i++){
        int flag = 0;
        for(int j = i + 1; j < array.length; j++){
            if (array[i] == array[j]){
                flag = 1;
                break;
            }
        }
        if (flag == 0){
            System.out.println(array[i]);
            break;
        }
    }
}

用异或:

//增加难度,一个单身狗升华成两个单身狗,算法升级为使用异或
import java.util.Arrays;
class Fun{
    //两个单身狗
    int[] array = {2, 3, 4, 5, 3, 2};
    public void fun1(){
        int ret = 0;
        for(int i = 0; i < array.length; i++){
            ret  ^= array[i];
        }
        if (Arrays.binarySearch(array, ret) < 0){
            int count = 0;
            for (int i = 0; i < 31; i++){
                if (((ret >> i) & 1) != 1)
                    count++;
                else break;
            }
            int x1 = 0;
            int x2 = 0;
            for(int i = 0; i < array.length; i++){
                if (((array[i] >> count) & 1) == 1){
                    x1 ^= array[i];
                }else{
                    x2 ^= array[i];
                }
            }
            System.out.println(x1 + "   " + x2);
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Fun fun = new Fun();
        fun.fun1();
    }
}

7. 多数元素

给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素

//给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素
public static void main(String[] args) {
    int[] array = {2, 2, 1, 1, 1, 2, 2};
    for(int i = 0; i < array.length; i++){
        int count = 1;
        for(int j = i + 1; j < array.length; j++){
            if (array[i] == array[j]){
                count++;
            }
        }
        if (count > (array.length / 2)){
            System.out.println(array[i]);
            break;
        }
    }
}

投票法:

public int fun2(){
        int array[] = {1, 3, 4, 5, 4, 4, 4, 4};
        int ret = array[0];
        int count = 1;
        for (int i = 1; i < array.length; i++){
            if (ret == array[i]){
                count++;
            }else{
                count--;
            }
            if (count == 0){
                ret = array[i + 1];
            }
        }
        return ret;
    }

8. 存在连续三个奇数的数组

给你一个整数数组 arr,请你判断数组中是否存在连续三个元素都是奇数的情况:如果存在,请返回 true ;否则,返回 false 。

//存在连续三个奇数的数组
    public boolean fun3(){
        int[] array = {2, 4, 7, 5, 3, 9};
        int count = 0;
        for(int i = 0; i < array.length; i++){
            if(array[i] % 2 != 0) {
                count++;
                if (count == 3)
                    return true;
            }else{
                count = 0;
            }
        }
        return false;
    }

9. 验证回文串

如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。

字母和数字都属于字母数字字符。

给你一个字符串 s,如果它是 回文串 ,返回 true ;否则,返回 false 。

来源:力扣(LeetCode)

链接:https://leetcode.cn/problems/valid-palindrome
1. Character.isLetter() 是否是一个字母
 2. Characrer.isDigit() 是否是一个数字字符
class Solution {
    public boolean effective(char a){
        if (Character.isLetter(a) || Character.isDigit(a)){
            return true;
        }
        return false;
    }
    public boolean isPalindrome(String s) {
       s = s.toUpperCase(); 
       int left = 0;
       int right = s.length() - 1;
       while(left < right){
           while(left < right && !effective(s.charAt(left))){
               left++;
           }
           while(left < right && !effective(s.charAt(right)))
                right--;
            if(s.charAt(left) != s.charAt(right)){
                return false;
            }
            left++;
            right--;
       }
        return true;
    }   
}

java纠错试题 java程序改错题技巧_算法_02

三. 错题积累

3.1 System 不是关键字

3.2 java程序中永远不会执行的语句在程序中会报错

3.3 main 不是关键字

3.4 static修饰类的变量或者类的方法,不能修饰方法的局部变量

在方法当中定义的变量是局部变量,而静态的变量属于类变量。随着类的加载而被创建,而局部变量是调用该方法的时候,才创建的。

所以,此时两种变量的性质是冲突的。Java当中不允许定义局部的静态变量。

3.5 import不能导入一个具体的包,只能导入一个具体的类

3.6 this 只能在成员方法中使用,表示当前对象的引用

四. 持续更新中…