Programming Problems 15th Annual Computer Science Programming Contest

Similar documents
2004 Stanford Local Programming Contest

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AP Computer Science Summer Work Mrs. Kaelin

Input from Files. Buffered Reader

Lesson 3: Accepting User Input and Using Different Methods for Output

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

1. Download the JDK 6, from

2005 Stanford Local Programming Contest

2.8. Decision Making: Equality and Relational Operators

CMSC 201 Spring 2017 Project 1 Number Classifier

Byte and Character Streams. Reading and Writing Console input and output

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Exercise 4: Loops, Arrays and Files

Chapter 2: Basic Elements of Java

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Repetition CSC 121 Fall 2014 Howard Rosenthal

Course Outline. Introduction to java

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

DOMjudge team manual. Summary. Reading and writing. Submitting solutions. Viewing scores, submissions, etc.

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Over and Over Again GEEN163

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Review of Basic Java

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Programming Problems 22nd Annual Computer Science Programming Contest

2015 Canadian Computing Competition: Junior Division. Sponsor:

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Fundamentals of Programming Session 4

TJ IOI Practice Programming Round. Thomas Jefferson High School for Science and Technology

Loops. CSE 114, Computer Science 1 Stony Brook University

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Project #1 Computer Science 2334 Fall 2008

by Pearson Education, Inc. All Rights Reserved. 2

DOMjudge team manual. Summary. Reading and writing. Submitting solutions. Viewing scores, submissions, etc.

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

Question 1a) Trace of program

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

DOMjudge team manual. Summary. Reading and writing. Submitting solutions. Viewing scores, submissions, etc.

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

Algorithms and Problem Solving

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

Official Competition Manual March 2, 2005

2006 Stanford Local Programming Contest

AP CS Unit 3: Control Structures Notes

Lecture 11.1 I/O Streams

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

ACM SOUTH PACIFIC DIVISIONAL PROGRAMMING CONTEST

Full file at

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

2010 Canadian Computing Competition: Senior Division. Sponsor:

Example Program. public class ComputeArea {

2008 Canadian Computing Competition: Junior Division. Sponsor:

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Oct Decision Structures cont d

Introduction to Java Applications

CIS 162 Project 4 Farkle (a dice game)

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Intermediate Math Circles February 07, 2018 Contest Preparation I

You must bring your ID to the exam.

COMP 110 Programming Exercise: Simulation of the Game of Craps

Chapter 2: Data and Expressions

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Object-Oriented Programming Design. Topic : Streams and Files

do fifty-two: An Introductory Programming Language

Decision-Making and Repetition

Chapter 5 Some useful classes

IT 374 C# and Applications/ IT695 C# Data Structures

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Activity 1: Introduction

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Selected Sections of Applied Informatics LABORATORY 0

Introduction to Computer Science Unit 3. Programs

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

download instant at

Handout 4 Conditionals. Boolean Expressions.

St. Edmund Preparatory High School Brooklyn, NY

Data Types and the while Statement

CSEN 202 Introduction to Computer Programming

CMPT 125: Lecture 4 Conditionals and Loops

Midterm Review Session

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

2007 Canadian Computing Competition: Senior Division. Sponsor:

CS Final Exam Review Suggestions - Spring 2014

AP Computer Science Unit 3. Programs

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Add Subtract Multiply Divide

Final Exam. COMP Summer I June 26, points

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Transcription:

Programming Problems 15th Annual Computer Science Programming Contest Department of Mathematics and Computer Science Western Carolina University March 0, 200 Criteria for Determining Scores Each program should: 1. execute without error, solve the appropriate problem efficiently, and satisfy all the conditions specified in the problem statement, 2. follow good programming style conventions such as avoiding confusing and unnecessary code,. be documented with comments and easily understandable variable names, and. start with an initial comment that includes (a) the name of the team s school, (b) the team s number, and (c) the names of the team s members. An example such comment is // Somewhere High School, Team 2, Susan Smith, and Sandy Hart The score assigned to each program will be based upon the extent that the program satisfies the properties listed above. The total accumulated score for all four programs represents the team score. All the programs have the same weight in the scoring. Notes Each team may use two reference books appropriate to their language division (e.g. two Java books for a team competing in the Java division). Each team may use up to two computers. Inappropriate code comments will result in a lower score and is grounds for disqualification for an award. 1

Programming notes General C++ Java Where appropriate, you should do input verification. In other words, if the user is supposed to input, for example, a positive number, an appropriate error message should be displayed when the user does not enter a positive number. The user should then get additional chances to enter valid input. The AP classes apmatrix, apqueue, apstack, apstring, and apvector are available for your use. (This is not to say all these are needed for the contest.) They are located at /home/contest200/ap_cpp_classes Use the cp command to bring them into your home directory. The command below will copy all the AP classes into your home directory, if run from your home directory: cp /home/contest200/ap_cpp_classes/*. The last dot on the above line is important. It copies the files into the current directory. The space immediately preceding this last dot is also important. Furthermore, even though this is 200, the 200 is not a typo. The College Board requires that we include the following disclaimer regarding the ap classes: Inclusion of the C++ classes defined for use in the Advanced Placement Program Computer Science courses does not constitute endorsement of the other material in this contest by the College Board, Educational Testing Service, or the AP Computer Science Development Committee. The versions of the C++ classes defined for use in the AP Computer Science courses included in this contest were accurate as of March 20, 200. Revisions to the classes may have been made since that time. If you wish to use the C++ standard string class, you must include the following statement along with the #include<string> statement: using namespace std; Console I/O (i.e. keyboard input & screen output) is the expected form of input/output. Use System.out.println (or System.out.print, as appropriate) for output. The following information may be useful to implement keyboard input: 1. First, make sure that the following import statement is included at the very top of the code: import java.io.*; 2. Before doing any keyboard input, declare a BufferedReader object (named br in the following example) tied to System.in: BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );. Use this object to read Strings (one line at a time) from the keyboard; for example: System.out.println("Enter one line of input:"); String s = br.readline(); System.out.println("Enter another line of input:"); String t = br.readline(); The above fragment would read two lines from the keyboard; s references the String that is the first line of input, while t references the second line.. Use Integer.parseInt or Double.parseDouble to convert any Strings to ints or doubles, respectively. Reading a line of input and converting it to a numeric type can be done in one statement; for example: int i = Integer.parseInt( br.readline() ); 5. Finally, any methods that contain a call to readline (as well as methods that call a method that calls readline, etc.) need a throws Exception clause in the method header, unless you want to deal with Exception handling and place the readline call in a try..catch block. (This type of Exception handling is not an expectation for the contest, so it s recommended that you just include the throws Exception clause.) For example: public static void main( String[] args ) throws Exception {... // call to readline somewhere in here } 2

An additional note about console I/O and BlueJ: Be sure to have a System.out.println statement that executes before any call to readline. The System.out.println call tells BlueJ to open a console window; it s not smart enough to do this if a call is made to readline first.

1 Final Jeopardy You re contestant #1 on the popular Jeopardy game show, it s time for Final Jeopardy, and you re trying to decide your wager. Recall that in the Final Jeopardy round, each contestant must wager any amount between $0 and their current winnings (inclusive), knowing only the Final Jeopardy category. Each contestant wins or loses the dollar amount of their wager if they answer the Final Jeopardy question correctly or incorrectly, respectively. Not considering the Final Jeopardy category, write a program that accepts as input your current winnings, followed by the current winnings of your two competitors (contestants #2 and #). The program should determine your final jeopardy wager, so that in the case you win, you do so by only $1. (Therefore, you don t wager too much, just enough to win if you can.) For example, if you are currently in the lead with $9000, while your closest competitor only has $000, your wager should be $999. (Because in the worst-case that your competitor wagers their whole $000 and answers correctly, you can answer incorrectly and still win by $1.) Or, for example, suppose you have the lead with $8000, while your closest competitor has $5000; you should wager $2001. (Because if your competitor wagers their entire $5000 and answers correctly, you need to answer correctly and have wagered $2001 to win by $1.) If, however, you do not currently have the lead, you should wager your entire winnings. Welcome to the Final Jeopardy round! Enter your current winnings: 8000 Enter the current winnings of contestant #2: 000 Enter the current winnings of contestant #: 2000 You can comfortably wager $1999. Another example session: Welcome to the Final Jeopardy round! Enter your current winnings: 000 Enter the current winnings of contestant #2: 000 Enter the current winnings of contestant #: 500 You re not in the lead, and should wager your entire $000.

2 Array Rank The rank of an element in an array of integers is the number of smaller elements in the array plus the number of equal elements that appear to its left. For example, consider the array [,, 9,, 7]. The respective ranks of these elements are 2, 0,, 1, and ; thus the rank array is the array [2, 0,, 1, ]. Write a program that prompts the user for an arbitrarily-sized array of integers, and then outputs the corresponding rank array. Enter desired size of array : 5 9 7 The input array was: 9 7 The corresponding rank array is: 2 0 1 5

Perfect Numbers A number (consider only positive integers) is perfect if it is equal to the sum of its proper divisors. For example, 6 is a perfect number, because its proper divisors are 1, 2, and (note that we do not include the number itself), and 1 + 2 + = 6. A number is deficient if the sum of its proper divisors is less than the number. For example, 8 is deficient, because its proper divisors are 1, 2, and, and 1 + 2 + = 7, which is less than 8. A number is abundant if the sum of its proper divisors is greater than the number. For example, 12 is abundant, because 1 + 2 + + + 6 = 16, which is greater than 12. Write a program that prompts the user for a number, then determines whether the number is perfect, deficient, or abundant. Your program should continue to prompt the user for numbers until a 0 is provided as input. Enter an integer (0 to quit): 7 7 is deficient. Enter an integer (0 to quit): 12 12 is abundant. Enter an integer (0 to quit): 6 6 is perfect. Enter an integer (0 to quit): 0 Goodbye! 6

Permutations A permutation of an array of integers is an arrangement of the integers in the array. Consider, for example, the array of integers [5, 2, ]. One possible permutation is the array [2, 5, ]. Some other permutations are [2,, 5], [5,, 2], and [5, 2, ] (the original array itself). For this program, we ll assume that no integer appears more than once in the original array. (Thus, we are not considering arrays such as [,, ].) Note that for this example of an array of three integers, there are six possible permutations. Write a program that prompts the user for an arbitrarily-sized array of integers, and outputs all the permutations of the input array. Enter number of integers in array : 1 2 1 2 1 2 2 1 2 1 1 2 2 1 Another example session: Enter number of integers in array : 6 2-5 6 2-5 6-5 2 6 2-5 6 2-5 6-5 2 6-5 2 6 2-5 6-5 2 2 6-5 2-5 6-5 6 2-5 2 6 2 6-5 2 6-5 2 6-5 2-5 6 2-5 6 2-5 6-5 6 2-5 6 2-5 6 2-5 2 6-5 2 6-5 2 6 Note: It is not critical that your program output the permutations in any particular order, it just needs to list every permutation exactly once. 7

5 Tie Breaker: Permutations, Part 2 Note: This program will only be graded if needed to break a tie. Write a new version of the Permutations program that does not assume that elements in the input array are distinct. Again, this program should output each possible permutation exactly once. (Thus, you must take care not to output any duplicate permutations.) Enter number of integers in original array : 1 1 2 1 1 2 1 2 1 2 1 1 Another example session: Enter number of integers in original array : 2 And, one more example session: Enter number of integers in original array : 8