网易2019校招C++研发工程师笔试编程题
丰收?
(忘了题目了QAQ)
题目描述:
输入描述:
测试样例:
5
2 7 3 4 9
3
1 25 11
输出
1
5
3
思路:
前缀和+二分
第i个位置表示前i堆苹果总数量,利用二分查找输入苹果所在的位置
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <queue> #include <map> #include <list> #include <utility> #include <set> #include <algorithm> #include <deque> #include <vector> #define IO ios::sync_with_stdio(false);\ cin.tie();\ cout.tie(); using namespace std; typedef long long ll; typedef vector<int > vi; const ll INF = 0x3f3f3f3f; ; + ; int a[MAX]; int front_a[MAX]; int main() { int n,m; scanf("%d",&n); ; i <= n; i++) { scanf("%d", a + i ); front_a[i] = front_a[i-] + a[i]; } scanf("%d",&m); while(m--) { int q; scanf("%d",&q); ,right = n; while(left < right) { ; if(front_a[middle] < q ) { left = middle+; } else if(front_a[middle]>q){ right = middle; }else { left = middle; break; } } printf("%d\n",left); } ; } /* 5 2 7 3 4 9 3 1 25 11 */
塔
题目描述
小易有一些立方体,每个立方体的边长为1,他用这些立方体搭了一些塔
现在小男定义:这些场的不稳定值为它们之中最高的塔与最低的塔的高度差,
小易想让这些塔尽量稳定,所以他进行了如下操作:每次从基座楼上取下块立方体并把它到号空塔上,
注意,小题不会把立方体放到它原本的那座塔上,因为他认为这样无意义
输入描述
第一行一个n(1<=n<=100),一个k(1<=k<=1000)
第二行ai(1<=ai<=10000)代表每个塔的高度
输出描述
第一行输出一个s,一个m,s代表最低的不稳定性,m为操作步数
接下来m行每行代表a塔向b塔移动一个单位
测试样例:
样例输入:
3 2
5 8 5
样例输出
0 2
2 1
2 3
思路:
因为数据量不大,全程模拟,先将所有的塔从低到高排序 将最高塔移动一个单位给最低塔 再排序移动 最多循环k次
如果最低与最高塔的差小于等于1,直接退出。
PS:描述可能有差池,请见谅 还有 这仅代表个人思路 笔试过程中仅通过20%(听说此题已重判,故妄自菲薄的贴上代码,望大神指正)
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <queue> #include <map> #include <list> #include <utility> #include <set> #include <algorithm> #include <deque> #include <vector> #define IO ios::sync_with_stdio(false);\ cin.tie();\ cout.tie(); using namespace std; typedef long long ll; typedef vector<int > vi; const ll INF = 0x3f3f3f3f; ; + ; typedef struct node { int pos; int high; } node; node High[N]; bool cmp(node a , node b) { return a.high < b.high; } int main() { int n,k; scanf("%d%d",&n,&k); ; i <= n; i++) { scanf("%d",&High[i].high); High[i].pos = i; } queue <pair<int,int> > q; ,num = ; sort(High + , High+n+,cmp); ; ; i < k; i++) { ].high<=) { value = High[n].high - High[].high; flag = ; break; }else { High[n].high--; High[].high++; num++; q.push(pair<].pos)); } sort(High + , High+n+,cmp); } ) { value = High[n].high - High[].high; } printf("%d %d\n",value,num); while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); printf("%d %d\n",x,y); } ; } /* 3 2 5 8 5*/
整理房间
题目描述
又到了周末,小易的房间乱得一团糟。
他希望将地上的杂物稍微整理下,使每团杂物看起来都紧凑一些, 没有那么乱。
地上一共有n团杂物,毎团杂物都包含4个物品。第i物品的坐标用(ai,bi)表示,小易毎次都可以将
它绕着(xi,yi)逆时针旋转90°,这将消耗他的一次移动次数。如果一团杂物的4个点构成了一个面
积不为0的正方形,我们说它是紧凑的。
因为小易很懒,所以他希望你帮助他计算一下每团杂物最少需要多少步移动能使它変得紧凑。
输入描述
第一行一个数(1 <= n <= 100),表示杂物的团数。
接下来4n行,毎4行表示一团杂物,毎行4个数xi, yi, ai, bi(-10^4<= xi, yi,
ai,bi <= 10^4),表示第i个物品旋转的中心点坐标和它本身的坐标。
輸出描述:
n行,毎行1个数,表示最少移动次数。
示例1
输入
4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0
输出
1
-1
3
3
说明
对于第一团杂物,我们可以旋转第二个或者第三个物品1次。
思路:
暴力枚举每团杂物4 ^ 4次旋转
代码:
来自牛客大神的代码。
作者:NotDeep 链接:https://www.nowcoder.com/discuss/92989?type=0&order=0&pos=6&page=0 来源:牛客网 #include <bits/stdc++.h> using namespace std; struct point { int x, y; point(, ) : x(x), y(y) {} point operator+(const point &rhs) const { return point(x + rhs.x, y + rhs.y); } point operator-(const point &rhs) const { return point(x - rhs.x, y - rhs.y); } point rotate() { return point(-y, x); } point rotate(const point &o) const { return o + (*this - o).rotate(); } bool operator==(const point &rhs) const { return x == rhs.x && y == rhs.y; } }; bool check(const point &a, const point &b) { && a.y == || b.x == && b.y == ) return false; if (a.x * a.x + a.y * a.y != b.x * b.x + b.y * b.y) return false; ) return false; return true; } int main() { int n; cin >> n; while (n--) { point p[], o[], a[]; ; i < ; i++) scanf("%d %d %d %d", &p[i].x, &p[i].y, &o[i].x, &o[i].y); ; int x, y, z, w; , a[] = p[]; x < ; x++) { , a[] = p[]; y < ; y++) { , a[] = p[]; z < ; z++) { , a[] = p[]; w < ; w++) { int cost = x + y + z + w; ] + a[] == a[] + a[] && check(a[] - a[], a[] - a[]) || a[] + a[] == a[] + a[] && check(a[] - a[], a[] - a[]) || a[] + a[] == a[] + a[] && check(a[] - a[], a[] - a[])) { || res > cost) res = cost; } a[] = a[].rotate(o[]); } a[] = a[].rotate(o[]); } a[] = a[].rotate(o[]); } a[] = a[].rotate(o[]); } printf("%d\n", res); } ; }
最新文章
- UP Board 人若有大胆,板子就很惨:首次上电开机失败
- String Aop 动态代理例子
- 2014年百度之星程序设计大赛 - 初赛(第二轮)JZP Set
- spring @condition 注解
- spring 注入静态变量
- isNaN() 确认是否是数字
- SQL Server中DateTime与DateTime2的区别
- iOS开发项目之三 [ 自定义tabBarCtrl]
- OpenShare新功能@2014年11月 [2014-11-30]
- php 获取远程图片
- servlet清晰理解
- Java日期时间使用(转)
- rm-rf 恢复过程中滥用
- RoportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法
- 动手动脑(lesson2)
- aws ubuntu 开启root
- windows系统显示隐藏文件以及显示文件扩展名
- 使用vue做项目
- 记一次Apache Carbondata PR的经历
- Sun公司的产品AnswerBook存在多种漏洞