重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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 + "是素数!");
}
}
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) + " ");
}
}
结果:
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧