判断素数个数、 根据周长求面积 题目

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遍历到根号num
            if(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到end
            if(i <= 2) //不大于2的不要
                continue;
            if(isPeime(i)) //判断这个数是否是素数
                count++; //返回true,素数+1
        }
        System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); //输出
    }
}

 
 
 

JAVA50 根据周长求面积

描述

已知:

  1. 图形类Shape,该类中定义了图形的周长属性,以及返回周长的方法。

  2. Area接口,该接口中定义了返回面积的方法getArea()。

要求:

  1. 定义圆形类Circle,使其继承于Shape,并实现Area接口。

  2. 定义方形类Square,使其继承于Shape,并实现Area接口。

注意:

圆周率要使用Math类中的常量。

输入描述:

周长

输出描述:

面积(计算时请使用Math类中的常量,面积为double类型,保留三位小数,四舍五入部分预设代码已经完成)

示例:

输入:4
输出:1.273
	 1.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);
    }

    @Override
    public 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);
    }

    @Override
    public 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);
    }
 
    @Override
    public 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);
    }
 
    @Override
    public double getArea() {
        return Math.pow((getS() / 4), 2);
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值