sony go for it

sony go for it

q1.cpp


// 2012.2.13
// nekohand
// このプログラムはGPLに準拠します

#include
#include

namespace {
const int MonthsNum = 12;
const int DaysNumOfMonth[] =
{31,28,31, 30,31,30, 31,31,30, 31,30,31};
const int DaysNumOfMonthOfYear = 365;
const int LeapMonth = 2;
const int DaysNumOfLeapMonth = 29;
const int DaysNumOfLeapYear = 366;
}

// Input data
typedef struct Date {
int m_year;
int m_month;
int m_day;
} Date;

// Output data
typedef struct Time {
int m_hour;
int m_minute;
int m_second;
} Time;

bool IsLeapYear(int year) {
bool leap = false;
if (year%4==0) {
if (year%100==0) {
if (year%400==0) {
leap = true;
}
} else {
leap = true;
}
}
return leap;
}

// Days since January 1, regard of leap year and month.
int DaysSinceThisYear(const Date &present) {
int days = 0;
for(int month=0; month Time(Hour, Minute, Second)
Time Date2Time(const Date &birth, const Date &present, int lengthOfLife) {
int daysToPresent = HowManyDays(birth, present);

Date life;
life.m_year = birth.m_year + lengthOfLife;
life.m_month = birth.m_month;
life.m_day = birth.m_day; // regardless of leap day
int daysToLife = HowManyDays(birth, life) - 1;

double rate = static_cast(daysToPresent)/static_cast(daysToLife);

Time time;
int *times[3] = {&time.m_hour, &time.m_minute, &time.m_second};
const double scales[3] = {24.0, 24.0*60.0, 24.0*60.0*60.0};

for(int i=0; i<3; ++i) {
*times[i] = static_cast(rate * scales[i]);
rate -= static_cast(*times[i]) / scales[i];
assert(rate>=0.0 && rate<1.0);
}

return time;
}


int main () {
//------
// input
Date birth, present;
std::cout << "Input birth day" << std::endl;
std::cin >> birth.m_year >> birth.m_month >> birth.m_day;

std::cout << "Input present day" << std::endl;
std::cin >> present.m_year >> present.m_month >> present.m_day;

int lengthOfLife;
std::cout << "Input length of life" << std::endl;
std::cin >> lengthOfLife;

//------------
// calculation
Time time = Date2Time(birth, present, lengthOfLife);

//-------
// output
std::cout << "Today is " << std::endl;
std::cout << "hour:" << time.m_hour << " minute:" << time.m_minute << " second:" << time.m_second;

return 0;
}



q2

// 2012.2.13
// nekohand
// このプログラムはGPLに準拠します

#include
#include

int main() {
double real;
std::cout << "Input number";
std::cin >> real;

double result = 0.0;

int i=0;
double step = 1.0/10000.0;
while(true) {
double t = step * static_cast(++i);
double delta = step * exp(-t) * pow(t, real);
if (abs(delta)<=1.0e-16) {
break;
}
result += delta;
}

std::cout << result;

return 0;
}


q3.cpp
// 2012.2.13
// nekohand
// このプログラムはGPLに準拠します

#include
#include
#include
#include
#include

// Output data
typedef struct Answer {
int m_skip;
int m_index;

bool operator < (const Answer &rhs) {
return m_skip!=rhs.m_skip ? m_skip Search(std::string str, std::string key) {
std::vector answers;

const int strN = str.size();
const int keyN = key.size();

// i as skip 1 to strN-1
for(int i=1; i> inputFileName;
std::ifstream ifs(inputFileName);
if (ifs.bad()) {
std::cout << "Error: input file open" << std::endl;
return 0;
}

std::string outputFileName;
std::cout << "Output results file name" << std::endl;
std::cin >> outputFileName;
std::ofstream ofs(outputFileName);
if (ifs.bad()) {
std::cout << "Error: output file open" << std::endl;
return 0;
}


std::string str;
ifs >> str;

std::string key;
std::cout << "Input keyword" << std::endl;
std::cin >> key;


//---------
// Proccess
std::vector answers = Search(str, key);


//-------
// Output
std::sort(answers.begin(), answers.end());
for(int i=0; i