Download ACMexcise

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Power dividers and directional couplers wikipedia , lookup

CMOS wikipedia , lookup

Oscilloscope history wikipedia , lookup

Power electronics wikipedia , lookup

Phase-locked loop wikipedia , lookup

Radio transmitter design wikipedia , lookup

Amplifier wikipedia , lookup

Analog-to-digital converter wikipedia , lookup

Wilson current mirror wikipedia , lookup

Current mirror wikipedia , lookup

Negative-feedback amplifier wikipedia , lookup

Integrating ADC wikipedia , lookup

Valve RF amplifier wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Flip-flop (electronics) wikipedia , lookup

Operational amplifier wikipedia , lookup

Schmitt trigger wikipedia , lookup

Transistor–transistor logic wikipedia , lookup

Opto-isolator wikipedia , lookup

Rectiverter wikipedia , lookup

Transcript
ACM 练习题
编号
Z1
Z2
题目内容
A + B Problem
Calculate a + b
Input
The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in
input.
Sample Input
15
Sample Output
6
Crashing Balloon
On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV. The rule is very simple. On the
ground there are 100 labeled balloons, with the numbers 1 to 100. After the referee shouts "Let's go!" the two players, who each
starts with a score of "1", race to crash the balloons by their feet and, at the same time, multiply their scores by the numbers written
on the balloons they crash. After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant
reports his\her score, the product of the numbers on the balloons he\she's crashed. The unofficial winner is the player who
announced the highest score.
Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved. The player who
claims the lower score is entitled to challenge his\her opponent's score. The player with the lower score is presumed to have told the
truth, because if he\she were to lie about his\her score, he\she would surely come up with a bigger better lie. The challenge is upheld
if the player with the higher score has a score that cannot be achieved with balloons not crashed by the challenging player. So, if the
challenge is successful, the player claiming the lower score wins.
So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score
343 is by crashing balloons labeled 7 and 49, and the only way to score 49 is by crashing a balloon labeled 49. Since each of two
scores requires crashing the balloon labeled 49, the one claiming 343 points is presumed to be lying.
On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one
crashes balloons 2, 3 and 27, while the other crashes balloon 81), so the challenge would not be upheld.
1
3
By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if
one player claims 10001 points and the other claims 10003, then clearly none of them are telling the truth. In this case, the challenge
would not be upheld.
Unfortunately, anyone who is willing to referee a game of crashing balloon is likely to get over-excited in the hot atmosphere that
he\she could not reasonably be expected to perform the intricate calculations that refereeing requires. Hence the need for you, sober
programmer, to provide a software solution.
Input
Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of crashing balloon.
Output
Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.
Sample Input
343 49
3599 610
62 36
Sample Output
49
610
62
Parencodings
Let S = s1 s2 … s2n be a well-formed string of parentheses. S can be encoded in two different ways:

By an integer sequence P = p1 p2 … pn where pi is the number of left parentheses before the ith right parenthesis in S
(P-sequence).

By an integer sequence W = w1 w2 … wn where for each right parenthesis, say a in S, we associate an integer which is the
number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:
S (((()()())))
P-sequence 4 5 6666
W-sequence 1 1 1456
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each
2
4
test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed string.
It contains n positive integers, separated with blanks, representing the P-sequence.
Output
The output consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers
describing the W-sequence of the string corresponding to its given P-sequence.
Sample Input
2
6
456666
9
466668999
Sample Output
111456
112451139
Currency Exchange
When Issac Bernand Miller takes a trip to another country, say to France, he exchanges his US dollars for French francs. The
exchange rate is a real number such that when multiplied by the number of dollars gives the number of francs. For example, if the
exchange rate for US dollars to French francs is 4.81724, then 10 dollars is exchanged for 48.1724 francs. Of course, you can only
get hundredth of a franc, so the actual amount you get is rounded to the nearest hundredth. (We'll round .005 up to .01.) All
exchanges of money between any two countries are rounded to the nearest hundredth.
Sometimes Issac's trips take him to many countries and he exchanges money from one foreign country for that of another. When
he finally arrives back home, he exchanges his money back for US dollars. This has got Issac thinking about how much if his unspent
US dollars is lost (or gained!) to these exchange rartes. You'll compute how much money Issac ends up with if he exchanges it many
times. You'll always start with US dollars and you'll always end with US dollars.
Input
The first 5 lines of input will be the exchange rates between 5 countries, numbered 1 through 5. Line i will five the exchange rate
from country i to each of the 5 countries. Thus the jth entry of line i will give the exchange rate from the currency of country i to the
currency of country j. the exchange rate form country i to itself will always be 1 and country 1 will be the US. Each of the next lines
will indicate a trip and be of the form
N c1 c2 … cn m
Where 1 <= n <= 10 and c1, …, cn are integers from 2 through 5 indicating the order in which Issac visits the countries. (A value
3
5
of n = 0 indicates end of input, in which case there will be no more numbers on the line.) So, his trip will be 1 -> c1 -> c2 -> … -> cn
-> 1. the real number m will be the amount of US dollars at the start of the trip.
Output
Each trip will generate one line of output giving the amount of US dollars upon his return home from the trip. The amount should
be fiven to the nearest cent, and should be displayed in the usual form with cents given to the right of the decimal point, as shown in
the sample output. If the amount is less than one dollar, the output should have a zero in the dollars place.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
1 1.57556 1.10521 0.691426 7.25005
0.634602 1 0.701196 0.43856 4.59847
0.904750 1.42647 1 0.625627 6.55957
1.44616 2.28059 1.59840 1 10.4843
0.137931 0.217555 0.152449 0.0953772 1
3 2 4 5 20.00
1 3 100.00
6 2 3 4 2 4 3 120.03
0
Sample Output
19.98
99.99
120.01
To the Max
Problem
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or
greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the
sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
4
6
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
92
-4 1
-1 8
and has a sum of 15.
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating
the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These
are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in
the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Example
Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
Output
15
Palindrom Numbers
Statement of the Problem
We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number
75457 is a palindrom.
Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its
representation in base 2 (10001) is a palindrom.
The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.
Input Format
5
7
Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input
ends with a zero.
Output Format
Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the
representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print
the message Number i is not palindrom.
Sample Input
17
19
0
Sample Output
Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom
Monkey and Banana
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building,
and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by
placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid
with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of
the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower,
one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly
smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on.
This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
Input Specification
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
Output Specification
6
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the
tallest possible tower in the format "Case case: maximum height = height"
Sample Input
1
10 20 30
2
6 8 10
555
7
111
222
333
444
555
666
777
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0
Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
8
Ships
Probably everyone who ever attended school knows the game where two opposing players place a set of ships on a sheet of paper
and try to eliminate each other's ships by guessing their location.
7
In our version of the game, your opponent has distributed the following seven ship patterns over a rectangular grid of squares:
xx xx
xx x
x
x
xx
xx xx
xxx
xxx xxx xxxx
Each ship pattern covers exactly four squares. The patterns may be rotated but not mirrored. All patterns are guaranteed to be
placed completely within the boundaries of the rectangle and not to overlap each other, whereas touching another pattern or the
border is allowed.
We assume that we are in the middle of the game and that several squares have already been uncovered. You will be given a
rectangular grid of squares representing your current knowledge about the positions of your enemy's ships. Every square is marked
by one of the following characters:
`x' if a ship covers the square
`o' if no ship covers the square
`.' if the square has not yet been uncovered
Given that information, you are to decide whether you can determine all remaining `x' squares with at most one miss, i.e. whether
you could uncover the `.' squares without getting more than one `o' square before you had all `x' squares uncovered. This means you
are allowed to hit a `o' if then the solution becomes unique.
Input
The input contains several game situations. Every test case starts with a line containing two integers h and w. These define width
and height of the game rectangle, where 2 <= w, h <= 16.
Each of the next h lines contains a string of w characters. Each of these characters is either `x', `o' or `.', depending on the state of the
corresponding square.
A blank line separates each game from the next. The input ends with a game having w = 0 and h = 0. This game should not be
processed.
Output
For each test case you should first output a line containing the number of the game, followed by a line containing either `yes.' (if
you can determine all `x' with at most one miss) or `no.' (if you cannot determine all `x' without at least two misses).
Output a blank line after every game.
Sample Input
10 10
.x..x.....
oooooxoooo
8
oxooxxx...
xxoooooo..
xoooxooo..
ooxxxxoo..
oooooxxoox
ooooooxoox
ooooooooxx
oooooooooo
00
Sample Output
Game #1
yes.
9
Packets
A factory produces products packed in square packets of the same height h and of the sizes 1x1, 2x2, 3x3, 4x4, 5x5, 6x6. These
products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6x6.
Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to
deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of
parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a
program.
Input
The input consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated
by one space representing successively the number of packets of individual size from the smallest size to the biggest size . The end of
the input is indicated by the line containing six zeros.
Output
The output contains one line for each line in the input. This line contains the minimal number of parcels into which the order from
the corresponding line of the input file can be packed. There is no line in the output corresponding to the last ``null'' line of the input.
Sample Input
004001
751000
000000
Sample Output
9
2
1
10
Network
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered
by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places
and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges.
From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in
such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places
cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying
to write a program for finding the number of all such critical places. Help them.
Input
The input consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number
of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which
there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places
in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line
containing just 0. The last block has only one line with N = 0.
Output
The output contains for each block except the last in the input one line containing the number of critical places.
Sample Input
5
51234
0
6
213
5462
0
0
Sample Output
1
2
10
11
Uniform Generator
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form
seed(x+1) = [seed(x) + STEP] % MOD
where '%' is the modulus operator.
Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is
that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values
carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.
For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a
repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of
the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a
function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD
iterations.
If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other
than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.
Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.
Input
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).
Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value
right-justified in columns 11 through 20 and either ``Good Choice" or ``Bad Choice" left-justified starting in column 25. The ``Good
Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and
MOD-1 when MOD numbers are generated. Otherwise, your program should print the message ``Bad Choice". After each output test
set, your program should print exactly one blank line.
Sample Input
35
15 20
63923 99999
Sample Output
3
5
Good Choice
15
20
Bad Choice
63923
99999
Good Choice
11
12
Excuses, Excuses!
Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order
to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a
list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.
Input
Input to your program will consist of multiple sets of data.
Line 1 of each set will contain exactly two integers. The first number (1 <= K <= 20) defines the number of keywords to be used in
the search. The second number (1 <= E <= 20) defines the number of excuses in the set to be searched.
Lines 2 through K+1 each contain exactly one keyword.
Lines K+2 through K+1+E each contain exactly one excuse.
All keywords in the keyword list will contain only contiguous lower case alphabetic characters of length L (1 <= L <= 20) and will
occupy columns 1 through L in the input line.
All excuses can contain any upper or lower case alphanumeric character, a space, or any of the following punctuation marks
[SPMamp".,!?&] not including the square brackets and will not exceed 70 characters in length.
Excuses will contain at least 1 non-space character.
Output
For each input set, you are to print the worst excuse(s) from the list.
The worst excuse(s) is/are defined as the excuse(s) which contains the largest number of incidences of keywords.
If a keyword occurs more than once in an excuse, each occurrence is considered a separate incidence.
A keyword ``occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of
the line or any non-alphabetic character or a space.
For each set of input, you are to print a single line with the number of the set immediately after the string ``Excuse Set #". (See the
Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one
worst excuse, you may print them in any order.
After each set of output, you should print a blank line.
Sample Input
53
dog
ate
homework
canary
12
13
died
My dog ate my homework.
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
This excuse is so good that it contain 0 keywords.
65
superhighway
crazy
thermonuclear
bedroom
war
building
I am having a superhighway built in my bedroom.
I am actually crazy.
1234567890.....,,,,,0987654321?????!!!!!!
There was a thermonuclear war!
I ate my dog, my canary, and my homework ... note outdated keywords?
Sample Output
Excuse Set #1
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
Excuse Set #2
I am having a superhighway built in my bedroom.
There was a thermonuclear war!
M*A*S*H
Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean War; and he will do just about anything
to get out. The U.S. Army has made an offer for a lottery that will choose some number of lucky people (X) to return to the states for
a recruiting tour. Klinger needs your help getting out.
The lottery is run by lining up all the members of the unit at attention and eliminating members by counting off the members from 1
to N where N is a number chosen by pulling cards off of the top of a deck. Every time N is reached, that person falls out of the line,
and counting begins again at 1 with the next person in line. When the end of the line has been reached (with whatever number that
may be), the next card on the top of the deck will be taken, and counting starts again at 1 with the first person in the remaining line.
The last X people in line get to go home.
13
14
Klinger has found a way to trade a stacked deck with the real deck just before the selection process begins. However, he will not
know how many people will show up for the selection until the last minute. Your job is to write a program that will use the deck
Klinger supplies and the number of people in line that he counts just before the selection process begins and tell him what position(s)
in the line to get in to assure himself of a trip home. You are assured that Klinger's deck will get the job done by the time the 20th
card is used.
A simple example with 10 people, 2 lucky spots, and the numbers from cards 3, 5, 4, 3, 2 would show that Klinger should get in
positions 1 or 8 to go home.
Input
For each selection, you will be given a line of 22 integers. The first integer (1 <= N <= 50) tells how many people will participate
in the lottery. The second integer (1 <= X <= N) is how many lucky "home" positions will be selected. The next 20 integers are the
values of the first 20 cards in the deck. Card values are interpretted to integer values between 1 and 11 inclusive.
Output
For each input line, you are to print the message ``Selection #A" on a line by itself where A is the number of the selection starting
with 1 at the top of the input file. The next line will contain a list of ``lucky" positions that Klinger should attempt to get into. The list
of ``lucky" positions is then followed by a blank line.
Sample Input
10 2 3 5 4 3 2 9 6 10 10 6 2 6 7 3 4 7 4 5 3 2
47 6 11 2 7 3 4 8 5 10 7 8 3 7 4 2 3 9 10 2 5 3
Sample Output
Selection #1
18
Selection #2
1 3 16 23 31 47
Perfect Cubes
For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n =
b^n + c^n, has remained elusively unproven. (A recent proof is believed to be correct, though it is still undergoing scrutiny.) It is
possible, however, to find integers greater than 1 that satisfy the ``perfect cube'' equation a^3 = b^3 + c^3 + d^3 (e.g. a quick
calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to
find all sets of numbers {a, b, c, d} which satisfy this equation for a <= 200.
Output
The output should be listed as shown below, one perfect cube per line, in non-decreasing order of a (i.e. the lines should be sorted
14
15
by their a values). The values of b, c, and d should also be listed in non-decreasing order on the line itself. There do exist several
values of a which can be produced from multiple distinct sets of b, c, and d triples. In these cases, the triples with the smaller b values
should be listed first.
The first part of the output is shown here:
Cube = 6, Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
Note: The programmer will need to be concerned with an efficient implementation. The official time limit for this problem is 2
minutes, and it is indeed possible to write a solution to this problem which executes in under 2 minutes on a 33 MHz 80386 machine.
Due to the distributed nature of the contest in this region, judges have been instructed to make the official time limit at their site the
greater of 2 minutes or twice the time taken by the judge's solution on the machine being used to judge this problem.
The Drunk Jailer
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey, and then runs
down the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell
(cells 2, 4, 6, …). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, …). If
the cell is locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately
escape.
Given the number of cells, determine how many prisoners escape jail.
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines
contains a single integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
15
5
100
Sample Output
2
10
16
Gamblers
A group of n gamblers decide to play a game:
At the beginning of the game each of them will cover up his wager on the table and the assitant must make sure that there are no two
gamblers have put the same amount. If one has no money left, one may borrow some chips and his wager amount is considered to be
negative. Assume that they all bet integer amount of money.
Then when they unveil their wagers, the winner is the one who's bet is exactly the same as the sum of that of 3 other gamblers. If there
are more than one winners, the one with the largest bet wins.
For example, suppose Tom, Bill, John, Roger and Bush bet $2, $3, $5, $7 and $12, respectively. Then the winner is Bush with $12
since $2 + $3 + $7 = $12 and it's the largest bet.
Input
Wagers of several groups of gamblers, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of
gamblers in a group, followed by their amount of wagers, one per line. Each wager is a distinct integer between -536870912 and
+536870911 inclusive. The last line of input contains 0.
Output
For each group, a single line containing the wager amount of the winner, or a single line containing "no solution".
Sample Input
5
2
3
5
7
12
5
2
16
64
16
256
1024
0
Output for Sample Input
12
no solution
Z17
Z18
Reverse Text
In most languages, text is written from left to right. However, there are other languages where text is read and written from right to
left. As a first step towards a program that automatically translates from a left-to-right language into a right-to-left language and back,
you are to write a program that changes the direction of a given text.
Input Specification
The input contains several test cases. The first line contains an integer specifying the number of test cases. Each test case consists of
a single line of text which contains at most 70 characters. However, the newline character at the end of each line is not considered to
be part of the line.
Output Specification
For each test case, print a line containing the characters of the input line in reverse order.
Sample Input
3
Frankly, I don't think we'll make much
money out of this scheme.
madam I'm adam
Sample Output
hcum ekam ll'ew kniht t'nod I ,ylknarF
.emehcs siht fo tuo yenom
mada m'I madam
Digital Roots
Background
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that
digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is
continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital
17
Z19
root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be
repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
Output
For each integer in the input, output its digital root on a separate line of the output.
Example
Input
24
39
0
Output
6
3
Word Reversal
For each list of words, output a line with each word reversed without changing the order of the words.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each
case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.
Output
For each test case, print the output on one line.
Sample Input
1
3
I am happy today
To be or not to be
I want to win the practice contest
18
Sample Output
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc
Z20
Z21
A Mathematical Curiosity
Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a^2+b^2 +m)/(ab) is an integer.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input
is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.
Output
For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case
on one line in the format as shown below.
Sample Input
1
10 1
20 3
30 4
00
Sample Output
Case 1: 2
Case 2: 4
Case 3: 5
Niven Numbers
A Niven number is a number such that the sum of its digits divides itself. For example, 111 is a Niven number because the sum of
its digits is 3, which divides 111. We can also specify a number in another base b, and a number in base b is a Niven number if the
sum of its digits divides its value.
Given b (2 <= b <= 10) and a number in base b, determine whether it is a Niven number or not.
19
Z22
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
You will be given a number of test cases. Each line of input contains the base b, followed by a string of digits representing a positive
integer in that base. There are no leading zeroes. The input is terminated by a line consisting of 0 alone.
Output
For each case, print "yes" on a line if the given number is a Niven number, and "no" otherwise.
Sample Input
1
10 111
2 110
10 123
6 1000
8 2314
0
Sample Output
yes
yes
no
yes
no
Perfection
From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c are integers such that a = bc, a is called a multiple of b or
of c, and b or c is called a divisor or factor of a. If c is not 1/-1, b is called a proper divisor of a. Even integers, which include 0, are
multiples of 2, for example, -4, 0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a
positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which
equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant
according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1,
3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant."
20
Given a number, determine if it is perfect, abundant, or deficient.
Input
A list of N positive integers (none greater than 60,000), with 1 < N < 100. A 0 will mark the end of the list.
Output
The first line of output should read PERFECTION OUTPUT. The next N lines of output should list for each input integer whether
it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within
the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output
should read END OF OUTPUT. 解题思路:利用 while 语句实现输入的循环,for 循环实现判断输
Sample Input
入对应于输出的哪一种,再实现输出
15 28 6 56 60000 22 496 0 //必须输入字符形式
Sample Output
PERFECTION OUTPUT // 必须先单独输出
15 DEFICIENT
28 PERFECT
6 PERFECT
56 ABUNDANT
60000 ABUNDANT
22 DEFICIENT
496 PERFECT
END OF OUTPUT
Function Run Fun
23
We all love recursion! Don't we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
21
24
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15,
b = 15, c = 15), the program takes hours to run because of the massive recursion.
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above
technique, you are to calculate w(a, b, c) efficiently and print the result. For example:
111
222
10 4 6
50 50 50
-1 7 18
-1 -1 -1
Output
Print the value for w(a,b,c) for each triple, like this:
w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
String Matching
It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how
close is "almost"?
There are lots of techniques for approximate word matching. One is to determine the best substring match, which is the number of
common letters when the words are compared letter-byletter.
The key to this approach is that the words can overlap in any way. For example, consider the words CAPILLARY and MARSUPIAL.
One way to compare them is to overlay them:
CAPILLARY
MARSUPIAL
There is only one common letter (A). Better is the following overlay:
CAPILLARY
MARSUPIAL
22
25
with two common letters (A and R), but the best is:
CAPILLARY
MARSUPIAL
Which has three common letters (P, I and L).
The approximation measure appx(word1, word2) for two words is given by:
common letters * 2
----------------------------length(word1) + length(word2)
Thus, for this example, appx(CAPILLARY, MARSUPIAL) = 6 / (9 + 9) = 1/3. Obviously, for any word W appx(W, W) = 1, which is
a nice property, while words with no common letters have an appx value of 0.
Input:
The input for your program will be a series of words, two per line, until the end-of-file flag of -1.
Using the above technique, you are to calculate appx() for the pair of words on the line and print the result. For example:
CAR CART
TURKEY CHICKEN
MONEY POVERTY
ROUGH PESKY
AA
-1
The words will all be uppercase.
Output:
Print the value for appx() for each pair as a reduced fraction, like this:
appx(CAR,CART) = 6/7
appx(TURKEY,CHICKEN) = 4/13
appx(MONEY,POVERTY) = 1/3
appx(ROUGH,PESKY) = 0
appx(A,A) = 1
Fractions reducing to zero or one should have no denominator.
Self Numbers
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define
d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5
23
= 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)),
d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1
= 57, and so you generate the sequence
33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...
The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of
57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no
generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.
Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line.
Sample Output
1
3
5
7
9
20
31
42
53
64
|
| <-- a lot more numbers
|
9903
9914
9925
9927
9938
9949
9960
9971
9982
24
9993
|
|
|
26
DNA Sorting
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For
instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one
letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion
(E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be--exactly the reverse of
sorted).
You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However,
you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the
strings are of the same length.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (1 < m
<= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output
Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in
the same order they are in the input file.
Sample Input
1
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
25
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
27
Inversion
Let { A1,A2,...,An } be a permutation of the set{ 1,2,..., n}. If i < j and Ai > Aj then the pair (Ai,Aj) is called an "inversion" of the
permutation. For example, the permutation {3, 1, 4, 2} has three inversions: (3,1), (3,2) and (4,2).
The inversion table B1,B2,...,Bn of the permutation { A1,A2,...,An } is obtained by letting Bj be the number of elements to the left of
j that are greater than j. (In other words, Bj is the number of inversions whose second component is j.) For example, the permutation:
{ 5,9,1,8,2,6,4,7,3 }
has the inversion table
236402210
since there are 2 numbers, 5 and 9, to the left of 1; 3 numbers, 5, 9 and 8, to the left of 2; etc.
Perhaps the most important fact about inversions is Marshall Hall's observation that an inversion table uniquely determines the
corresponding permutation. So your task is to convert a permutation to its inversion table, or vise versa, to convert from an inversion
table to the corresponding permutation.
Input:
The input consists of several test cases. Each test case contains two lines.
The first line contains a single integer N ( 1 <= N <= 50) which indicates the number of elements in the permutation/invertion table.
The second line begins with a single charactor either 'P', meaning that the next N integers form a permutation, or 'I', meaning that the
next N integers form an inversion table.
Following are N integers, separated by spaces. The input is terminated by a line contains N=0.
Output:
For each case of the input output a line of intergers, seperated by a single space (no space at the end of the line). If the input is a
permutation, your output will be the corresponding inversion table; if the input is an inversion table, your output will be the
corresponding permutation.
Sample Input:
26
9
P591826473
9
I236402210
0
Sample Output:
236402210
591826473
Deck
28
Scenario
A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table's edge, and half the
length of the card hangs over the edge of the table. If the card hung any further out, with its center of gravity off the table, it would fall
off the table and flutter to the floor. The same reasoning applies if the card were placed on another card, rather than on a table.
Two playing cards can be arranged, carefully, with short edges parallel to table edges, to extend 3/4 of a card length beyond the edge
of the table. The top card hangs half a card length past the edge of the bottom card. The bottom card hangs with only 1/4 of its length
past the table's edge. The center of gravity of the two cards combined lies just over the edge of the table.
Three playing cards can be arranged, with short edges parallel to table edges, and each card touching at most one other card, to extend
11/12 of a card length beyond the edge of the table. The top two cards extend 3/4 of a card length beyond the edge of the bottom card,
and the bottom card extends only 1/6 over the table's edge; the center of gravity of the three cards lines over the edges of the table.
If you keep stacking cards so that the edges are aligned and every card has at most one card above it and one below it, how far out can
4 cards extend over the table's edge? Or 52 cards? Or 1000 cards? Or 99999?
Input
Input contains several nonnegative integers, one to a line. No integer exceeds 99999.
Output
The standard output will contain, on successful completion of the program, a heading:
# Cards Overhang
(that's two spaces between the words) and, following, a line for each input integer giving the length of the longest overhang achievable
with the given number of cards, measured in cardlengths, and rounded to the nearest thousandth. The length must be expressed with at
least one digit before the decimal point and exactly three digits after it. The number of cards is right-justified in column 5, and the
decimal points for the lengths lie in column 12.
Sample Input
27
29
1
2
3
4
30
Sample Output
The line of digits is intended to guide you in proper output alignment, and is not part of the output that your solution should produce.
12345678901234567
# Cards Overhang
1 0.500
2 0.750
3 0.917
4 1.042
30
1.997
IBM Minus One
You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In
it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is
controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the
crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)
After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some
thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace
every letter in the word HAL by its successor in the alphabet, you get ... IBM.
Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.
Input
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one
string of at most 50 upper-case letters.
Output
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived
from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.
Print a blank line after each test case.
Sample Input
28
2
HAL
SWERC
Sample Output
String #1
IBM
String #2
TXFSD
30
Floppies
In this age of Internet, on-line connections, instantaneous email, etc., there are still some people who need to work with floppy disks.
John is one of those. Every evening he goes home and continues what he has been doing in his office on his private UNIX system. He
copies all the files he needs to floppy disks according to the following procedure:
1. Put all files in one big SHAR file.
2. Compress the file.
3. Uuencode it, such that it is split in nice lines of 62 characters each (including the new-line).
4. Split it in files of 30,000 lines each (about 1.86Mb).
5. Compress each of the files and put it on a floppy by itself.
So far, this procedure always worked, since 1.86Mb of uuencoded text, after compression, will nicely fit on a 1.44Mb floppy disk.
Now, given that through compression the size of the SHAR file halves and that uuencoding a compressed file adds 50% to its size
(each rounded to the nearest integer number of bytes), we would like to know for a given size of the SHAR file how many floppies
John needs.
Input Specification
The input contains several test cases. Each test case consists of a single line containing one integer s (0 <= s <= 1,000,000,000),
specifying the size of the SHAR file in bytes.
The file ends with a file having s = 0. Do not generate output for this file.
Output Specification
For each test case, first output the number of the test case ('File #1', 'File #2', etc.), followed by a line that contains the minimal
number of floppies needed for the transfer and a blank line. Adhere to the format shown below in the sample output.
Sample Input
1000000
10000000
29
100000000
0
Sample Output
File #1
John needs 1 floppies.
File #2
John needs 5 floppies.
File #3
John needs 41 floppies.
31
Fake Tickets
Your school organized a big party to celebrate your team brilliant win in the prestigious, worldfamous ICPC (International Collegiate
Poetry Contest). Everyone in your school was invited for an evening which included cocktail, dinner and a session where your team
work was read to the audience. The evening was a success - many more people than you expected showed interested in your poetry although some critics of yours said it was food rather than words that attracted such an audience.
Whatever the reason, the next day you found out why the school hall had seemed so full: the school director confided he had
discovered that several of the tickets used by the guests were fake. The real tickets were numbered sequentially from 1 to N (N <=
10000). The director suspects some people had used the school scanner and printer from the Computer Room to produce copies of the
real tickets. The director gave you a pack with all tickets collected from the guests at the party's entrance, and asked you to determine
how many tickets in the pack had 'clones', that is, another ticket with the same sequence number.
Input
The input contains data for several test cases. Each test case has two lines. The first line contains two integers N and M which indicate
respectively the number of original tickets and the number of persons attending the party (1 <= N <= 10000 and 1 <= M <= 20000).
The second line of a test case contains M integers Ti representing the ticket numbers in the pack the director gave you (1 <= Ti <= N).
The end of input is indicated by N = M = 0.
Output
For each test case your program should print one line, containing the number of tickets in the pack that had another ticket with the
same sequence number.
Sample Input
55
33124
30
6 10
6136642312
00
Sample Output
1
4
32
33
Big Number
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of
data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the
number.
Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested,
followed by n lines, one integer 1 <= n <= 10^7 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
2
10
20
Sample Output
7
19
Lot
Out of N soldiers, standing in one line, it is required to choose several to send them scouting.
In order to do that, the following operation is performed several times: if the line consists of more than three soldiers, then all soldiers,
standing on even positions, or all soldiers, standing on odd positions, are taken away. The above is done until three or less soldiers are
left in the line. They are sent scouting. Find, how many different groups of three scouts may be created this way.
Note: Groups with less than three number of soldiers are not taken into consideration.
0 < N <= 10 000 000
Input
The input file contains the number N.
31
34
Process to the end of file.
Output
The output file must contain the solution - the amount of variants.
Sample Input
10
4
Sample Output
2
0
Stripies
Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian polosatiki, but the scientists had to invent an English name to apply for an international patent). The stripies are transparent
amorphous amebiform creatures that live in flat colonies in a jelly-like nutrient medium. Most of the time the stripies are moving.
When two of them collide a new stripie appears instead of them. Long observations made by our scientists enabled them to establish
that the weight of the new stripie isn't equal to the sum of weights of two disappeared stripies that collided; nevertheless, they soon
learned that when two stripies of weights m1 and m2 collide the weight of resulting stripie equals to 2*sqrt(m1*m2). Our chemical
biologists are very anxious to know to what limits can decrease the total weight of a given colony of stripies.
You are to write a program that will help them to answer this question. You may assume that 3 or more stipies never collide together.
Input
The first line of the input file contains one integer N (1 <= N <= 100) - the number of stripies in a colony. Each of next N lines
contains one integer ranging from 1 to 10000 - the weight of the corresponding stripie.
Process to the end of file.
Output
The output file must contain one line with the minimal possible total weight of colony with the accuracy of three decimal digits after
the point.
Sample Input
2
72
50
3
72
32
30
50
Sample Output
120.000
120.000
35
36
GCD & LCM
Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
1) p and q are positive integers;
2) GCD(p, q) = x;
3) LCM(p, q) = y.
Input
x and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
PreQueL
In some simplistic DBMS named PreQueL, the only column type allowed is CHAR(1) (a single character), and furthermore, its values
are restricted to English upper-case letters ('A' to 'Z'). Table may contain up to 9 columns, numbered from 1 to 9. Tables themselves
are named with lower-case English letters ('a' to 'z').
The only database query possible first joins all the tables, then selects some rows according to conditions in one of two forms: either
<column>=<value> or <column1>=<column2>, for example a2=A or b1=c4. All conditions must hold simultaneously, as if they were
connected by 'AND' operator.
You must write a PreQueL processor, which, given a tables and a set of conditions, will produce query result, i.e. those rows of a join
satisfying all the conditions. Resulting rows must be sorted alphabetically.
Input
The first line of input file contains of two integers - number of tables T and number of conditions D.
Starting from the second line there are T tables represented with number of rows RN and number of columns CN in the first line of a
33
37
table, which is followed by RN lines consisting of exactly CN characters each. D lines with conditions follow the whole set of tables.
The constraints are: 1 <= T <= 26, 1 <= D <= 50, 1 <= CN <= 9, 1 <= RN <= 1000.
Process to the end of file.
Output
Output file contains result rows, one row per line. No input query will produce more than 1000 rows. The result rows are to be sorted
alphabetically.
Sample Input
22
32
AX
BX
BY
23
ACD
BCC
a1=b1
a2=X
Sample Output
AXACD
BXBCC
Hamming Problem
For each three prime numbers p1, p2 and p3, let's define Hamming sequence Hi(p1, p2, p3), i=1, ... as containing in increasing order
all the natural numbers whose only prime divisors are p1, p2 or p3.
For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ...
So H5(2, 3, 5)=6.
Input
In the single line of input file there are space-separated integers p1 p2 p3 i.
Process to the end of file.
Output
The output file must contain the single integer - Hi(p1, p2, p3). All numbers in input and output are less than 10^18.
Sample Input
34
7 13 19 100
Sample Output
26590291
38
39
Multiplication Puzzle
The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one
card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards
on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards
are left in the row.
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.
Input
The first line of the input file contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from
1 to 100, separated by spaces.
Process to the end of file.
Output
Output file must contain a single integer - the minimal score.
Sample Input
6
10 1 50 50 20 5
Sample Output
3650
Count the Colors
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
35
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color,
they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
5
044
031
342
022
023
4
011
341
132
131
6
010
121
231
120
230
121
Sample Output
11
21
36
31
11
02
11
40
41
Circuit Board
On the circuit board, there are lots of circuit paths. We know the basic constrain is that no two path cross each other, for otherwise the
board will be burned.
Now given a circuit diagram, your task is to lookup if there are some crossed paths. If not find, print "ok!", otherwise "burned!" in one
line.
A circuit path is defined as a line segment on a plane with two endpoints p1(x1,y1) and p2(x2,y2).
You may assume that no two paths will cross each other at any of their endpoints.
Input
The input consists of several test cases. For each case, the first line contains an integer n(<=2000), the number of paths, then followed
by n lines each with four float numbers x1, y1, x2, y2.
Output
If there are two paths crossing each other, output "burned!" in one line; otherwise output "ok!" in one line.
Sample Input
1
0011
2
0011
0110
Sample Output
ok!
burned!
Author: ZHOU, Feng
Machined Surfaces
An imaging device furnishes digital images of two machined surfaces that eventually will be assembled in contact with each other.
The roughness of this final contact is to be estimated.
A digital image is composed of the two characters, "X" and " " (space). There are always 25 columns to an image, but the number of
rows, N, is variable. Column one (1) will always have an "X" in it and will be part of the left surface. The left surface can extend to
37
the right from column one (1) as contiguous X's.
Similarly, column 25 will always have an "X" in it and will be part of the right surface. The right surface can extend to the left from
column 25 as contiguous X's.
Digital-Image View of Surfaces
Left
Right
XXXX
XXXXX
XXX
XXXXXXX
XXXXX
XXXX
XX
XXXXXX
.
.
.
.
.
.
XXXX
XXXX
XXX
XXXXXX
1
25
In each row of the image, there can be zero or more space characters separating the left surface from the right surface. There will
never be more than a single blank region in any row.
For each image given, you are to determine the total ``void" that will exist after the left surface has been brought into contact with the
right surface. The ``void" is the total count of the spaces that remains between the left and right surfaces after theyhave been brought
into contact.
The two surfaces are brought into contact by displacing them strictly horizontally towards each other until a rightmost "X" of the left
surface of some row is immediately to the left of the leftmost "X" of the right surface of that row. There is no rotation or twisting of
these two surfaces as they are brought into contact; they remain rigid, and only move horizontally.
Note: The original image may show the two surfaces already in contact, in which case no displacement enters into the contact
roughness estimation.
Input
The input consists of a series of digital images. Each image data set has the following format:
First line A single unsigned integer, N, with value greater than zero (0) and less than 13. The first digit of N will be the first character on a line.
Next N lines Each line has exactly 25 characters; one or more X's, then zero or more spaces, then one or more X's.
38
42
The end of data is signaled by a null data set having a zero on the first line of an image data set and no further data.
Output
For each image you receive as a data set, you are to reply with the total void (count of spaces remaining after the surfaces are brought
into contact). Use the default output for a single integer on a line.
Sample Input
(character "B" for ease of reading. The actual input file will use the ASCII-space character, not "B").
4
XXXXBBBBBBBBBBBBBBBBXXXXX
XXXBBBBBBBBBBBBBBBXXXXXXX
XXXXXBBBBBBBBBBBBBBBBXXXX
XXBBBBBBBBBBBBBBBBBXXXXXX
2
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXBBBBBBBBBBBBBBXX
0
Sample Output
4
0
0
Word Index
Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a
simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.
Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic
order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word
are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,
abc aep gwz
are all valid three-letter words, whereas
aab are cat
are not.
39
For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:
a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681
Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long.
For each word read, if the word is invalid give the number 0. If the word read is valid, give the word's position index in the above
alphabetical list.
Input
The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only
the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an
input line.
The input will be terminated by end-of-file.
Output
The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should
be the first character on a line. There is one line of output for each input line.
Sample Input
z
a
cat
vwxyz
40
Sample Output
26
1
0
83681
43
44
The Drunk Jailer
A certain prison contains a long hall of n cells, each right next to each other. Each cell has a prisoner in it, and each cell is locked.
One night, the jailer gets bored and decides to play a game. For round 1 of the game, he takes a drink of whiskey, and then runs down
the hall unlocking each cell. For round 2, he takes a drink of whiskey, and then runs down the hall locking every other cell (cells 2, 4,
6, …). For round 3, he takes a drink of whiskey, and then runs down the hall. He visits every third cell (cells 3, 6, 9, …). If the cell is
locked, he unlocks it; if it is unlocked, he locks it. He repeats this for n rounds, takes a final drink, and passes out.
Some number of prisoners, possibly zero, realizes that their cells are unlocked and the jailer is incapacitated. They immediately
escape.
Given the number of cells, determine how many prisoners escape jail.
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a
single integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
Sample Input
2
5
100
Sample Output
2
10
Number Base Conversion
Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:
{ 0-9,A-Z,a-z }
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to
the original base, you should get the original number.
41
Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a
(decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and
the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, …, Z = 35, a = 36, b = 37, …, z = 61 (0-9 have
their usual meanings).
Output
The output of the program should consist of three lines of output for each base conversion performed. The first line should be the
input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should
be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.
Sample Input
8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030
Sample Output
62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001
10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A
35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05
23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj
49 1VbDkSIMJL3JjRgAdlUfcaWj
42
45
61 dl9MDSWqwHjDnToKcsWE1S
61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030
5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890
Unimodal Palindromic Decompositions
A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease up to the middle value and then (since the sequence is
palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal
Palindromic while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the
sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3), (1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1), (1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2), (1 1 2 2 1 1), (1 1 1 1 1 1 1 1)
Write a program, which computes the number of Unimodal Palindromic Decompositions of an integer.
Input
Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.
Output
For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal
Palindromic Decompositions of the input value.
Sample Input
2
3
43
4
5
6
7
8
10
23
24
131
213
92
0
Sample Output
22
32
44
53
67
75
8 11
10 17
23 104
24 199
131 5010688
213 1055852590
92 331143
说明:
建议学生利用假期时间完成上述题目中的 20——30 道题。需要利用专门的作业本完成,标明题号。如果能够上机调试,整理好源
程序代码。
本次作业作为开学选拔学生参赛选手的其中一个重要依据。
44