Showing posts with label Volume VI. Show all posts
Showing posts with label Volume VI. Show all posts

Solution of 694 - The Collatz Sequence

Problem Description
source:https://uva.onlinejudge.org/external/6/694.html

An algorithm given by Lothar Collatz produces sequences of integers, and is described as follows: 

Step 1: Choose an arbitrary positive integer A as the first item in the sequence. 
Step 2: If A = 1 then stop. 
Step 3: If A is even, then replace A by A/2 and go to step 2. 
Step 4: If A is odd, then replace A by 3 ∗ A + 1 and go to step 2. 

It has been shown that this algorithm will always stop (in step 2) for initial values of A as large as 109 , but some values of A encountered in the sequence may exceed the size of an integer on many computers. In this problem we want to determine the length of the sequence that includes all values produced until either the algorithm stops (in step 2), or a value larger than some specified limit would be produced (in step 4). 

image

Solution of 673 - Parentheses Balance

Problem Description
source:https://uva.onlinejudge.org/external/6/673.html

You are given a string consisting of parentheses () and []. A string of this type is said to be correct: 

(a) if it is the empty string 
(b) if A and B are correct, AB is correct, 
(c) if A is correct, (A) and [A] is correct. 

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

image

Solution of 621 - Secret Research

Problem Description
source:https://uva.onlinejudge.org/external/6/621.html

At a certain laboratory results of secret research are thoroughly encrypted. A result of a single experiment is stored as an information of its completion:
      ‘positive result’, ‘negative result’, ‘experiment failed’ or ‘experiment not completed’
      The encrypted result constitutes a string of digits S, which may take one of the following forms:
        • positive result S = 1 or S = 4 or S = 78
        • negative result S = S35
        • experiment failed S = 9S4
        • experiment not completed S = 190S

(A sample result S35 means that if we add digits 35 from the right hand side to a digit sequence then we shall get the digit sequence corresponding to a failed experiment)
      You are to write a program which decrypts given sequences of digits.

image

Solution of 616 - Coconuts, Revisited

Problem Description
source:https://uva.onlinejudge.org/external/6/616.html

The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening Post on October 9, 1926. The story tells about five men and a monkey who were shipwrecked on an island. They spent the first night gathering coconuts. During the night, one man woke up and decided to take his share of the coconuts. He divided them into five piles. One coconut was left over so he gave it to the monkey, then hid his share and went back to sleep.
    Soon a second man woke up and did the same thing. After dividing the coconuts into five piles, one coconut was left over which he gave to the monkey. He then hid his share and went back to bed. The third, fourth, and fifth man followed exactly the same procedure. The next morning, after they all woke up, they divided the remaining coconuts into five equal shares. This time no coconuts were left over.
image