创新工场2018笔试题

1给出N年的一个周期,计算2017年1月1日至2017+N-1年12月31日中17号周一到周日的次数,N为正整数且不大于400.

注意:

1、2017年1月17日是星期二.

2、闰年2月有29天.

#include <iostream>
#include <vector>
using namespace std;
#define STARTYEAR 2017


bool Is_Leapyear(int year)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
void Print(int n)
{
if(n <= 0 || n > 400)
{
cout<<"argc error"<<endl;
return ;
}
int ve_day[] = {31,28,31,30,31,30,31,31,30,31,30,31};
vector<int> ve_end(7,0);
int week = 2;


for(int i = 0; i < n; ++i)
{
if(Is_Leapyear(i+STARTYEAR))
{
ve_day[1] = 29;
}
for(int j = 0; j < 12; ++j)
{
if(week)
ve_end[week-1] ++;
else
ve_end[week+7-1]++;
week += ve_day[j];
week %= 7;
}
ve_day[1] = 28;
}
for(int i = 0; i < 7; ++i)
cout<<ve_end[i]<<" ";
cout<<endl;
}
int main()
{
int n;
cin>>n;
Print(n);
return 0;
}


2公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。

排列顺序从左至右依次为:地址码,生日码,顺序码和校验码。

他们遵循以下规则:

1.地址码是由6个数字组成,有些代表了一个地区,有些是不合法的。

2.生日码是由8个数字组成,格式如YYYYMMDD,前四位代表年份,中间两位代表月份,最后两位代表日期。

注意:考虑闰年的情况。另外,一个合法的生日码必须在1900年1月1日到2011年12月31日之间。

3.顺序码是由3个数字组成,三个数字可以是任意的,但是"000"是不合法的。注意:顺序码为偶数代表女性,奇数代表男性。

4.校检码可能是1个数字或者'X'组成。用x代表校检码,a1-a17代表前17位数字,会满足以下等式:

(x+a1*2^17+a2*2^16+a3*2^15....+a17*2^1)%11 = 1,会得到一个解 0<=x<=10,如果x=10,用'X'表示。

请写一个函数判断是否合法,regionArry代表合法的地址码。

String verify(String id, String[] regionArry)

:

"110101197204300849",{"110101","110102"}

"Female"


#include <iostream>
#include <vector>
#include <iterator>
#include <math.h>
#include <string>
using namespace std;


const int len = 3;
static int num_all[18] = {0};
void Change(string &id)
{
for(int i = 0; i < 18-1; ++i)
num_all[i] = id[i] - '0';
if((id[17] - '0') == 'X' || (id[17] - '0' == 'x'))
id[17] = 10;
else
id[17] = id[17] - '0';
}
void Get_year_month_day(string &id, int &year, int &month, int &day)
{
for(int i = 6; i < 6+4; ++i)
year += num_all[i] * pow(10, 3 - (i - 6));
for(int i = 10; i < 10+2; ++i)
month += num_all[i] * pow(10, 1 - (i - 10));
for(int i = 12; i < 12+2; ++i)
day += num_all[i] * pow(10, 1 - (i - 12));
}
bool Is_region_birthday(string &id)
{
bool end = false;
int year = 0, month = 0, day = 0;
int ar[] = {31,28,31,30,31,30,31,31,30,31,30,31};
Get_year_month_day(id, year, month, day);


if(year >= 1900 && year <= 2011)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
ar[1] = 29;
if(month >= 1 && month <= 12)
{
if(day >= 1 && day <= ar[month-1])
end = true;
}
}
return end;
}
int Is_region_ordercode(string &id)
{
int end = 0;
for(int i = 14; i < 14+3; ++i)
end += (num_all[i] * pow(10, 2 - (i - 14)));
if(end == 0)
return 0;
if(end % 2 == 1)
return 1;
else
return 2;
}
bool Is_region_checkcode(string &id)
{
int num = 0;
for(int i = 1; i < 18; ++i)
{
num += (num_all[i-1]*pow(2,18-i));
}
for(int i = 0; i <= 10; ++i)
{
if((i + num) % 11 == 1)
return true;
}
return false;
}
string verify(string &id,string regionArray[])
{
string end = "input error";
if(id.size() == 18)
{
int i;
for(i = 0; i < len; ++i)
{
if(strncmp(id.c_str(),regionArray[i].c_str(),6) == 0)
break;
}
if(i < len)
{
Change(id);
if(Is_region_birthday(id))
{
int tmp = Is_region_ordercode(id);
if(tmp != 0)
{
if(Is_region_checkcode(id))
{
end = (tmp == 1 ? "male" : "female");
}
}
}
}
}
return end;
}
int main()
{
string regionArray[len] = {"110101","110102","610121"};
string id;
cin>>id;
string tmp = verify(id,regionArray);
cout<<tmp<<endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值