华南师范机试14--java

1.写一个求素数的算法。

import java.util.Scanner;
public class fourteenYear {
    public static void main(String[] args) {
       // 1.    写一个求素数的算法。
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean flag = true;
        if(n == 2) flag = false;
        for(int i = 2; i<n/2;i++){
            if(n%i != 0){
                flag = false;
            }
        }
        if(!flag) {
            System.out.println("是素数");
        }else {
            System.out.println("不是素数");
        }
    }
}

2.在一个有序数列中插入一个数,使得数列依然有序,并且把最大的那个数剔除出队列

没有读懂题目什么意思,于是就全部都用队列写了。

public static void main(String[] args) {
    LinkedList<Integer> queue = new LinkedList<>();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for(int i = 0;i < 10;i++){
        queue.add(i);
    }
    int j = 0;
    for (int i = 0; i < queue.size(); i++) {
        int num = queue.peek();
        if(num<n){
            queue.offer(queue.poll());
            j = i;
        }
    }
    queue.addFirst(n);

    for (int i = 0; i <= queue.size()-j-1; i++) {

        System.out.println(queue.toString());
        if(i == queue.size()-j-2 ){
            if(j != 0) {
                int x = queue.poll();
                System.out.println(x + " " + i);
                break;
            }

        }
        if(j == 0 && i == queue.size()-j-1){
            int x = queue.poll();
            System.out.println(x + " " + i);
            break;
        }

        queue.offer(queue.poll());
    }


    for (Integer i : queue) {
        System.out.print(i + " ");
    }
}

3.写一个类,类里面包含了私有成员变量和方法,并且用该类里面的方法给私有成员变量赋值和输出 

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值