網易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\n",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\n",value,num);
    while(!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        printf("%d %d\n",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\n", res);
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM