Подорожуй з gofrie.com

Робіть висновки

In ICPCs, coding should not be your bottleneck at all. That is, once you figure out the ‘worst
AC algorithm’ that will pass the given time limit, you are supposed to be able to translate it into
bug-free code and you can do it fast! Try to do some exercises below. If you need more than
10 lines of code to solve them, you will need to relearn your programming language(s) in depth!

Mastery of programming language routines will help you a lot in programming contests.

1. Given a string that represents a base X number, e.g. FF (base 16, Hexadecimal), convert it
to base Y, e.g. 255 (base 10, Decimal), 2 ≤ X, Y ≤ 36. (More details in Section 5.4.2).

2. Given a list of integers L of size up to 1M items, determine whether a value v exists in L?
(More details in Section 2.2.1).

3. Given a date, determine what is the day (Monday, Tuesday, ..., Sunday) of that date?

4. Given a long string, replace all the occurrences of a character followed by two consecutive
digits in with “***”, e.g. S = “a70 and z72 will be replaced, but aa24 and a872 will not” will
be transformed to S = “*** and *** will be replaced, but aa24 and a872 will not”.
  • +1
  • 05 березня 2014, 00:35
  • kutsyk
Автор має нагороди

Коментарі (1)

RSS згорнути / розгорнути
3. Given a date, determine what is the day (Monday, Tuesday, ..., Sunday) of that date?

зробив у одну строчку коду ;)

// d = [1..31]; m = [1..12]
// returns [1..7]: 1 for Monday,2 for Tuesday, ... ,7 for Sunday
const unsigned int getDayOfWeek(unsigned int y, unsigned int m, unsigned int d) 
{ 
	return static_cast<unsigned int>(((m<3?4+y--:y+2)+floor((double) 23*m/9)+d+floor((double) y/4) + floor(floor( (double) y/100)*25/4)) - 1) % 7 + 1;
}


а от і код тестування:
for (unsigned int i=10;i<=23;i++) 
{
	unsigned int year = 2014;
	unsigned int month = 3;
	cout<< year<< "." << month <<"."<<i<<": "<< '\t' << getDayOfWeek(year,month,i) << endl;
}

посилання:
Алгоритм має назву Zellers Algorithim, оптимізація Michael Keith
+1