Java入门练习题及其答案第一弹
文章目录
- Java入门练习题及其答案第一弹
- 素数打印
- 乘法口诀表
- 最大公约数
- 水仙花数
- 二进制中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 + "是素数!");
}
}
-
n -> a*b
2 – n/2
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 + "是素数!");
}
}
-
一定有一个数字小于等于根号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 – 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间所有水仙花数(三位自逆数)
思路:
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);
}
}
更高效的思路
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) + " ");
}
}
结果: