출처: https://train.usaco.org/usacogate


문제 설명

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 NINETEEN NINETY.

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.

입력 양식

One line with the integer N.

출력 양식

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

입력 예

20

출력 예

36 33 34 33 35 35 34


문제 풀이 내용

매달 13일이 요일마다 몇번씩 있었는지 세는 문제이다. 1900. 1. 1 이 월요일에서 시작하기 때문에 첫번째 13일이 토요일이 되는 것을 기준으로 각 달이 며칠인지 더해서 계산했다.

프로그램 내용

더보기
#define BEGIN_YEAR 1900
#define FIRST_DAY 2 // Jan. 1. 1990 - Monday
/// Sat=0, Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6
#define DAYS_IN_WEEK 7
#define MONTH_IN_YEAR 12
#define MAX_IN_YEAR 400

/*
Leap Year : year % 4 == 0 && year % 100 != 100 && year %400 == 0
*/
bool LeapYear (int inYear)
{
    bool LeapFlag = false;

    if (inYear % 4 == 0) { LeapFlag = true; }
    if (inYear % 100 == 0) { LeapFlag = false; }
    if (inYear % 400 == 0) { LeapFlag = true; }

    return LeapFlag;
};

int Days = 13 + FIRST_DAY - 1; /// 15 Mon - Sat

    for (int index = 0; index < InYear ; ++index)
    {
        int year = BEGIN_YEAR + index;
        if(!LeapYear (year))
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += DayInMonth[idx];
            }
        }
        else
        {
            for (int idx =0; idx< MONTH_IN_YEAR ; ++idx)
            {
                ++Thirteen[Days%7];
                Days += LeapDayInMonth[idx];
            }
        }
    }

    for (int index=0; index<DAYS_IN_WEEK ; ++index)
    {
        fout << Thirteen[index];
        if(index < 6)
            fout << " ";
    }

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

출처: https://train.usaco.org/usacogate


문제 설명

Task 'gift1': 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 some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give.

The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get 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 7 among 2 friends would be 3 each for the friends with 1 left over – that 1 left over goes into the giver's "account". All the participants' gift accounts start at 0 and are decreased by money given and increased by money received.

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 money each person ends up with.

입력 양식

Line # Contents
1 A single integer, NP
2..NP+1 Line i+1 contains the name of group member i
NP+2..end NP groups of lines organized like this:
The first line of each group tells the person's name who will be giving gifts.
The second line in the group contains two numbers:
The amount of money (in the range 0..2000) to be divided into gifts by the giver
NGi (0 ≤ NGi ≤ NP), the number of people to whom the giver will give gifts
If NGi is nonzero, each of the next NGi lines lists the name of a recipient of a gift; recipients are not repeated in a single giver's list.

출력 양식

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.

입력 예

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

출력 예

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


문제 풀이 내용

각 인원마다 선물을 주고 받은 금액을 더하고 빼줘야 한다. 다만, 나머지 부분을 주는 사람이 가지게 하면 된다.

간단하게 이름/금액을 가지는 클래스를 만들고, 이름으로 검색해서 금액을 조정해주도록 했다.

프로그램 내용

더보기
class GiftMap
{
public:
    string Names;
    int gifts;
};

class GiftMap Party[MAXPEOPLE];

/* look for name in people table */
class GiftMap* lookup(string inName)
{
    for(int i=0; i<MAXPEOPLE; ++i)
	if(inName == Party[i].Names)
	    return &Party[i];
}
...
    for(int index=0;index < size ; ++index)
    {
        string gName;
        fin >> gName;
        int amount, targets;
        fin >> amount >> targets ;

		// Reduce Giver's amount
        class GiftMap *giver;
        giver = lookup(gName);

        if(targets)
        {
            giver->gifts -= amount;
            giver->gifts += amount%targets;
        }

 

 

Chapter 1. Getting started

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

출처: https://train.usaco.org/usacogate


문제 설명

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  GO 
HVNGAT

ABSTAR  STAY
USACO 

입력 양식

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.

출력 양식

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

입력 예

COMETQ 
HVNGAT

출력 예

GO


문제 풀이 내용

기본적인 시스템 적응 문제이다. 파일 입출력으로 자료를 읽어 들이고, 결과를 기록하면 된다. 특별한 알고리즘이 필요하지는 않고, 문제에서 지시한 내용대로 알파벳을 숫자로 변환해서, 처리하면 된다.

프로그램 내용

더보기
...
    string a, b;
    fin >> a >> b;    
...
	for(int index=0; index< a.size(); ++index)
        sum_1 *= (int)(a[index]-'A') + 1;

    for(int index=0; index< b.size(); ++index)
        sum_2 *= (int)(b[index]-'A') + 1;
... 
	/// compare sum_1 % 47 vs sum_2 % 47

 

Chapter 1. Getting started

 

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
USACO Training - Chapter 1. Getting started  (0) 2019.09.06

USACO Training Site problems ( link )

 

Chapter 1. Getting started

 

Section 1.1 Introduction

section 1.1 Text - Introduction

 

Section 1.2 Submitting Soln, Task Types, Ad Hoc 
section 1.2.1 Text - submitting solution 
section 1.2.2 prob - Your Ride is Here  (1
section 1.2.3 Text - contest problem type 

section 1.2.4 Text - ad hoc problem 

section 1.2.5 prob - Greedy Gift Givers (1) (2)

section 1.2.6 prob - Friday the Thirteenth (1) 
section 1.2.7 prob - Broken Necklace (1) (?)

 

Section 1.3 Complete Search 
section 1.3.1 Text - Complete Search
section 1.3.2 prob - Milking Cow (1)
section 1.3.3 prob - Transformations (1)
section 1.3.4 prob - Name That Numbers (1)
section 1.3.5 prob - Palindromic Squares (1)
section 1.3.6 prob - Dual Palindromes (1)


Section 1.4 Greedy, Crafting Solutions 
section 1.4.1 Text - Greedy Algorithm
section 1.4.2 prob - Mixing Milk (1)
section 1.4.3 prob - Barn Repair (1)
section 1.4.4 Text - Winning Solutions
section 1.4.5 prob - Prime Cryptarithm (1)
section 1.4.6 prob - Combination Lock (1) (2)
section 1.4.7 prob - Wormholes (1) (?)
section 1.4.8 prob - Ski Course Design (1) (?)

 

Section 1.5 More Search Techniques 

section 1.5.1 Text - More Search Techniques 

section 1.5.2 prob - Arithmetic Progressions (1)
section 1.5.3 prob - Mother's Milk (1)

 

Section 1.6 Binary Numbers

section 1.6.1 Text - Introduction to Binary Numbers 
section 1.6.2 prob - Number Triangles (1)
section 1.6.3 prob - Prime Palindromes (1)
section 1.6.4 prob - Super Prime Rib (1)

 

'USACO Training' 카테고리의 다른 글

Problem 1.3.2 Milking Cow  (0) 2019.09.07
Problem 1.2.7 Broken Necklace  (0) 2019.09.07
Problem 1.2.6 Friday the Thirteenth  (0) 2019.09.07
Problem 1.2.5 Greedy Gift Givers  (0) 2019.09.07
Problem 1.2.2 Ride  (0) 2019.09.07

+ Recent posts