Section 1.1
Your Ride Is Here
Greedy Gift Givers
Broken Necklace
Friday the Thirteenth
It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular group's turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.
Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47 is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)
Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing "GO" if they match and "STAY" if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.
Examples:
Input | Output |
COMETQ HVNGAT |
GO |
ABSTAR USACO |
STAY |
Line 1: | An upper case character string of length 1..6 that is the name of the comet. |
Line 2: | An upper case character string of length 1..6 that is the name of the group. |
NOTE: The input file has a newline at the end of each line but does not have a "return". Sometimes, programmers code for the Windows paradigm of "return" followed by "newline"; don't do that! Use simple input routines like "readln" (for Pascal) and, for C/C++, "fscanf" and "fid>>string".
NOTE 2: Because of the extra characters, be sure to leave enough room for a 'newline' (also notated as '\n') and an end of string character ('\0') if your language uses it (as C and C++ do). This means you need eight characters of room instead of six.
COMETQ HVNGAT
GO
C | O | M | E | T | Q | |
3 | 15 | 13 | 5 | 20 | 17 | |
H | V | N | G | A | T | |
8 | 22 | 14 | 7 | 1 | 20 |
3 * 15 * 13 * 5 * 20 * 17 = 994500 mod 47 = 27 8 * 22 * 14 * 7 * 1 * 20 = 344960 mod 47 = 27
Because both products evaluate to 27 (when modded by 47), the mission is 'GO'.
/*
ID: mcdonne1
PROG: ride
LANG: C++
*/
#include
#include
#include
using namespace std;
int main()
{
ofstream fout ("ride.out");
ifstream fin ("ride.in");
string a,b;
while(fin>>a>>b)
{
int suma=1,sumb=1;
for(int i=0;i
A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than you might expect. Each person sets aside a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 1 left over -- that 1 left over stays in the giver's "account".
In any group of friends, some people are more giving than others (or at least may have more acquaintances) and some people have more money than others.
Given a group of friends, no one of whom has a name longer than 14 characters, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts, determine how much more (or less) each person in the group gives than they receive.
The grader machine is a Linux machine that uses standard Unix conventions: end of line is a single character often known as '\n'. This differs from Windows, which ends lines with two characters, '\n' and '\r'. Do not let your program get trapped by this!
Line 1: | The single integer, NP | |||
Lines 2..NP+1: | Each line contains the name of a group member | |||
Lines NP+2..end: | NP groups of lines organized like this:
|
5 dave laura owen vick amr dave 200 3 laura owen vick owen 500 1 dave amr 150 2 vick owen laura 0 2 amr vick vick 0 0
The output is NP lines, each with the name of a person followed by a single blank followed by the net gain or loss (final_money_value - initial_money_value) for that person. The names should be printed in the same order they appear starting on line 2 of the input.
All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible that meets this constraint. Any money not given is kept by the giver.
dave 302 laura 66 owen -359 vick 141 amr -150
dave | laura | owen | vick | amr |
0 | 0 | 0 | 0 | 0 |
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over | ||||
-200+2 | 66 | 66 | 66 | 0 |
Second, 'owen' gives 500 to 'dave': | ||||
-198+500 | 66 | 66-500 | 66 | 0 |
Third, 'amr' splits 150 between 'vick' and 'owen': | ||||
302 | 66 | -434+75 | 66+75 | -150 |
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes: | ||||
302 | 66 | -359 | 141 | -150 |
Finally, 'vick' gives 0 to no one: | ||||
dave | laura | owen | vick | amr |
302 | 66 | -359 | 141 | -150 |
/*
ID: mcdonne1
PROG: gift1
LANG: C++
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
int n;
fin >> n ;
char *a0 = new char[n*18];
char **a;
a = new char *[n];
for(int i=0;i>a[i];
}
for(int i=0;i<18;i++)
{
c[i]='\0';
}
fin>>c;
while(c[0]!='\0')
{
int sum,n2;
fin>>sum>>n2;
if(n2!=0)
{
char *d0 = new char[n2*18];
char **d;
d = new char *[n2];
for(int i=0;i>d[i];
}
for(int i=0;i>c;
}
for(int i=0;i
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
Note that the start year is NINETEEN HUNDRED, not 1990.
There are few facts you need to know before you can solve this problem:
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
20
36 33 34 33 35 35 34
/*
ID: mcdonne1
PROG: friday
LANG: C++
*/
#include
#include
#include
#include
#include
using namespace std;
int n;
int friday[8];
int date;
int yue[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
ifstream fin("friday.in");
ofstream fout("friday.out");
date=0;
fin>>n;
for(int i=1900;i<=1899+n;i++)
{
if(i%100==0)
{
if(i%400==0) yue[2]=29;
else yue[2]=28;
}
else
{
if(i%4==0) yue[2]=29;
else yue[2]=28;
}
for(int j=1;j<=12;j++)
{
for(int k=1;k<=yue[j];k++)
{
date++;
if(date>7) date=1;
if(k==13) friday[date]++;
}
}
}
fout<
You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:
1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r b b b b b b r b r r b r b r r r b r r r r r r b r b r r r w Figure A Figure B r red bead b blue bead w white bead
The beads considered first and second in the text that follows have been marked in the picture.
The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).
Determine the point where the necklace should be broken so that the most number of beads can be collected.
For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.
In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can be collected from a supplied necklace.
Line 1: | N, the number of beads |
Line 2: | a string of N characters, each of which is r, b, or w |
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
11
Two necklace copies joined here v wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ******|***** rrrrrb|bbbbb <-- assignments 5xr .....#|##### 6xb 5+6 = 11 total
/*
ID: mcdonne1
PROG: beads
LANG: C++
*/
#include
#include
#include
#include
#include
using namespace std;
int main()
{
ifstream cin("beads.in");
ofstream cout("beads.out");
int before,after;
int n;
int p = 0;
char necklace[800];
char current_color;
int max = 0;
cin>>n;
cin>>necklace;
for(int i = 0;i < n;i ++)
necklace[i+n] = necklace[i];
necklace[n*2] = '\0';
before = 0;
while(necklace[p] == 'w')
{
before++;
p++;
}
current_color = necklace[p];
while((necklace[p] == current_color|| necklace[p] == 'w') && p max && before+after <= n)
{
max = before+after;
}
before = after;
int j = p-1-before;
while(necklace[j] == 'w')
{
before++;
j--;
}
}
}
else
max = n;
cout << max<