微软笔试题
2015-09-30 01:17
302 查看
已爆炸。
时间限制:5000ms
单点时限:1000ms
内存限制:256MB
Given a circle on a two-dimentional plane.
Output the integral point in or on the boundary of the circle which has the largest distance from the center.
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.
For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000
One line with two integers separated by one space, indicating the answer.
If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.
样例输入
1.000 1.000 5.000
样例输出6 1
此题不难,很遗憾只过了40%。再多一点时间应该可以调好。
代码
#include <iostream>#include <cmath>using namespace std;
bool isInteger(float x);
void maxPoint(float x, float y, float r, int& xmax, int& ymax){ float maxDis = -1.0; int y1, y2; int x1, x2; for(y1 = y; y1<=y+r; y1++) { float temp = sqrt(r*r-(y1-y)*(y1-y)); float f1 = x+temp; float f2 = x-temp; if(isInteger(f1)) x1 = round(f1); else x1 = f1; if(isInteger(f2)) x2 = round(f2); else x2 = f2+1; float dis1 = sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y)); float dis2 = sqrt((x2-x)*(x2-x)+(y1-y)*(y1-y)); if(dis1>maxDis and dis1<=r) { maxDis = dis1; xmax = x1; ymax = y1; } if(dis2>maxDis and dis2<=r) { maxDis = dis2; xmax = x1; ymax = y1; } }}
bool isInteger(float x){ if(abs(x-(int)x)<0.000001) return true; return false;}
int main(int argc, char** argv){ float x, y, r; cin>>x>>y>>r; int xmax, ymax; maxPoint(x, y, r, xmax, ymax); cout<<xmax<<" "<<ymax; return 0;}
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.
The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City
3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.
During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?
Line 1: two integers N and M.
Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.
Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.
EDIT i j k, indicating change the length of the highway between city i and city j to k miles.
QUERY, for querying the THD.
For 30% of the data: 2<=N<=100, 1<=M<=20
For 60% of the data: 2<=N<=2000, 1<=M<=20
For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.
For each QUERY operation output one line containing the corresponding THD.
样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出10
14
12
图算法,我的方法是每条边计算的次数是该边两端的顶点数之积
#include <iostream>#include <cmath>#include <set>#include <vector>using namespace std;
int n;void dfs(vector<vector<int> >& graph, int i, set<int>& s){ for(int j=0; j<n; j++) { if(graph[i][j]!=0 and s.find(j)==s.end()) { s.insert(j); dfs(graph, j, s); } }}
void getNumber(vector<vector<int> >& graph, int& l, int& r, int i, int j){ set<int>s1, s2; s1.insert(i); s2.insert(j); dfs(graph, i, s1); dfs(graph, j, s2); l=s1.size(); r=s2.size();}
int main(int argc, char** argv){ vector<vector <int> >graph; int m; cin>>n>>m; for(int i=0; i<n; i++) { vector<int>tempV; tempV.clear(); for(int j=0; j<n; j++) { tempV.push_back(0); } graph.push_back(tempV); } int v1, v2, len; for(int i=1; i<=n-1; i++) { cin>>v1>>v2>>len; graph[v1-1][v2-1] = len; graph[v2-1][v1-1] = len; } vector<vector <int> >c; for(int i=0; i<n; i++) { vector<int>tempV; tempV.clear(); for(int j=0; j<n; j++) { tempV.push_back(0); } c.push_back(tempV); }
for(int i=0; i<n; i++) { for(int j=i; j<n; j++) { if(graph[i][j]!=0) { int old = graph[i][j]; graph[i][j] = 0; graph[j][i] = 0; int l, r; getNumber(graph, l, r, i, j); graph[i][j] = old; graph[j][i] = old; c[i][j] = l*r; c[j][i] = l*r; } } } string s; while(1) { s=""; cin>>s; if(s=="") { break; } else if(s[0]=='Q') { int sum=0; for(int i=0; i<n; i++) { for(int j=i; j<n; j++) { if(graph[i][j]!=0) { sum+=graph[i][j]*c[i][j]; } } } cout<<sum<<endl; } else if(s[0]=='E') { int v1, v2, len; cin>>v1>>v2>>len; graph[v1-1][v2-1] = len; graph[v2-1][v1-1] = len; } } return 0; }
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.
A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The fibonacci sequence is defined as below:
F1 = 1, F2 = 1
Fn = Fn-1 + Fn-2,
n>=3
One line with an integer n.
Second line with n integers, indicating the sequence {an}.
For 30% of the data, n<=10.
For 60% of the data, n<=1000.
For 100% of the data, n<=1000000, 0<=ai<=100000.
One line with an integer, indicating the answer modulo 1,000,000,007.
The 7 sub-sequences are:
{a2}
{a3}
{a2, a3}
{a2, a3, a4}
{a2, a3, a5}
{a2, a3, a4,
a6}
{a2, a3, a5,
a6}
样例输入
6
2 1 1 2 2 3
样例输出
7
第一反应是写了个dfs,最后发现时间过不了,最后才发现可以用动态规划,无奈时间不够了。
#include <iostream>#include <vector>using namespace std;
void dfs(vector<int>& v, int idx, int key, int formerFib, int &num){ for(int i=idx; i<v.size(); i++) { if(v[i]==key) { num++; dfs(v, i+1, key+formerFib, key, num); } }}
int main(int argc, char** argv){ int n; cin>>n; vector<int>v; int temp; for(int i=0; i<n; i++) { cin>>temp; v.push_back(temp); } int num=0; dfs(v, 0, 1, 0, num); cout<<num<<endl; return 0;}
动态规划版本(等价于lis问题)
#include <iostream>#include <vector>using namespace std;
int find(vector<int>&v, int key, int startIdx, int lastIdx){ while(startIdx<=lastIdx) { int middle = (startIdx + lastIdx)/2; if(v[middle]>key) lastIdx = middle - 1; else if(v[middle]<key) startIdx = middle + 1; else return middle; } return -1;}
void dfs(vector<int>& v, int &num){ int i=0; while(i<v.size() and v[i]!=1) i++; if(i==v.size()) return; vector<int>allNum; allNum.push_back(1); vector<int>fib; fib.push_back(1); fib.push_back(1); i++; for(;i<v.size();i++) { fib.push_back(fib[fib.size()-2]+fib[fib.size()-1]); if(v[i]==1) { if(allNum.size()==1) { allNum.push_back(allNum[0]); } else { allNum[1]+=allNum[0]; } allNum[0]+=1; } else { int idx = find(fib, v[i], 2, allNum.size()); if(idx!=-1) { if(idx==allNum.size()) { allNum.push_back(0); } allNum[idx]+=allNum[idx-1]; } } } for(int i=0; i<allNum.size(); i++) num+=allNum[i]; }
int main(int argc, char** argv){ int n; cin>>n; vector<int>v; int temp; for(int i=0; i<n; i++) { cin>>temp; v.push_back(temp); } int num=0; dfs(v, num); cout<<num<<endl; return 0;}
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
A fancy square image encryption algorithm works as follow:
0. consider the image as an N x N matrix
1. choose an integer k∈ {0, 1, 2, 3}
2. rotate the square image k * 90 degree clockwise
3. if N is odd stop the encryption process
4. if N is even split the image into four equal sub-squares whose length is N / 2 and encrypt them recursively starting from step 0
Apparently different choices of the k serie result in different encrypted images. Given two images A and B, your task is to find out whether it is POSSIBLE that B is encrypted from A. B is possibly encrypted from A if there is a choice of k serie that encrypt
A into B.
Input may contains multiple testcases.
The first line of the input contains an integer T(1 <= T <= 10) which is the number of testcases.
The first line of each testcase is an integer N, the length of the side of the images A and B.
The following N lines each contain N integers, indicating the image A.
The next following N lines each contain N integers, indicating the image B.
For 20% of the data, 1 <= n <= 15
For 100% of the data, 1 <= n <= 100, 0 <= Aij, Bij <=
100000000
For each testcase output Yes or No according to whether it is possible that B is encrypted from A.
样例输入321 23 43 14 221 24 33 14 244 1 2 31 2 3 42 3 4 13 4 1 23 4 4 12 3 1 21 4 4 32 1 3 2样例输出YesNoYes
没做
总结:
从前三题看不算太难。编程速度亟待提高。实力和缘分都不太够。
革未成,同需努。
题目1 : Farthest Point
时间限制:5000ms单点时限:1000ms
内存限制:256MB
描述
Given a circle on a two-dimentional plane.Output the integral point in or on the boundary of the circle which has the largest distance from the center.
输入
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000
输出
One line with two integers separated by one space, indicating the answer.If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.
样例输入
1.000 1.000 5.000
样例输出6 1
此题不难,很遗憾只过了40%。再多一点时间应该可以调好。
代码
#include <iostream>#include <cmath>using namespace std;
bool isInteger(float x);
void maxPoint(float x, float y, float r, int& xmax, int& ymax){ float maxDis = -1.0; int y1, y2; int x1, x2; for(y1 = y; y1<=y+r; y1++) { float temp = sqrt(r*r-(y1-y)*(y1-y)); float f1 = x+temp; float f2 = x-temp; if(isInteger(f1)) x1 = round(f1); else x1 = f1; if(isInteger(f2)) x2 = round(f2); else x2 = f2+1; float dis1 = sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y)); float dis2 = sqrt((x2-x)*(x2-x)+(y1-y)*(y1-y)); if(dis1>maxDis and dis1<=r) { maxDis = dis1; xmax = x1; ymax = y1; } if(dis2>maxDis and dis2<=r) { maxDis = dis2; xmax = x1; ymax = y1; } }}
bool isInteger(float x){ if(abs(x-(int)x)<0.000001) return true; return false;}
int main(int argc, char** argv){ float x, y, r; cin>>x>>y>>r; int xmax, ymax; maxPoint(x, y, r, xmax, ymax); cout<<xmax<<" "<<ymax; return 0;}
题目2 : Total Highway Distance
时间限制:10000ms单点时限:1000ms
内存限制:256MB
描述
Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City
3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.
During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?
输入
Line 1: two integers N and M.Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.
Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.
EDIT i j k, indicating change the length of the highway between city i and city j to k miles.
QUERY, for querying the THD.
For 30% of the data: 2<=N<=100, 1<=M<=20
For 60% of the data: 2<=N<=2000, 1<=M<=20
For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.
输出
For each QUERY operation output one line containing the corresponding THD.样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出10
14
12
图算法,我的方法是每条边计算的次数是该边两端的顶点数之积
#include <iostream>#include <cmath>#include <set>#include <vector>using namespace std;
int n;void dfs(vector<vector<int> >& graph, int i, set<int>& s){ for(int j=0; j<n; j++) { if(graph[i][j]!=0 and s.find(j)==s.end()) { s.insert(j); dfs(graph, j, s); } }}
void getNumber(vector<vector<int> >& graph, int& l, int& r, int i, int j){ set<int>s1, s2; s1.insert(i); s2.insert(j); dfs(graph, i, s1); dfs(graph, j, s2); l=s1.size(); r=s2.size();}
int main(int argc, char** argv){ vector<vector <int> >graph; int m; cin>>n>>m; for(int i=0; i<n; i++) { vector<int>tempV; tempV.clear(); for(int j=0; j<n; j++) { tempV.push_back(0); } graph.push_back(tempV); } int v1, v2, len; for(int i=1; i<=n-1; i++) { cin>>v1>>v2>>len; graph[v1-1][v2-1] = len; graph[v2-1][v1-1] = len; } vector<vector <int> >c; for(int i=0; i<n; i++) { vector<int>tempV; tempV.clear(); for(int j=0; j<n; j++) { tempV.push_back(0); } c.push_back(tempV); }
for(int i=0; i<n; i++) { for(int j=i; j<n; j++) { if(graph[i][j]!=0) { int old = graph[i][j]; graph[i][j] = 0; graph[j][i] = 0; int l, r; getNumber(graph, l, r, i, j); graph[i][j] = old; graph[j][i] = old; c[i][j] = l*r; c[j][i] = l*r; } } } string s; while(1) { s=""; cin>>s; if(s=="") { break; } else if(s[0]=='Q') { int sum=0; for(int i=0; i<n; i++) { for(int j=i; j<n; j++) { if(graph[i][j]!=0) { sum+=graph[i][j]*c[i][j]; } } } cout<<sum<<endl; } else if(s[0]=='E') { int v1, v2, len; cin>>v1>>v2>>len; graph[v1-1][v2-1] = len; graph[v2-1][v1-1] = len; } } return 0; }
题目3 : Fibonacci
时间限制:10000ms单点时限:1000ms
内存限制:256MB
描述
Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The fibonacci sequence is defined as below:
F1 = 1, F2 = 1
Fn = Fn-1 + Fn-2,
n>=3
输入
One line with an integer n.Second line with n integers, indicating the sequence {an}.
For 30% of the data, n<=10.
For 60% of the data, n<=1000.
For 100% of the data, n<=1000000, 0<=ai<=100000.
输出
One line with an integer, indicating the answer modulo 1,000,000,007.
样例提示
The 7 sub-sequences are:{a2}
{a3}
{a2, a3}
{a2, a3, a4}
{a2, a3, a5}
{a2, a3, a4,
a6}
{a2, a3, a5,
a6}
样例输入
6
2 1 1 2 2 3
样例输出
7
第一反应是写了个dfs,最后发现时间过不了,最后才发现可以用动态规划,无奈时间不够了。
#include <iostream>#include <vector>using namespace std;
void dfs(vector<int>& v, int idx, int key, int formerFib, int &num){ for(int i=idx; i<v.size(); i++) { if(v[i]==key) { num++; dfs(v, i+1, key+formerFib, key, num); } }}
int main(int argc, char** argv){ int n; cin>>n; vector<int>v; int temp; for(int i=0; i<n; i++) { cin>>temp; v.push_back(temp); } int num=0; dfs(v, 0, 1, 0, num); cout<<num<<endl; return 0;}
动态规划版本(等价于lis问题)
#include <iostream>#include <vector>using namespace std;
int find(vector<int>&v, int key, int startIdx, int lastIdx){ while(startIdx<=lastIdx) { int middle = (startIdx + lastIdx)/2; if(v[middle]>key) lastIdx = middle - 1; else if(v[middle]<key) startIdx = middle + 1; else return middle; } return -1;}
void dfs(vector<int>& v, int &num){ int i=0; while(i<v.size() and v[i]!=1) i++; if(i==v.size()) return; vector<int>allNum; allNum.push_back(1); vector<int>fib; fib.push_back(1); fib.push_back(1); i++; for(;i<v.size();i++) { fib.push_back(fib[fib.size()-2]+fib[fib.size()-1]); if(v[i]==1) { if(allNum.size()==1) { allNum.push_back(allNum[0]); } else { allNum[1]+=allNum[0]; } allNum[0]+=1; } else { int idx = find(fib, v[i], 2, allNum.size()); if(idx!=-1) { if(idx==allNum.size()) { allNum.push_back(0); } allNum[idx]+=allNum[idx-1]; } } } for(int i=0; i<allNum.size(); i++) num+=allNum[i]; }
int main(int argc, char** argv){ int n; cin>>n; vector<int>v; int temp; for(int i=0; i<n; i++) { cin>>temp; v.push_back(temp); } int num=0; dfs(v, num); cout<<num<<endl; return 0;}
题目4 : Image Encryption
时间限制:10000ms单点时限:1000ms
内存限制:256MB
描述
A fancy square image encryption algorithm works as follow:0. consider the image as an N x N matrix
1. choose an integer k∈ {0, 1, 2, 3}
2. rotate the square image k * 90 degree clockwise
3. if N is odd stop the encryption process
4. if N is even split the image into four equal sub-squares whose length is N / 2 and encrypt them recursively starting from step 0
Apparently different choices of the k serie result in different encrypted images. Given two images A and B, your task is to find out whether it is POSSIBLE that B is encrypted from A. B is possibly encrypted from A if there is a choice of k serie that encrypt
A into B.
输入
Input may contains multiple testcases.The first line of the input contains an integer T(1 <= T <= 10) which is the number of testcases.
The first line of each testcase is an integer N, the length of the side of the images A and B.
The following N lines each contain N integers, indicating the image A.
The next following N lines each contain N integers, indicating the image B.
For 20% of the data, 1 <= n <= 15
For 100% of the data, 1 <= n <= 100, 0 <= Aij, Bij <=
100000000
输出
For each testcase output Yes or No according to whether it is possible that B is encrypted from A.样例输入321 23 43 14 221 24 33 14 244 1 2 31 2 3 42 3 4 13 4 1 23 4 4 12 3 1 21 4 4 32 1 3 2样例输出YesNoYes
没做
总结:
从前三题看不算太难。编程速度亟待提高。实力和缘分都不太够。
革未成,同需努。
相关文章推荐
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
- [leetcode] 274 275 H-Index I && II
- int 13h 参数大全
- 我的hosts文件
- 阿里云部署 Flask + WSGI + Nginx 详解
- 计算机网络系列学习:Circuit Switch与Packet Switch
- 协议设计注意事项
- VS2015 Could not locate the Android Debug Bridge (adb.exe).
- 《深入浅出SQL》
- iOS.AppThinning-iOS9-new-feature-for-app-thinning-bitcode-odr-slicing
- HDU 5475An easy problem 离线set/线段树
- 【个人博客作业II】有关代码规范问题的讨论
- Leetcode 26. Remove Duplicates from Sorted Array
- linux用户及权限管理
- C语言结构体struct
- JVM-ClassLoader
- Configuring HTTPS servers
- 计算机网络系列学习:Internet简介
- [CareerCup] 9.10 Stack Boxes 垒箱子问题
- 学习笔记 - FileOutputStream流将字符串写入文件中