public class TestDemo220427 { public static void main(String[] args) { // 这里以求取1~100之间的素数为例 for(int i = 2;i <= 100;i++){//素数从2开始,所以从2开始产生到100的数 int flg = 1;//假设是素数 for(int j = 2;j < i;j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素数!"); } } } }
public class TestDemo220427 { public static void main(String[] args) { // 这里以求取1~100之间的素数为例 for(int i = 2;i <= 100;i++){//素数从2开始,所以从2开始产生到100的数 int flg = 1;//假设是素数 for(int j = 2;j < i/2;j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素数!"); } } } }
import java.lang.Math; public class TestDemo220427 { public static void main(String[] args) { // 这里以求取1~100之间的素数为例 for(int i = 2;i <= 100;i++){//素数从2开始,所以从2开始产生到100的数 int flg = 1;//假设是素数 for(int j = 2;j <= (int)(Math.sqrt(i));j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素数!"); } } } }
import java.lang.Math; public class TestDemo220427 { public static void main(String[] args) { // 这里以求取1~100之间的素数为例 for(int i = 1;i <= 100;i += 2){//从1开始,产生到100的奇数 int flg = 1;//假设是素数 if(i == 1){ System.out.println((i+1) + "是素数!");//2这里需要单拎出来考虑,比较特殊 continue; } for(int j = 2;j <= (int)(Math.sqrt(i));j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素数!"); } } } }
public class TestDemo220427 { public static boolean isleapYear(int year){ if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ return true; } else{ return false; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入年份:"); int year = scan.nextInt(); boolean ret = isleapYear(year); if(ret == true){ System.out.println(year + "是闰年!"); }else{ System.out.println(year + "不是闰年!"); } } }
import java.util.Scanner; public class TestDemo220427 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int m = 0; while((m = a%b) != 0){//辗转相除法 a = b; b = m; } System.out.println(b); } }
import java.util.Scanner; public class TestDemo220427 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); for(int i = 1;i > 0;i++){ if((a*i)%b == 0){ System.out.println("最小公倍数:" + a*i); break; } } } }
import java.lang.Math; public class TestDemo220427 { public static boolean isNarnum(int num,int count){ int sum = 0; int tmp = num; while(tmp != 0){ sum += Math.pow(tmp%10,count); tmp /= 10; } if(sum == num){ return true; }else{ return false; } } public static void main(String[] args) { // 判断一个数是不是自幂数 Scanner scan = new Scanner(System.in); System.out.println("请输入一个数:"); int num = scan.nextInt(); int count = 0; int tmp = num; while(tmp != 0){ count++; tmp /= 10; } boolean ret = isNarnum(num,count); if(ret == true){ System.out.println(num + "是一个" + count +"位自幂数!"); }else{ System.out.println(num + "不是自幂数!"); } } }
import java.util.Scanner; public class TestDemo220427 { public static int getOnecount(int num){ int count = 0; while(num != 0){//右移后不为0就继续统计 if((num& 1) == 1){ count++; } num = num >> 1; } return count; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个数:"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二进制位中1的个数 :" + ret); } }
public class TestDemo220427 { public static int getOnecount(int num){ int count = 0; for(int i = 0;i < 32;i++){ if((num & (1 << i)) != 0){ count++; } } return count; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个数:"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二进制位中1的个数 :" + ret); } }
import java.util.Scanner; public class TestDemo220427 { public static int getOnecount(int num){ int count = 0; while(num != 0){ num = num&(num-1); count++; } return count; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个数:"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二进制位中1的个数 :" + ret); }
import java.util.Scanner; public class TestDemo220427 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个数:"); int num = scan.nextInt(); if((num & (num-1)) == 0){ System.out.println("是2的k次方数!"); }else{ System.out.println("不是2的k次方数!"); } } }
Das obige ist der detaillierte Inhalt vonBeispielanalyse der Java-Syntax. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!