COMP2013 Data Structures and Algorithms Programming 1C/C++

Java Python COMP2013

Data Structures and Algorithms

Programming Assignment 1

Deadline: 10:00am, 28th  OCT, 2024

Instructions

Submit the soft-copy of your program to Learn@PolyU

You can only submit one program file (either C++ or Java or Python), and the filename must follow the format below.

Language

Filename format

Filename example

C++

mainStudentID.cpp

main10987654d.cpp

Java

mainStudentID.java

main10987654d.java

Python

mainStudentID.py

main10987654d.py

Section 1: Problem

Problem description:

You need exactly m truck drivers to transport goods from a source location to a destination. All m truck drivers will depart from the source at the sametime. Each truck driver may take a different amount of time to reach the destination. There are n candidate drivers available, and you need to select m drivers from this pool. The driving time for each candidate driver from the source to the destination is given in an array A of size n, where each element represents the driving time in hours  (a positive real number). For example, if A=[2.3, 3, 2.5, 7], it means there aren=4 candidate drivers with driving times of 2.3 hours, 3 hours, 2.5 hours, and 7 hours respectively. Your objective is to select m drivers so that the difference between the maximum and minimum driving times among the selected drivers is minimized.

Input: Array A with n elements, m

Output: The minimum difference value

Note: You cannot use existing libraries to directly call off-the-shelf heap, queue, sorting, stack algorithms, but you can implement them when needed. Except these algorithms, you can use existing libraries when necessary, in your program.

Examples for the problem

Input

Output

Example 1

A=[2.3, 3, 2.5, 7], m=2

0.2

Example 2

A=[3, 4.3, 6.4, 5.2, 7.1], m=3

1.9

Section 2: Input and Output Format

Your program should read a test file in “.txt” containing the driving times of n drivers. The format of test files is illustrated by examples “file1.txt” and “file2.txt” below.

Your program should output on the screen a single value that is the minimum difference.

Here are samples of the input file and the output of your program.

Sample input file “file1.txt”

The output onscreen (stdout in C++)

4

2

2.3 3

2.5 7

0.2

Sample input file “file2.txt”

The output onscreen (stdout in C++)

5

3

3

4.3 6.4 5.2 7.1

1.9

The format of the input file is as follows:

the 1st line shows the number of candidate drivers n,

the 2nd line shows the number of drivers needed m,

each subsequent line shows the driving time of a candidate driver

n is integer in the range [1, 10000000].

m is integer in the range [1, 10000000].

Each driving time is in range (0, 200]

We will run your program by a command line like:

Language

Command line

C++ (after compilation)

./main StudentID input123.txt

Java (after compilation)

java main StudentID input123.txt

Python

python main StudentID.py input123.txt

where the argument “ input123.txt” is an example of the input filename.

Your program should only output the result number.

Please follow the above output format and DO NOT print any extra information.

Your program should pass all possible inputs, including those maybe invalid

-    You need to create testcases to debug and test your program’s correctness.

Section 3: Hints

•   Use alarge enough integer type (e.g., long) to prevent “integer overflow” in counting.

•   A simple program is to use nested for-loops. Its time complexity is O(n2). It is acceptable for small input file but too slow for large input file.

•   Some programming tricks can be used to speedup your program by a constant factor.

Section 4: Grading Criteria

(4.1) Naming conventions, compilation and execution commands

Before submission, rename your program to

mainStudentID.cpp,   e.g., main10987654d.cpp

OR      mainStudentID.java,  e.g., main10987654d.java

OR      mainStudentID.py,     e.g., main10987654d.py

[C++ and Java only] Make sure that you can compile your program using the command:

g++ main StudentID.cpp -o main StudentID

OR      javac main StudentID.java

o No marks will be given if your program cannot be compiled.

We will run your program by a command line like:

./main StudentID input123.txt

OR      java main StudentID input123.txt

OR      python main StudentID.py input123.txt

where the argument “ input123.txt” is an example of the input filename.

Make sure that you can execute the above commands on the COMP apollo server successfully (see Appendix about how to connect to apollo server).

Otherwise, no marks will be given. The current versions on apollo are as follows:

g++ (GCC) 4.8.5,      javac 1.8.0_20,           Python 2.7.5

(4.2) Test files for grading

Total marks: 100%

Your program will be graded by using 10 test files.

The running time of your program will be measured on the COMP apollo server.

For each test file,

if your program can produce the correct output within 1 minute,

then you get +10 marks.

If you use off-the-shelf heap, queue, sorting, stack algorithms in existing libraries, 20 marks will be deducted         

这个错误通常是在运行 C/C++ 程序时出现的。它意味着编译器在编译程序时发生了错误,导致无法正确运行程序。可能的原因包括语法错误、链接错误或者库文件丢失等等。为了解决这个问题,你可以检查编译器的输出信息,查看具体的错误提示,从而找到并修复错误。你也可以在编译程序时加入调试信息,以便更好地定位问题。另外,确保你的代码符合语法规范,并且正确地链接了所需的库文件,这些也有助于解决这个问题。非常抱歉,由于您没有提供更多的上下文信息,我很难确定您遇到的确切问题。不过,通常情况下,“error while running c/c++ comp”是指在运行C/C++编译器时出现了错误。以下是一些常见的可能导致此错误的原因和解决方法: 1. 您的代码存在语法错误或逻辑错误,导致编译器无法正常编译。请检查您的代码并尝试修复任何错误。 2. 您的编译器可能没有正确地配置或安装。请检查您的编译器的设置和安装,并确保它们正确地安装和配置。 3. 您的代码可能依赖于缺失的库或头文件。请检查您的代码并确保它们正确地包含了必要的库和头文件。 4. 您的代码可能包含了与平台或操作系统不兼容的部分。请检查您的代码并确保它们与您的平台和操作系统兼容。 如果以上解决方法仍然无法解决您的问题,我建议您提供更多的上下文信息,例如具体的错误消息和代码片段,这将有助于更准确地确定问题并提供更好的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值