19年机试

1. 编程计算1/1!-1/3!+1/5!-...+(-1)^(n+1)/(2n-1)!
 

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    double sum = shuLieQiuHe(n);
    System.out.println(sum);
}

private static double shuLieQiuHe(int n) {
    double sum = 0.0 ;
    int j ;
    for(int i = 1;i <= n; i++){
        if(i % 2 == 0){
            j = -1;
        }else j = 1;
        sum += (double) 1 /(2*i-1)*j;
    }
    return sum;
}

甲乙丙对一次竞赛名次进行预测。甲:A第1 B第3乙:C第1 D第4丙:D第1 B第3 (数据不是原题,记不太清,但解题方法是一样的)他们都说对了一半,求ABCD正确的名次(ABCD不会出现相同的名次)。   

​​​​​​​public static void main(String[] args) {

        for (int a = 1; a < 5; a++) {
            for (int b = 1; b < 5 && b!=a; b++) {
                for (int c = 1; c< 5 && c!=a && c!=b; c++) {
                    int d = 10-a-b-c;
                    if((a==1||b==3)&&(c==1||d==4)&&((d==1)||(b==3))){
                        System.out.println("a"+a+" "+"b"+b+" "+"c"+c+" "+"d"+d);
                    }
                }
            }
        }
    }

太暴力了。

3.    给定链表节点的定义,实现双链表递归合并
//        struct Node{
//            int data;
//            Node *next;
//        }

    public static void main(String[] args) {
//        
        MyList myList = new MyList();
        myList.add(new ListNode(1,null));
        myList.add(new ListNode(3,null));
//        ListNode head1 = myList.head;
//        while (head1 != null){
//            System.out.println(head1.val);
//            head1 = head1.next;
//        }
//        int c = myList.size;
//        ListNode head1 = myList.head.next;
//        while(c != 0){
//            System.out.println(head1.val);
//            head1 = head1.next;
//            c--;
//        }
        myList.add(new ListNode(5,null));
        myList.add(new ListNode(7,null));
        myList.add(new ListNode(9,null));
        myList.add(new ListNode(11,null));
        MyList myList1 = new MyList();
        myList1.add(new ListNode(2,null));
        myList1.add(new ListNode(4,null));
        myList1.add(new ListNode(6,null));
        myList1.add(new ListNode(8,null));
        myList1.add(new ListNode(10,null));
        myList1.add(new ListNode(12,null));
        MyList myList3 = new MyList();
        MyList myList2 = hebingshuzu(myList3,myList.head.next,myList1.head.next);
        int c = myList2.size;
        ListNode head1 = myList2.head.next;
        while(c != 0){
            System.out.println(head1.val);
            head1 = head1.next;
            c--;
        }
    }

    private static MyList hebingshuzu(MyList myList2,ListNode myList1, ListNode myList) {

        if(myList1 != null && myList != null) {
            if (myList1.val < myList.val) {
                ListNode LN1 = new ListNode(myList1.val,null);
                myList2.add(LN1);
                hebingshuzu(myList2,myList1.next, myList);
            } else {
                ListNode LN = new ListNode(myList.val,null);
                myList2.add(LN);
                hebingshuzu(myList2,myList1, myList.next);
            }
        }
        if(myList1 == null) {
            myList2.add(myList);
            return myList2;
        }
        if(myList == null ) {
            myList2.add(myList1);
            return myList2;
        }
        return myList2;

    }
}
class ListNode{
    int val;
    ListNode next;

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
class MyList{
    ListNode head;
    int size;

    public MyList() {
        head = new ListNode(-1,null);
        size = 0;
    }
    public void add(ListNode listNode){
       ListNode temp = listNode;
       ListNode temp1 = head;
        while(temp.next != null){
            temp = temp.next;
            size++;
        }
        while(temp1.next != null){
            temp1 = temp1.next;
        }
        temp1.next = listNode;
        size++;
    }
}

4.  现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。
其中编号Num需要程序自动生成。现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。并定义3个Guest对象来对成员函数进行测试。

    public static void main(String[] args) {
//        4.	现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。
//        现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。
//        并定义3个Guest对象来对成员函数进行测试。
        Guest guest = new Guest("zhangsan",100);
        Guest guest1 = new Guest("zhangsan1",800);
        System.out.println(guest1.GetTotalIncome());
        Guest guest2 = new Guest("zhangsan2",100);
        guest2.Show();
        System.out.println(guest2.GetTotalIncome());
        System.out.println(guest2.GetCount());

    }

class Guest{
    int num;
    String name;
    int free;
    static int count;
    static int income=0;
    static int a = 0;
    public Guest() {
    }

    public Guest(String name, int free) {
        this.name = name;
        this.free = free;
        income += free;
        num = a++;
        count++;
    }
    public void Show(){
        System.out.println(num+"\t"+name+"\t"+free);
    }
    public int GetCount(){
        return count;
    }
    public int GetTotalIncome(){
        return income;
    }

}

5.  现有一抽象类Shape,它拥有一系列虚函数:Input()输入类需要的信息、Show()显示类的信息、Perimeter()计算周长、Area()计算面积。
先定义Circle、Square、Triangle来继承Shape并实现其虚函数。
要求创建Circle、Square、Triangle的对象,用基类指针指向这些对象,并调用成员函数进行测试。

改成double就行了

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.Input(3);
        System.out.println(circle.Area());
        System.out.println(circle.Perimeter());
    }

abstract class Shape{
    abstract void Input(int a);
    abstract void Input(int a,int b,int c);
    abstract void Show();
    abstract int Perimeter();
    abstract int Area();
}
class Circle extends Shape {
    int R;
    @Override
    void Input(int a, int b, int c) {

    }
    public void Input(int a) {
        R = a;
    }

    @Override
    void Show() {
        System.out.println("我是圆");
    }

    @Override
    int Perimeter() {

        return (int) (2*PI*R);
    }

    @Override
    int Area() {
        return (int) (PI*R*R);
    }
}
class Square extends Shape {
    int bian;
    @Override
    void Input(int a) {
        bian = a;
    }

    @Override
    void Input(int a, int b, int c) {

    }

    @Override
    void Show() {
        System.out.println("我是正方形");
    }

    @Override
    int Perimeter() {
        return bian*4;
    }

    @Override
    int Area() {
        return bian*bian ;
    }
}
class Triangle extends Shape {
    int a = 0;
    int b = 0;
    int c = 0;
    @Override
    void Input(int a) {

    }
    void Input(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    @Override
    void Show() {
        System.out.println("我是三角形");
    }

    @Override
    int Perimeter() {
        return a+b+c;
    }

    @Override
    int Area() {
        int p = Perimeter();
        return (int) Math.sqrt((p*(p-a)*(p-b)*(p-c)));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值