网易2019校招C++研发工程师笔试编程题

丰收?

(忘了题目了QAQ)

题目描述:

又到了丰收的季节,恰逢小易去牛牛的果园里游玩。
牛午常说他对整个果园的每个地方都了如指掌,小易不太相信,
所以他想考考牛牛。
在果园里有N堆苹果,每堆苹果的数量为ai,小易希望知道从左往
右数第x个苹果是属于哪一堆的。
牛牛觉得这个问题大简单,所以希望你来着他回答。

输入描述:

第一行一个数n(1<=n<=100000)
第二行n个数ai(1<=ai<=1000),表示从左往右数第i堆有多少苹果
第三行一个数m(1<=m<=100000),表示有m次询口。
第四行m个数,表示小易希望知道第qi个苹果属于哪一堆。
 

测试样例:

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(0);
        cout.tie(0);
using namespace std;
typedef long long ll;
typedef vector<int > vi;
const ll INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAX = 100000 + 5;
int a[MAX];
int front_a[MAX];
int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i = 1 ; i <= n; i++)
    {
        scanf("%d", a + i );
        front_a[i] = front_a[i-1] + a[i];
    }
    scanf("%d",&m);
    while(m--) {
        int q;
        scanf("%d",&q);
        int left = 1,right = n;

        while(left < right) {
            int middle = (left+right)/2;
            if(front_a[middle] < q )
            {
                left = middle+1;
            }
            else if(front_a[middle]>q){
                right = middle;
            }else {
                left = middle;
                break;
            }
        }
        printf("%d
",left);
    }
    return 0;
}
/*
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(0);
        cout.tie(0);
using namespace std;
typedef long long ll;
typedef vector<int > vi;
const ll INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 100 + 5;
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);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&High[i].high);
        High[i].pos = i;
    }
    queue <pair<int,int> > q;
    int value = 0,num = 0;
    sort(High + 1 , High+n+1,cmp);
    int flag = 0;
    for(int i = 0 ; i < k; i++) {

        if(High[n].high - High[1].high<=1) {
            value = High[n].high - High[1].high;
            flag = 1;
            break;
        }else {
            High[n].high--;
            High[1].high++;
            num++;
            q.push(pair<int,int >(High[n].pos,High[1].pos));
        }
        sort(High + 1 , High+n+1,cmp);
    }
    if(flag == 0) {
        value = High[n].high - High[1].high;
    }
    printf("%d %d
",value,num);
    while(!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        printf("%d %d
",x,y);
    }
    return 0;
}
/*
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(int x = 0, int y = 0) : 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) {
    if (a.x == 0 && a.y == 0 || b.x == 0 && b.y == 0) return false;
    if (a.x * a.x + a.y * a.y != b.x * b.x + b.y * b.y) return false;
    if (a.x * b.x + a.y * b.y != 0) return false;
    return true;
}
 
int main() {
    int n;
    cin >> n;
    while (n--) {
        point p[4], o[4], a[4];
        for (int i = 0; i < 4; i++)
            scanf("%d %d %d %d", &p[i].x, &p[i].y, &o[i].x, &o[i].y);
        int res = -1;
        int x, y, z, w;
        for (x = 0, a[0] = p[0]; x < 4; x++) {
            for (y = 0, a[1] = p[1]; y < 4; y++) {
                for (z = 0, a[2] = p[2]; z < 4; z++) {
                    for (w = 0, a[3] = p[3]; w < 4; w++) {
                        int cost = x + y + z + w;
                        if (a[0] + a[1] == a[2] + a[3] &&
                                check(a[0] - a[1], a[2] - a[3]) ||
                            a[0] + a[2] == a[1] + a[3] &&
                                check(a[0] - a[2], a[1] - a[3]) ||
                            a[0] + a[3] == a[1] + a[2] &&
                                check(a[0] - a[3], a[1] - a[2])) {
                            if (res == -1 || res > cost) res = cost;
                        }
                        a[3] = a[3].rotate(o[3]);
                    }
                    a[2] = a[2].rotate(o[2]);
                }
                a[1] = a[1].rotate(o[1]);
            }
            a[0] = a[0].rotate(o[0]);
        }
        printf("%d
", res);
    }
    return 0;
}
宝剑锋从磨砺出 梅花香自苦寒来
原文地址:https://www.cnblogs.com/GHzcx/p/9461409.html