C++——USACO Section 1.1 题解

44 篇文章 0 订阅

PS:大部分为原创,少部分代码为转载。

Section 1.1

  Your Ride Is Here

Greedy Gift Givers

Broken Necklace

Friday the Thirteenth


Your Ride Is Here

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:

InputOutput
COMETQ
HVNGAT
GO
ABSTAR
USACO 
STAY

PROGRAM NAME: ride

This means that you fill in your header with:
PROG: ride  
WARNING:  You must have 'ride' in this field or the wrong test data (or no test data) will be used.

INPUT FORMAT

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.

SAMPLE INPUT (file ride.in)

COMETQ
HVNGAT

OUTPUT FORMAT

A single line containing either the word "GO" or the word "STAY".

SAMPLE OUTPUT (file ride.out)

GO

OUTPUT EXPLANATION

Converting the letters to numbers:
COMETQ 
3151352017 
HVNGAT
822147120 
then calculate the product mod 47:
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 <iostream>  
#include <fstream>  
#include <string>  
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.size();i++)  
    {  
        suma*=(a[i]-'A'+1);  
    }  
    suma%=47;  
    for(int i=0;i<b.size();i++)  
    {  
        sumb*=(b[i]-'A'+1);  
    }  
    sumb%=47;  
    if(suma==sumb)  
        fout<<"GO"<<endl;  
    else  
        fout<<"STAY"<<endl;  
    }  
    return 0;  
}

Greedy Gift Givers

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.

IMPORTANT NOTE

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!

PROGRAM NAME: gift1

INPUT FORMAT

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:
The first line in the group tells the person's name who will be giving gifts.
The second line in the group contains two numbers: The initial amount of money (in the range 0..2000) to be divided up into gifts by the giver and then the number of people to whom the giver will give gifts, NGi (0 ≤ NGi ≤ NP-1).
If NGi is nonzero, each of the next NGi lines lists the the name of a recipient of a gift.

SAMPLE INPUT (file gift1.in)

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

OUTPUT FORMAT

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.

SAMPLE OUTPUT (file gift1.out)

dave 302
laura 66
owen -359
vick 141
amr -150

OUTPUT EXPLANATION

Five names: dave, laura, owen, vick, amr Let's keep a table of the gives (money) each person 'has':
davelauraowenvickamr
00000
First, 'dave' splits 200 among 'laura', 'owen', and 'vick'. That comes to 66 each, with 2 left over
-200+26666660
Second, 'owen' gives 500 to 'dave':
-198+5006666-500660
Third, 'amr' splits 150 between 'vick' and 'owen':
30266-434+7566+75-150
Fourth, 'laura' splits 0 between 'amr' and 'vick'; no changes:
30266-359141-150
Finally, 'vick' gives 0 to no one:
davelauraowenvickamr
30266-359141-150



/*
ID: mcdonne1
PROG: gift1
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<ctime>
#include<cmath>
#include<fstream>
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<n;++i)
    a[i] = a0 + i*18;
    char c[18];int *b=new int [n];
    for(int i=0;i<n;i++)
    {
        b[i]=0;
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<18;j++)
        {
            a[i][j]='\0';
        }
    for (int i=0;i<n;i++)
    {
        fin>>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<n2;++i)
            d[i] = d0 + i*18;
            for(int i=0;i<n2;i++)
            for(int j=0;j<18;j++)
            {
                d[i][j]='\0';
            }
            for(int i=0;i<n2;i++)
            {
                fin>>d[i];
            }
            for(int i=0;i<n2;i++)
            {
                for(int j=0;j<n;j++)
                {
                if(strcmp(a[j],d[i])==0)
                    {
                        b[j]+=(sum/n2);

                    }
                }
            }
            for(int j=0;j<n;j++)
            {
                if(strcmp(a[j],c)==0)
                {
                    b[j]-=sum;
                    b[j]+=sum%n2;
                }
            }
            delete[] d;
            delete [] d0;
        }
        for(int i=0;i<18;i++)
        {
            c[i]='\0';
        }
        fin>>c;
    }

    for(int i=0;i<n;i++)
    {
        fout<<a[i]<<" "<<b[i]<<endl;
    }

    delete[] a;
    delete b;
    return 0;
}


Friday the Thirteenth

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:

  • January 1, 1900 was on a Monday.
  • Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
  • Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
  • The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34


/* 
ID: mcdonne1 
PROG: friday 
LANG: C++ 
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<fstream>
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<<friday[6]<<" "<<friday[7]<<" "<<friday[1]<<" "<<friday[2]<<" "<<friday[3]<<" "<<friday[4]<<" "<<friday[5]<<endl;
	return 0;
}


Broken Necklace

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.

Example

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.

PROGRAM NAME: beads

INPUT FORMAT

Line 1:N, the number of beads
Line 2:a string of N characters, each of which is r, b, or w

SAMPLE INPUT (file beads.in)

29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT

A single line containing the maximum of number of beads that can be collected from the supplied necklace.

SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION

Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
                Two necklace copies joined here
                             v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                       ******|*****
                       rrrrrb|bbbbb  <-- assignments
                   5xr .....#|#####  6xb

                        5+6 = 11 total


/* 
ID: mcdonne1 
PROG: beads 
LANG: C++ 
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<algorithm>
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<n)
    {
        before++;
        p++;
    }
    if(p < n)
    {
       while(p < 2*n)
        {
            current_color = necklace[p];
            after = 0;
            while(necklace[p] == current_color|| necklace[p] == 'w')
            {
                after++;
                p++;
            }
            if(before + after > 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<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值