题目
- JAVA49 判断素数个数
- 描述
- 输入描述:
- 输出描述:
- 示例:
- 分析:
- 代码:
- 大佬代码:
- JAVA50 根据周长求面积
- 描述
- 输入描述:
- 输出描述:
- 示例:
- 分析:
- 代码:
- 大佬代码:
JAVA49 判断素数个数
描述
输入两个正整数,输出这两个正整数之间(左闭右闭,即判断包括这两个整数在内有多少素数)有多少个大于2的素数。如果start>end,则将start设为end,end设为start
输入描述:
两个正整数
输出描述:
start到end之间有count个大于2的素数
示例:
输入:1 100 输出:1到100之间有24个大于2的素数
输入:100 1 输出:1到100之间有24个大于2的素数
分析:
1.设置双层for循环。
2.第一层:表示从start — end的数字。
3.第二层:表示计算素数的for循环。
代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int start = scanner.nextInt();int end = scanner.nextInt();method(start,end);}public static void method(int start, int end) {//write your code here......int count=0;int temp;if(start>end){temp=start;start=end;end=temp;}int i,j;for( i=start;i<=end;i++){if(i==1){continue;}for (j = 2; j <i; j++) {if(i%j==0){break;}}if(j==i){//System.out.print(i+" ");if(i>2){count++;}}}//System.out.println();System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数");}
}
大佬代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int start = scanner.nextInt();int end = scanner.nextInt();method(start,end);}public static boolean isPeime(int num){for(int i = 2; i * i <= num; i++){ //从2遍历到根号numif(num % i == 0) //一旦可以整除就不是素数return false;}return true;}public static void method(int start,int end){int count=0;if(start > end){ //如果start更大,则交换int temp = start;start = end;end = temp;}for(int i = start; i <= end; i++){ //遍历start到endif(i <= 2) //不大于2的不要continue;if(isPeime(i)) //判断这个数是否是素数count++; //返回true,素数+1}System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); //输出}
}
JAVA50 根据周长求面积
描述
已知:
图形类Shape,该类中定义了图形的周长属性,以及返回周长的方法。
Area接口,该接口中定义了返回面积的方法getArea()。
要求:
定义圆形类Circle,使其继承于Shape,并实现Area接口。
定义方形类Square,使其继承于Shape,并实现Area接口。
注意:
圆周率要使用Math类中的常量。
输入描述:
周长
输出描述:
面积(计算时请使用Math类中的常量,面积为double类型,保留三位小数,四舍五入部分预设代码已经完成)
示例:
输入:4 输出:1.2731.000
分析:
1.根据周长就面积。
2.正方形:周长:4r ------->面积:pow(r,2);
3.圆形:周长:2*PI*r ------->面积:PI*pow(r,2);
代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNextDouble()) {double s = scanner.nextDouble();// Circle和Square是需要你定义的类System.out.println(String.format("%.3f",new Circle(s).getArea()));System.out.println(String.format("%.3f", new Square(s).getArea()));}}}class Shape {private double s; // 周长public Shape(double s) {this.s = s;}public double getS() {return s;}}interface Area {double getArea(); // 面积
}// 圆形
class Circle extends Shape implements Area {public Circle(double s) {super(s);}@Overridepublic double getArea() {return Math.pow(super.getS()/2,2)/Math.PI;}//write your code here......}// 方形
class Square extends Shape implements Area {public Square(double s) {super(s);}@Overridepublic double getArea() {return Math.pow(super.getS()/4,2);}//write your code here......}
大佬代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNextDouble()) {double s = scanner.nextDouble();// Circle和Square是需要你定义的类System.out.println(String.format("%.3f",new Circle(s).getArea()));System.out.println(String.format("%.3f", new Square(s).getArea()));}}}class Shape {private double s; // 周长public Shape(double s) {this.s = s;}public double getS() {return s;}}interface Area {double getArea(); // 面积
}// 圆形
class Circle extends Shape implements Area {//write your code here......public Circle(double s) {super(s);}@Overridepublic double getArea() {return Math.PI * Math.pow((getS() / (2 * Math.PI)), 2);}
}// 方形
class Square extends Shape implements Area {//write your code here......public Square(double s) {super(s);}@Overridepublic double getArea() {return Math.pow((getS() / 4), 2);}}