Baidu A Star 2007 competition May 27

来源:互联网 发布:淘宝店食品安全许可证 编辑:程序博客网 时间:2024/05/03 01:34

My code as belo for Item one :

#include<iostream>
#include <vector>
#include "stdlib.h"

using namespace std;

bool IsLeap(int year)
{
 if( !year%4 && year%100) return true;
 if(!year%4 && !year%100 && !year%400) return true;
 return false;
}

int CalculateDays(int year, int month, int day)

 int day_tbl[][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; 
 int i_nLeaps = 1;
  int days = 0;
 if(year > 2000)
 {
  int i = year - 2000 -1;  
  i_nLeaps += (i/4) - i/100 + i/400; 
  days += i*364 + i_nLeaps;
  int j;
  if(IsLeap(year))
  {
     for(j=1; j<month; j++)
   days += day_tbl[0][j-1];
  days += day -1; 
  }
  else
  {
    for(j=1; j<month; j++)
   days += day_tbl[1][j-1];
  days += day -1; 

  }
 
 }
 if( year = 2000)
 {
  int j;
  for(j=1; j<month; j++)
   days += day_tbl[1][j];
  days += day -1;
 } 
  return days;
}

//bool JudgeValid(string year, string month, string day)
//{
// 
// if(str_year.compare("2000") < 0) return false;
// if(str_year.compare("9999") > 0)) return false;
// if(str_month.compare("01") < 0) return false;
// if(str_month.compare("12") > 0) return false;
// if(str_day.compare(""))
//
//  atol
//    
//}


int main()
{
 int n_year, n_month, n_day;
 vector<string> vs_input;
 char ch_buffer[200];
 string str1;
 while(cin.getline(ch_buffer, 200, '/x0a'))
 {
  str1 = ch_buffer;
  if(!str1.compare("")) break;
  vs_input.push_back(ch_buffer);
 }
 
 int n_InputNumbers;
 n_InputNumbers = vs_input.size();
 int i;
 string::size_type n_pos;
 string str_year, str_month, str_day;
 for(i=0; i<n_InputNumbers; i++)
 {
  //cout << vs_input[i].c_str() << endl;
  str1 = vs_input[i];
  
        if((n_pos = str1.find_first_of('-')) != string::npos)
  {
   str_year.assign(str1, 0, n_pos);
   str1.erase(0, n_pos+1);
   if((n_pos = str1.find_first_of('-')) != string::npos)
   {
    str_month.assign(str1, 0, n_pos);
    str1.erase(0, n_pos+1);
    str_day = str1;
   }
   
   if(
    (n_year = atoi(str_year.c_str()) ) == 0
    || 
    (n_month = atoi(str_month.c_str())) == 0
    ||
    (n_day = atoi(str_day.c_str())) == 0 )
    cout << "Error" << endl;
   else
    cout << CalculateDays(n_year, n_month, n_day) << endl;
        }
  else if((n_pos = str1.find_first_of('/')) != string::npos)
  {
   str_month.assign(str1, 0, n_pos);
   str1.erase(0, n_pos+1);
   if((n_pos = str1.find_first_of('/')) != string::npos)
   {
    str_day.assign(str1, 0, n_pos);
    str1.erase(0, n_pos+1);
    str_year = str1;    
   }
   
   if(
    (n_year = atoi(str_year.c_str()) ) == 0
    || 
    (n_month = atoi(str_month.c_str())) == 0
    ||
    (n_day = atoi(str_day.c_str())) == 0 )
    cout << "Error" << endl;
   else
    cout << CalculateDays(n_year, n_month, n_day) << endl;
  }
  else cout << "Error" << endl; 
  
 } 
 
 return 0;
}

 

1.百度时间

 Baidu的服务器上使用的不是北京时间,而是Baidu时间。Baidu时间的时分秒与北京时间相同,但是日期与北京时间不同,是用一个正整数表示从2000年1月1日开始经过了几天。

现在就请大家设计一个程序将北京时间转换为百度时间。在本题中,闰年的年份是400的倍数,或者是4的倍数但不是100的倍数。比如2000和8888均为闰年,但6100不是。

输入格式
输入数据的每一行为一个待转化的北京时间(不含空格和TAB),正确的格式包括两种:
一种为:YYYY-MM-DD,(YYYY表示四位数年份,MM为两位月份,DD为两位日期);
另一种为:MM/DD/YYYY,(YYYY表示四位数年份,MM为两位月份,DD为两位日期);

输出格式
每个数据输出一行。如果可以成功转换,输出一个正整数,否则输出Error。

输入样例
2000-01-01
AStar2007
05/26/2007

输出样例
0
Error
2702

评分规则

  1. 程序将运行在一台Linux机器上(内存使用不作严格限制),在每一测试用例上运行不能超过1秒,否则该用例不得分;
  2. 要求程序能按照输入样例的格式读取数据,按照输出样例的格式将运行结果输出到标准输出上。如果不能正确读入数据和输出数据,该题将不得分;
  3. 该题共有5个测试数据集,数据1和数据2中的所有日期均能成功转换。所有数据中,每行不超过20个字符,每组数据最多包含100行;
  4. 该题目20分。