Đề Thi PE CSD201 - PE - De01

  • Bắt đầu Bắt đầu Gauche_97
  • Ngày bắt đầu Ngày bắt đầu
  • Trả lời Trả lời 6
  • Xem Xem 7K
  • Thẻ Thẻ
    csd201
Campus
Hồ Chí Minh
Học kỳ
SU2025
Loại tài liệu
PE

Gauche_97

FPT Student
Tham gia
10/9/23
Bài viết
1
FUO Point
694
CSD201 - PE - De01
18/07/2025 18h30
 

Đính kèm

  • PE_CSD201_De01_897649.zip
    PE_CSD201_De01_897649.zip
    37.4 KB · Xem: 1,187
  • CSD201_-_PE_-_De01_3965.webp
    CSD201_-_PE_-_De01_3965.webp
    61.7 KB · Xem: 9,051
  • CSD201_-_PE_-_De01_3965.webp
    CSD201_-_PE_-_De01_3965.webp
    64.5 KB · Xem: 9,225
  • CSD201_-_PE_-_De01_3965.webp
    CSD201_-_PE_-_De01_3965.webp
    69.5 KB · Xem: 6,366
  • CSD201_-_PE_-_De01_3965.webp
    CSD201_-_PE_-_De01_3965.webp
    48 KB · Xem: 6,020
  • CSD201_-_PE_-_De01_3965.webp
    CSD201_-_PE_-_De01_3965.webp
    26.4 KB · Xem: 10,070
Dap an o duoi nka, con cach giai thi ib
 
Chỉnh sửa lần cuối:
chắc đợt trước rớt hơn 50% do điều kiện xong giờ không cho điều kiện nx
 
Java:
import java.io.*;

class SllStudent {
    SllNode head, tail;

    SllStudent() { head = tail = null; }

    boolean isEmpty() { return (head == null); }

    void clear() { head = tail = null; }
    
    void loadDataRegistration(int k) { //do not edit this function   
        int[] a = Lib.readLineToIntArray("data.txt", k);
        String[] b = Lib.readLineToStrArray("data.txt", k + 1);
        boolean[] c = Lib.readLineToBooleanArray("data.txt", k + 2);     
        int n = a.length;
        for (int i = 0; i < n; i++) {
            addStudent(a[i], b[i], c[i]);
        }
    }

    void addStudent(int xID, String xName, boolean xFee) {     
        SllNode newNode = new SllNode(new Student(xID,xName,xFee));
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        if(head == null){
            head = tail = newNode;
            
        }else{
            tail.next = newNode;
            tail = newNode;
        }
          
        //------------------------------------------------------------------
    }
    
    void printInsertionOrder(RandomAccessFile f) throws Exception {
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        int count = 0;
        SllNode p = head;
        while (p != null && count <5) {
            if(p.info.isFee() == true){
                count++;
                f.writeBytes("(" + p.info.getId() + ","
                + p.info.getName() + "," + p.info.isFee() + ") ");
            }
            p = p.next;       
        }
        f.writeBytes("\r\n");
        f.close();
        //------------------------------------------------------------------
    }

    void display(RandomAccessFile f) throws Exception {
        SllNode p = head;
        while (p != null) {
            f.writeBytes("(" + p.info.getId() + ","
                        + p.info.getName() + "," + p.info.isFee() + ") ");
            p = p.next;       
        }
        f.writeBytes("\r\n");
        f.close();
    }
}

class BstStudent {
    BstNode root;
    
    BstStudent() { root = null; }

    boolean isEmpty() { return (root == null); }

    void clear() { root = null; }

    void fvisit(BstNode p, RandomAccessFile f) throws Exception {
        if (p != null) {
            f.writeBytes(p.info + " ");
        }
    }

    void inOrder(BstNode p, RandomAccessFile f) throws Exception {
        if (p == null) { return; }
        inOrder(p.left, f);
        fvisit(p, f);
        inOrder(p.right, f);
    }
    
    void printPaidDesc(BstNode p, RandomAccessFile f) throws Exception {
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        if(p == null){
            return;
        }else{
            printPaidDesc(p.right, f);
            if(p.info.isFee() == true){
                fvisit(p, f);
            }
            printPaidDesc(p.left, f);
        }
        //------------------------------------------------------------------
    }
    
    void loadDataRegistration(int k) //do not edit this function
    {
        int[] a = Lib.readLineToIntArray("data.txt", k);
        String[] b = Lib.readLineToStrArray("data.txt", k + 1);
        boolean[] c = Lib.readLineToBooleanArray("data.txt", k + 2);   
        int n = a.length;
        for (int i = 0; i < n; i++) {
            insert(a[i], b[i], c[i]);
        }
    }
    
    BstNode insertStudent(BstNode root, Student std) {
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        if(root == null){
            root = new BstNode(std);
        }else{
            if(root.info.getId() > std.getId()){
                root.left = insertStudent(root.left, std);
            }else{
                root.right = insertStudent(root.right, std);
            }
        }
        //------------------------------------------------------------------
        return root;
    }
    
    void insert(int xID, String xName, boolean xFee) {
        //You should insert here statements to complete this function
        //---------------------------------------------------------------------
        this.root = insertStudent(this.root, new Student(xID,xName,xFee));               
    //---------------------------------------------------------------------       
    }
    
    int count(BstNode node) {   
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        if(node == null){
            return 0;
        }else{
            if(node.info.isFee() != true){
                return 1 + count(node.left) + count(node.right);
            }else{
                return count(node.left) + count(node.right);
            }
        }
        //------------------------------------------------------------------
    }
    
    public BstNode searchById(BstNode node, int id) {
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        if(node == null){
            return null;
        }else{
            if(node.info.getId() == id){
                return node;
            }else{
                if(node.info.getId() > id){
                    return searchById(node.left, id);
                }else{
                    return searchById(node.right, id);
                }
            }
        }
        //------------------------------------------------------------------
    }

    public BstNode search(int id) {
        //You should insert here statements to complete this function
        //------------------------------------------------------------------
        return searchById(this.root, id);
        //------------------------------------------------------------------
    }
}

public class CourseRegistration {
    SllStudent sList = new SllStudent();
    BstStudent bList = new BstStudent();

    CourseRegistration() { }

    void load(int k) throws Exception { //do not edit this function 
        sList.loadDataRegistration(k);
        bList.loadDataRegistration(k);
    }
//=============================================================================
//========YOU CAN EDIT OR EVEN ADD NEW FUNCTIONS IN THE FOLLOWING PART=========
//=============================================================================   
    void f1() throws Exception {
        load(1);
        String fname = "f1.txt";
        File g123 = new File(fname);
        if (g123.exists()) {
            g123.delete();
        }
        RandomAccessFile f = new RandomAccessFile(fname, "rw");
        sList.printInsertionOrder(f);
    }

    void f2() throws Exception {
        load(1);
        String fname = "f2.txt";
        File g123 = new File(fname);
        if (g123.exists()) {
            g123.delete();
        }
        RandomAccessFile f = new RandomAccessFile(fname, "rw");
        //---------------------------------------------------------------------
        /*You must keep statements pre-given in this function.
        Your task is to insert statements here, just after this comment,
        to complete the question in the exam paper.*/       
        int i = 0;
        i = bList.count(bList.root);
        String c = "" + i;
        f.writeBytes(c.trim());       
        //---------------------------------------------------------------------
        f.close();
    }

    void f3(int search_id) throws Exception {
        load(1);
        String fname = "f3.txt";
        File g123 = new File(fname);
        if (g123.exists()) {
            g123.delete();
        }
        RandomAccessFile f = new RandomAccessFile(fname, "rw");
        //---------------------------------------------------------------------
        /*You must keep statements pre-given in this function.
        Your task is to insert statements here, just after this comment,
        to complete the question in the exam paper.*/ 
        BstNode p = bList.search(search_id);
        if (p != null) {
            f.writeBytes("(" + p.info.getId() + ","
                        + p.info.getName() + "," + p.info.isFee() + ") ");
            f.writeBytes("\r\n");
            f.close();
        } else {
            f.writeBytes("Not found");
            f.writeBytes("\r\n");
            f.close();
        }       
        //---------------------------------------------------------------------
    }

    void f4() throws Exception {
        load(1);
        String fname = "f4.txt";
        File g123 = new File(fname);
        if (g123.exists()) {
            g123.delete();
        }
        RandomAccessFile f = new RandomAccessFile(fname, "rw");
        bList.inOrder(bList.root, f);
        f.writeBytes("\r\n");
        //---------------------------------------------------------------------
        /*You must keep statements pre-given in this function.
        Your task is to insert statements here, just after this comment,
        to complete the question in the exam paper.*/       
        bList.printPaidDesc(bList.root, f);       
        //---------------------------------------------------------------------
        f.close();
    }
}
 
Back
Bên trên Bottom