Java入门练习题及其答案第一弹-创新互联

Java入门练习题及其答案第一弹

创新互联建站专业为企业提供银州网站建设、银州做网站、银州网站设计、银州网站制作等企业网站建设、网页设计与制作、银州企业网站模板建站服务,10年银州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。文章目录
  • Java入门练习题及其答案第一弹
    • 素数打印
    • 乘法口诀表
    • 大公约数
    • 水仙花数
    • 二进制中1的个数
    • 二进制奇偶数位

素数打印
  1. 只能被1和自己整除
import java.util.Scanner;

public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
    //这里通过if去判断1 或者是负数的情况
        int i = 2;
        for (; i< n ; i++) {if(n % i == 0){//System.out.println(i + "不是素数!");
                break;
            }
        }
        if(i == n) {System.out.println(n + "是素数!");
        }
    }
  1. n ->a*b

    2 – n/2

image-20221223212025213

public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 2;
        for (; i<= n/2 ; i++) {if(n % i == 0){break;
            }
        }
        if(i >n/2) {System.out.println(n + "是素数!");
        }
    }
  1. 一定有一个数字小于等于根号n

    public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int i = 2;
            for (; i<= Math.sqrt(n) ; i++) {if(n % i == 0){break;
                }
            }
            if(i >Math.sqrt(n)) {System.out.println(n + "是素数!");
            }
        }
    1. 打印1 – n间所有素数
import java.util.Scanner;
public class Measure {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int j = 1; j<= n; j++) {int i = 2;
            for (; i<= Math.sqrt(j); i++) {if (j % i == 0) {break;
                }
            }
            if (i >Math.sqrt(j)) {System.out.println(j);
            }
        }
    }
}
乘法口诀表
import java.util.Scanner;
public class Measure {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i<= n; i++) {for (int j = 1; j<= i; j++) {System.out.print(i + "*" + j + "=" + i*j + " ");
            }
            System.out.println();
        }
    }
}
大公约数

辗转相除法:除到余数为0

import java.util.Scanner;
public class Measure {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = a % b;
        while(c != 0) {a = b;
            b = c;
            c = a % b;
        }
        System.out.println(b);
    }
}
水仙花数

0~999间所有水仙花数(三位自逆数)

思路:image-20221224221702948

import java.util.Scanner;
public class Measure {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//999999
        for (int i = 0; i< n; i++) {//判断i是几位数 求得每个位上是几 加起来== 原来的数字
            int count = 0;
            int tmp = i;
            while(tmp != 0) {count++;
                tmp = tmp / 10;
            }
            //tmp=0  i不变
            tmp = i;
            int sum = 0;
            while(tmp != 0) {sum += Math.pow(tmp % 10,count);
                tmp /= 10;
            }
            if(sum == i) {System.out.println(i);
            }
        }
    }
}
二进制中1的个数
import java.util.Scanner;
public class Measure {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) != 0) {count++;
            }
        }
        System.out.println(count);
    }
}

另解

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

更高效的思路

image-20221224224604443

import java.util.Scanner;
public class Measure {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);
    }
}
二进制奇偶数位
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //偶数位
        for (int i = 31; i >= 1; i -= 2) {System.out.print(((n >>i) & 1) + " ");
        }
        System.out.println();
        for (int i = 30; i >= 0 ; i -= 2) {System.out.print(((n >>i) & 1) + " ");
        }
    }

结果:image-20221224231133281

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享标题:Java入门练习题及其答案第一弹-创新互联
转载来源:http://azwzsj.com/article/deohdd.html