2016年微软笔试题目

题目1 : Font Size

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Steven loves reading book on his phone. The book he reads now consists of N paragraphs and the i-th paragraph contains ai characters.

Steven wants to make the characters easier to read, so he decides to increase the font size of characters. But the size of Steven's phone screen is limited. Its width is W and height is H. As a result, if the font size of characters is S then it can only show ⌊W / S⌋ characters in a line and ⌊H / S⌋ lines in a page. (⌊x⌋ is the largest integer no more than x)  

So here's the question, if Steven wants to control the number of pages no more than P, what's the maximum font size he can set? Note that paragraphs must start in a new line and there is no empty line between paragraphs.

输入

Input may contain multiple test cases.

The first line is an integer TASKS, representing the number of test cases.

For each test case, the first line contains four integers N, P, W and H, as described above.

The second line contains N integers a1, a2, ... aN, indicating the number of characters in each paragraph.


For all test cases,

1 <= N <= 103,

1 <= W, H, ai <= 103,

1 <= P <= 106,

There is always a way to control the number of pages no more than P.

输出

For each testcase, output a line with an integer Ans, indicating the maximum font size Steven can set.

样例输入
2
1 10 4 3
10
2 10 4 3
10 10
样例输出
3
2

题目2 : 403 Forbidden

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Little Hi runs a web server. Sometimes he has to deny access from a certain set of malicious IP addresses while his friends are still allow to access his server. To do this he writes N rules in the configuration file which look like:

allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0

Each rule is in the form:  allow | deny address  or  allow | deny address/mask .

When there comes a request, the rules are checked in sequence until the first match is found. If no rule is matched the request will be allowed. Rule and request are matched if the request address is the same as the rule address or they share the same first mask digits when both written as 32bit binary number.

For example IP "1.2.3.4" matches rule "allow 1.2.3.4" because the addresses are the same. And IP "128.127.8.125" matches rule "deny 128.127.4.100/20" because 10000000011111110000010001100100 (128.127.4.100 as binary number) shares the first 20 (mask) digits with10000000011111110000100001111101 (128.127.8.125 as binary number).

Now comes M access requests. Given their IP addresses, your task is to find out which ones are allowed and which ones are denied.

输入

Line 1: two integers N and M.

Line 2-N+1: one rule on each line.

Line N+2-N+M+1: one IP address on each line.

All addresses are IPv4 addresses(0.0.0.0 - 255.255.255.255) . 0  <= m ask <= 32.


For 40% of the data: 1 <= N, M <= 1000.

For 100% of the data: 1 <= N, M <= 100000.

输出

For each request output "YES" or "NO" according to whether it is allowed.

样例输入
5 5
allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0
1.2.3.4
1.2.3.5
1.1.1.1
100.100.100.100
219.142.53.100
样例输出
YES
YES
NO
YES
NO


题目3 : Demo Day

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

You work as an intern at a robotics startup. Today is your company's demo day. During the demo your company's robot will be put in a maze and without any information about the maze, it should be able to find a way out.

The maze consists of N * M grids. Each grid is either empty(represented by '.') or blocked by an obstacle(represented by 'b'). The robot will be release at the top left corner and the exit is at the bottom right corner.

Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can't and keep moving to the bottom until it can't. At the beginning, the robot keeps moving to the right.

rrrrbb..            
...r....     ====> The robot route with broken sensors is marked by 'r'. 
...rrb..
...bb...

While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?

输入

Line 1: N, M.

Line 2-N+1: the N * M maze.


For 20% of the data, N * M <= 16.

For 50% of the data, 1 <= N, M <= 8.

For 100% of the data, 1<= N, M <= 100.

输出

The minimum number of grids to be changed.

样例输入
4 8
....bb..
........
.....b..
...bb...
样例输出
1

题目4 : Buiding in Sandbox

时间限制:30000ms
单点时限:3000ms
内存限制:256MB

描述

Little Hi is playing a sandbox voxel game. In the game the whole world is constructed by massive 1x1x1 cubes. The edges of cubes are parallel to the coordinate axes and the coordinates (x, y, z) of the center of each cube are integers.

At the beginning there is nothing but plane ground in the world. The ground consists of all the cubes of z=0. Little Hi needs to build everything by placing cubes one by one following the rules:

1. The newly placed cube must be adjacent to the ground or a previously placed cube. Two cubes are adjacent if and only if they share a same face.

2. The newly placed cube must be accessible from outside which means by moving in 6 directions(up, down, left, right, forward, backward) there is a path from a very far place - say (1000, 1000, 1000) in this problem - to this cube without passing through ground or other cubes.

Given a sequence of cubes Little Hi wants to know if he can build the world by placing the cubes in such order.

输入

The first line contains the number of test cases T(1 <= T <= 10).

For each test case the first line is N the number of cubes in the sequence.

The following N lines each contain three integers x, y and z indicating the coordinates of a cube.


For 20% of the data, 1 <= N <= 1000, 1 <= x, y, z <= 10.

For 100% of the data, 1 <= N <= 100000, 1 <= x, y, z <= 100.

输出

For each testcase output "Yes" or "No" indicating if Little Hi can place the cubes in such order.

样例提示

In the first test case three cubes are placed on the ground. It's OK.

In the second test case (1, 3, 2) is neither on the ground nor adjacent to previous cubes. So it can't be placed.

In the last test case (2, 2, 1) can not be reached from outside. So it can't be placed.  

样例输入
3
3
1 1 1
1 2 1
1 3 1
3
1 1 1
1 2 1
1 3 2
17
1 1 1
1 2 1
1 3 1
2 3 1
3 3 1
3 2 1
3 1 1
2 1 1
2 1 2
1 1 2
1 2 2
1 3 2
2 3 2
3 3 2
3 2 2
2 2 2
2 2 1
样例输出
Yes
No
No
#微软#
全部评论
//第二题详解 #include<iostream> #include<string> #include<vector> using namespace std; struct IPallow{ int flag; //1 allow or 0 deny; vector<int> ip; int mask; }; vector<int> IpStrToIpInt(string ipstr) { vector<int> ipint; int index=0; while(index!=string::npos) { index=ipstr.find('.'); if(index==string::npos) {ipint.push_back(atoi(ipstr.c_str())); break;} ipint.push_back(atoi((ipstr.substr(0,index)).c_str())); ipstr=ipstr.substr(index+1,ipstr.length()-index-1); } return ipint; } vector<IPallow> StringtoIPallow(vector<string> list) { vector<IPallow> iplist; string temp,allow,ipstr,maskstr,right; IPallow ip; int space,slash; for(int i=0;i<list.size();i++) { temp=list[i]; space=temp.find(' '); allow=temp.substr(0,space); right=temp.substr(space+1,temp.length()-space); slash=right.find('/'); if(slash!=string::npos) { ipstr=right.substr(0,slash); maskstr=right.substr(slash+1,right.length()-slash); } else { ipstr=right; maskstr="32"; } if(allow=="allow") ip.flag=1; else ip.flag=0; ip.mask=atoi(maskstr.c_str()); ip.ip=IpStrToIpInt(ipstr); iplist.push_back(ip); } return iplist; } void JudgeIP(vector<string> list,string q) { vector<int> qin=IpStrToIpInt(q); vector<IPallow> iplist=StringtoIPallow(list); vector<int> temp; IPallow tempip; int mask; for(int i=0;i<iplist.size();i++) { temp=qin; tempip=iplist[i]; mask=32-tempip.mask; if(mask<=8) { tempip.ip[3]=tempip.ip[3]>>mask; temp[3]=temp[3]>>mask; if(tempip.ip==temp) { if(tempip.flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; return; } continue; } if(mask<=16) { tempip.ip[3]=0; temp[3]=0; tempip.ip[2]=tempip.ip[2]>>mask-8; temp[2]=temp[2]>>mask-8; if(tempip.ip==temp) { if(tempip.flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; return; } continue; } if(mask<=24) { tempip.ip[3]=0; temp[3]=0; tempip.ip[2]=0; temp[2]=0; tempip.ip[1]=tempip.ip[1]>>mask-16; temp[1]=temp[1]>>mask-16; if(tempip.ip==temp) { if(tempip.flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; return; } continue; } if(mask<=32) { tempip.ip[3]=0; temp[3]=0; tempip.ip[2]=0; temp[2]=0; tempip.ip[1]=0; temp[1]=0; tempip.ip[0]=tempip.ip[0]>>mask-24; temp[0]=temp[0]>>mask-24; if(tempip.ip==temp) { if(tempip.flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; return; } continue; } } cout<<"No"<<endl; return; } int main(){ int n,m; cin>>n>>m; vector<string> list,qu; string temp; getchar(); for(int i=0;i<n;i++){ getline(cin,temp); list.push_back(temp);} for(int i=0;i<m;i++){ getline(cin,temp); qu.push_back(temp);} for(int i=0;i<m;i++){ JudgeIP(list,qu[i]);} system("pause"); return 0; }
点赞 回复 分享
发布于 2017-03-31 13:49
各位大神们,可以用本地IDE吗?还是必须在线编? 还有面试需要准备英文自我介绍之类的吗
点赞 回复 分享
发布于 2017-03-30 13:40
#include<algorithm> #include<utility> #include<string> #include<sstream> #include<iostream> #include<vector> #include<map> #include<bitset> std::pair<unsigned, unsigned> getad_ms(std::string str) { using namespace std; replace(str.begin(), str.end(), '.', ' '); replace(str.begin(), str.end(), '/', ' '); istringstream istr(str); pair<unsigned, unsigned> pa; for (int i{},j;i < 4;++i) { istr >> j; pa.first += j*(1 << 8 * (3 - i)); } !istr.eof() ? istr >> pa.second : (pa.second = 32, istr); return pa; } void trim_front(std::string& str, char ch) { size_t sz = str.find_first_not_of(ch, 0); str.erase(0, sz); } class search_tree { public: search_tree():head{ new node{} }/*,refuse_without_allow{false}*/{} void add(unsigned add, unsigned mask, bool acc, int num); bool accept(unsigned int add); ~search_tree(); private: enum class states { mid_state ,allow, deny }; struct node { node *left, *right; states acc; int num; node() :left{ nullptr }, right{ nullptr }, acc{ states::mid_state } {} }; node *head; void del(node *p); }; void search_tree::add(unsigned add, unsigned mask, bool acc,int num) { using namespace std; bitset<32> bit(add); node *pos = head; for (int i{};i < mask;++i) { if (pos->acc != states::mid_state) return; if (bit[31-i]) { if (pos->right == nullptr) pos->right = new node{}; pos = pos->right; } else { if (pos->left == nullptr) pos->left = new node{}; pos = pos->left; } } if (pos->acc != states::mid_state) return; pos->num = num; pos->acc = acc ? states::allow : states::deny; } bool search_tree::accept(unsigned int add) { using namespace std; bitset<32> bit(add); node * pos = head, *minnum{ nullptr }; int num=numeric_limits<int>::max(); for (size_t i = 0;i<32&&pos; i++) { if (pos&&pos->acc != states::mid_state) { if (pos->num < num) { num = pos->num; minnum = pos; } } pos = bit[31-i] ? pos->right : pos->left; } if (minnum == nullptr) return true; return minnum->acc == states::allow ? true : false; } search_tree::~search_tree() { del(head); } void search_tree::del(search_tree::node* p) { if (p->left) del(p->left); if (p->right) del(p->right); delete p; } int main() { using namespace std; for (int i, j;cin >> i >> j;) { string str; search_tree treenet; while (isspace(cin.peek())) { cin.get(); } for (int k{};k < i;++k) { getline(cin, str); trim_front(str, ' '); auto pos = str.find(' '); auto ps = getad_ms(string(str, pos + 1)); bool acc = str[0] == 'a' ? true:false; treenet.add(ps.first, ps.second, acc,k); } for (int k{};k < j;++k) { cin >> str; auto pa = getad_ms(str); cout << (treenet.accept(pa.first)?"YES":"NO") << '\n'; } } }
点赞 回复 分享
发布于 2016-04-09 00:18
AC一道.....
点赞 回复 分享
发布于 2016-04-07 15:21
第二题这样的,刚开始看错题目了耽误了好多时间!/(ㄒoㄒ)/~~ #include<iostream> #include<stdio.h> #include<string> #include<map> using namespace std; map<string, int>pp; int n,m; void work (string s) { bool p=0; int now; if (s[0]=='a') return ; for (int i=5;i<s.size();i++) if (s[i]=='/') { p=1; now=i; break; } string ss[5]; string temp=""; int count=1; int last; if (!p) { for (int i=5;i<s.size();i++) { if (s[i]!='.') temp+=s[i]; if (s[i]=='.') { ss[count]=temp; temp=""; count++; last=i; } } ss[4]=""; for (int i=last+1;i<s.size();i++) ss[4]+=s[i]; } else { for (int i=5;i<s.size();i++) { if (s[i]!='.') temp+=s[i]; if (s[i]=='.') { ss[count]=temp; temp=""; count++; last=i; } } ss[4]=""; for (int i=last+1;i<now;i++) ss[4]+=s[i]; } /*cout<<s<<endl; cout<<ss[1]<<" "; cout<<ss[2]<<" "; cout<<ss[3]<<" "; cout<<ss[4]<<" "<<endl;*/ int a[9],sz,x; sz=0; x=0; for (int i=0;i<ss[1].size();i++) x=x*10+(ss[1][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[2].size();i++) x=x*10+(ss[2][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[3].size();i++) x=x*10+(ss[3][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[4].size();i++) x=x*10+(ss[4][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); //cout<<temp<<endl; //cout<<temp.size()<<endl; string ans=""; for (int i=ss[4].size();i<temp.size();i++) ans+=temp[i]; //cout<<ans<<endl; //a[ans]=0; string t=""; for (int i=0;i<=19;i++) t+=ans[i]; pp[t]=1; //cout<<ans<<endl; //cout<<t<<endl; } void calc (string s) { bool p=0; int now; if (s[0]=='a') return ; for (int i=0;i<s.size();i++) if (s[i]=='/') { p=1; now=i; break; } string ss[5]; string temp=""; int count=1; int last; if (!p) { for (int i=0;i<s.size();i++) { if (s[i]!='.') temp+=s[i]; if (s[i]=='.') { ss[count]=temp; temp=""; count++; last=i; } } ss[4]=""; for (int i=last+1;i<s.size();i++) ss[4]+=s[i]; } else { for (int i=0;i<s.size();i++) { if (s[i]!='.') temp+=s[i]; if (s[i]=='.') { ss[count]=temp; temp=""; count++; last=i; } } ss[4]=""; for (int i=last+1;i<now;i++) ss[4]+=s[i]; } /*cout<<s<<endl; cout<<ss[1]<<" "; cout<<ss[2]<<" "; cout<<ss[3]<<" "; cout<<ss[4]<<" "<<endl;*/ int a[9],sz,x; sz=0; x=0; for (int i=0;i<ss[1].size();i++) x=x*10+(ss[1][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[2].size();i++) x=x*10+(ss[2][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[3].size();i++) x=x*10+(ss[3][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); sz=0; x=0; for (int i=0;i<ss[4].size();i++) x=x*10+(ss[4][i]-'0'); while (x) { a[++sz]=x%2; x=x/2; } if (sz<8) { for (int i=1;i<=8-sz;i++) temp+="0"; } for (int i=sz;i>=1;i--) temp+=char(a[i]+'0'); //cout<<temp<<endl; //cout<<temp.size()<<endl; string ans=""; for (int i=ss[4].size();i<temp.size();i++) ans+=temp[i]; //cout<<ans<<endl; //a[ans]=0; string t=""; for (int i=0;i<=19;i++) t+=ans[i]; //cout<<t<<endl; if (pp[t]) cout<<"NO"<<endl; else cout<<"YES"<<endl; } void doing () { cin>>n>>m; getchar(); string s; for (int i=1;i<=n;i++) { getline (cin,s); work (s); } //cout<<"!!!!!"<<pp["00000001000000010000"]; for (int i=1;i<=m;i++) { cin>>s; calc (s); } } int main () { doing (); return 0; }
点赞 回复 分享
发布于 2016-04-07 08:57
第三题动态规划吧
点赞 回复 分享
发布于 2016-04-06 22:39
第一题20分钟过,第二题40,第三题没调处来,输出1骗了20。。。输出0也有10分,其实要是早点去骗,就去输出1或者0,至少能骗30分的
点赞 回复 分享
发布于 2016-04-06 21:44
求最后一题思路。。。sigh 暴力骗了20分
点赞 回复 分享
发布于 2016-04-06 21:41
总分加起来三位数多一点,sigh...还是刷题太少了
点赞 回复 分享
发布于 2016-04-06 21:37
话说我也就完成了第一题,第二题本来想用个ip转换的库的,结果发现这是在线编程,并没有库给我用
点赞 回复 分享
发布于 2016-04-06 21:33
第一题完成 是比较简单的!代码如下: #include<iostream> #include<stdio.h> using namespace std; int n,w,h,p; int a[1010]; bool check (int s) {     if (w<s || h<s) return 0;     int nowx,nowy;     int x=w/s;     int y=h/s;     int tot=0;     int pages=0;     for (int i=1;i<=n;i++)     {         tot+=a[i]/x;         if (a[i]%x) tot++;         while (tot>y)         {             tot-=y;             pages++;         }         if (pages>p) return 0;     }     if (tot) pages++;     if (pages<=p) return 1;     return 0; } void doing () {     cin>>n>>p>>w>>h;     for (int i=1;i<=n;i++)         cin>>a[i];     int left,right,ans;     left=0;right=200000000;     while (left<=right)     {         int mid=(left+right)>>1;         if (check(mid)){             ans=mid;             left=mid+1;         }         else right=mid-1;     }     cout<<ans<<endl; } int main () {     int T;     cin>>T;     while (T--)         doing ();     return 0; }
点赞 回复 分享
发布于 2016-04-06 21:03

相关推荐

01-12 22:27
武汉大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
2025-11-25 19:47
点赞 评论 收藏
分享
头像
01-12 14:44
已编辑
百度_高级研发工程师
今天看到了某平台攻击牛友的帖子,段段今天打算为牛友们说句话,我们的努力到底有没有意义。&nbsp;(原文复述:感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的区味)&nbsp;&nbsp;粗鄙,无礼,傲慢,攻击,在这里我没有看到任何有用的分析,我只看到了屁股决定脑袋的攻击,我只看到了嫉妒和眼红。一、去医院不看病你去逛街吗&nbsp;去医院你不去看病你去逛街吗?去加油站不加油你去抽烟吗?去部队你不训练战斗技能你去养老吗?来牛客你不努力求职你来干什么来了。&nbsp;牛客本身就是个求职平台,大家分享有用的知识,分享面经,分享offer,分享求职经验的,来牛客不就干这个来了吗?有什么问题吗?...
给个好点的工作吧啊啊...:不知道我看的是不是和博主同样的帖子,我记得原帖是表达的是有些匿名老是发几十万的offer侮辱价,然后就有牛友觉得凡尔赛了导致后面的评论有些偏激。我觉得这个最近闫学晶那个事情有点类似了,她说他儿子一年只能赚七八十万家庭生活都难以为继,不说普通家庭,多少大厂的程序员都赚不到这个数字,大部分家庭看到这种发言肯定会难受的一p,生活的担子又这么重,人都是需要发泄情绪的,互联网就是个极佳的载体,所以很多人直接就喷她了,人在情绪发泄的时候是不思考的,否则就不叫发泄了。然后还有一个点,段哥假定了这些喷的人全都是“躺平的”,这点可能有失偏颇,很多人一直在努力,但是始终缺乏天时地利人和的某一个条件,这点相信段哥找工作的过程中深有体会。绝大部分人都以结果的失败去否认了努力的全过程,可能只是别人努力的方向错了。就像一次面试,可能你准备了很久,结果面试官就是比较奇葩,一直问没有学习到的领域或知识点,然后有人凭一个挂掉的结果就直接给你扣了一个“躺平”的帽子,觉得挂掉是你不够努力,您心里滋味如何?再说点近点的,我也是od,多少同事深夜无偿加班,涨过一分工资吗?多少外包的技术大牛因为学历被困在外包,连od都进不去,这些人难道不努力吗?只是限制与生活、公司制度等等之类的无奈罢了。说到努力,又想到李家琦79元眉笔事件,这么多年有没有认真工作?有没有涨工资?他嘴里说出来是那么的理所当然,打工牛马都知道“任劳任怨”,“认真工作”真能涨工资?只干活不发声就等着被摘果子吧,企业里永远都是“汇报杰出者”升的最快(当然不是所有企业),这种事情相信段哥包括我甚至大部分od都经历过。最近辞职回老家,和老爸散步每次他都会感慨街上的蔬菜小贩多不容易,他们晚上就窝在那种三轮小货车的驾驶室里,腿都伸不直,我们这里晚上零下了,只盖一条薄毛毯,始终舍不得住我们镇上几十块的酒店,因为一车蔬菜就赚几百块顶多一千而且要卖好久,这样的例子还有太多了。这种芸芸众生可能辛苦了一天之后,打开手机看到网上的凡尔赛发言,跟风喷了几句发泄情绪,我觉得这种人不应该扣上“躺平”的帽子。我觉得大部分正常人都是努力的,或者曾经努力过,但世界上有太多努力解决不了的无奈了,甚至说你都没有那个努力的机会,不过正因如此,才显得坚持不懈的努力奋斗之人的难得可贵,认清生活的真相后仍然热爱生活,敢于直面现实的淋漓。
段段STEADY觉醒与突...
点赞 评论 收藏
分享
评论
4
27
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务