USACO Solution Code(2)

黑色星期五(Friday the thirteenth)的程序。這些程序其實都很簡單,沒有很複雜的算法在裏面,追求極致的Geek們可以從代碼的長度、優雅簡潔等方面下功夫了,俺僞Geek一個,寫了個程序能通過,就不糾結了,哈哈…

第二題有點小麻手,關鍵是將兩條鏈子拼湊在一起,Problem Description的最後也提示了這一點。

 

/*

ID: fairyroad

PROG: friday

LANG: C++

*/

 

#include <iostream>

#include <fstream>

using namespace std;

 

inline bool IsLeapyear(int year)

{

               return (year%4==0 && year%100!=0 || year%400==0) ? true : false;

}

 

int Leapyear[12]={31,29,31,30,31,30,31,31,30,31,30,31};

int Averageyear[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int result[7];

 

int date=0; // the day associated with 13-th of the month

int year=1900;

int preSurplus=0; // number of days left from 13-th of last month

 

int main()

{

               ofstream fout ("friday.out");

               ifstream fin ("friday.in");

 

               int n;

               fin>>n;

 

               for(int i=0;i<n;i++)

               {

                               bool flag=IsLeapyear(year);

                               for(int j=0;j<12;j++)

                               {

                                              date=((preSurplus+13)%7+date)%7;

                                              ++result[date];

                                              preSurplus=flag?Leapyear[j]-13:Averageyear[j]-13;

                               }

                               ++year;

               }

 

               fout<<result[6]<<" "; //Saturday

               fout<<result[0]<<" "; //Sunday

               fout<<result[1]<<" ";

               fout<<result[2]<<" ";

               fout<<result[3]<<" ";

               fout<<result[4]<<" ";

               fout<<result[5]<<endl;

 

               return 0;

}

 

 

/*

ID: fairyroad

LANG:C++

TASK:beads

*/

#include<fstream>

#include<string>

#include<vector>

using namespace std;

int main()

{

               ifstream fin("beads.in");

               ofstream fout("beads.out");

 

               int len;

               fin>>len;

               string str,myStr;

               fin>>str;

               myStr=str+str;

               int color=0;

               int patrol=0;

               int header=0;

               int max(0);

               int wconut(0);

               for(int i=0;(i<len*2)&&(header+patrol<len);++i)

               {

                               if (myStr[i]=='w'){

                                              wconut++;

                                              ++patrol;

                               }

                               else if (myStr[i]==color){

                                              wconut=0;

                                              ++patrol;

                               }

                               else

                               {

                                              color=myStr[i];

                                              if(header+patrol>max) max=header+patrol;

                                              header=patrol-wconut;

                                              patrol=1+wconut;

                                              wconut=0;

                               }

 

               }

               if(max<(patrol+header))  fout<<patrol+header<<endl;

               else fout<<max<<endl;

}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章