招聘之笔试题目

1 阿里—编程测验

题目

给出一个有向无环图,有向边“A -> B”的含义是:节点A依赖于节点B,并且每个节点给出一个附加消耗值Vi,i => [1, n],定义一条有向链条为”从入度为0的节点”沿着有向边一直到达一个”出度为0的节点”的所有边和节点的集合,有向链条上所有节点的消耗值的和为该有向链条的消耗值。
求:最大的有向链条的深度,所有有向链条对应消耗值的最大值。

输入样例:

5 4 // 表示有5个系统和 4个依赖关系
3 // 调用1号系统耗时 3 ms
2 // 调用2号系统耗时 2 ms
10 // 调用3号系统耗时 10 ms
5 // 调用4号系统耗时 5 ms
7 // 调用5号系统耗时 7 ms
1 2 // 2号系统依赖1号系统
1 3 // 3号系统依赖1号系统
2 5 // 5号系统依赖2号系统
4 5 // 5号系统依赖4号系统


输出样例:

3 13


思路:

遍历入度为0的节点 + 对每一个节点进行dfs

代码如下:
#include<stdio.h>

#define huha 10000

typedef struct{
    int time;
    int _v[huha];
    int _v_c;
    int _in;
}node;
int n, m;
node nn[huha];

int res_max = 0;
int res_time = 0;
void dfs(int i, int res1, int res2){
    res2 += nn[i].time;
    res1 ++;
    if(nn[i]._v_c == 0){
        if(res1 > res_max){
            res_max = res1;
        }
        if(res2 > res_time){
            res_time = res2;
        }
    }else{
        int j;
        for(j = 0; j < nn[i]._v_c; j ++){
            dfs(nn[i]._v[j], res1, res2);
        }
    }
}

char hh[huha];
// 注意解决一下输入输出问题
int main(){
    gets(hh);
    //fflush(stdin);
    sscanf(hh, "%d%d", &n, &m);
    int i;
    for(i = 1; i <= n; i ++){
        gets(hh);
        //fflush(stdin);
        sscanf(hh, "%d", &nn[i].time);
        nn[i]._v_c = 0;
        nn[i]._in = 0;
    }
    int a, b;
    for(i = 0; i < m; i ++){
        gets(hh);
        //fflush(stdin);
        sscanf(hh, "%d%d", &a, &b);
        nn[b]._v[nn[b]._v_c ++] = a;
        nn[a]._in ++;
    }
    for(i = 1; i <= n; i ++){
        if(nn[i]._in == 0){
            dfs(i, 0, 0);
        }
    }
    printf("%d %d\n", res_max, res_time);
    return 0;
}

2 阿里—代码面试

题目

  按段(段内的元素不翻转)翻转链表:如链表 1->2->3->4->5->6->7->8->9,如果段大小为3,翻转后为7->8->9->4->5->6->1->2->3。注意段大小作为参数传入。要求编写可以运行的测试用例。

代码如下
#include<stdio.h>

typedef struct node{
  int v;
  struct node *next;
}node;

void reverse(node *head, int m){
    node *start, *end, *pre, *next;
    int i, count = 0;
    pre = head;//point to the last node before start.
    next = head->next;//point to the first node after end.
    while(next != NULL){
        start = next;//point to the first element of seg.
        end = start;//point to the last element of seg.
        for(i = 1; i < m; i ++){
            if(end->next == NULL){
                break;
            }
            end = end->next;
        }
        next = end->next;//update the next pointer.
        pre->next = next;//delete the seg.
        end->next = head->next;//insert the seg after head.
        head->next = start;
        if(count++ == 0){
            pre = end;
        }
    }
    pre->next = NULL;
}

int main(int argc, char *argv[]){
  int n, m;//n is the total number of link. m is the length of seg.
  node *head = (node *)malloc(sizeof(node));
  head->v = -1;
  head->next = NULL;
  scanf("%d", &n);//input the length of the link.
  node *temp, *next = head;
  int i;
  for(i = 0; i < n; i ++){
    temp = malloc(sizeof(node));
    temp->next = NULL;
    scanf("%d", &(temp->v));//input the element of the link.
    next->next = temp;
    next = temp;
  }
  scanf("%d", &m);//input the length of the seg.
  if(n >= m)
    reverse(head, m);//reverse the link.
  printf("After reverse, the link is:\n");
  next = head->next;
  while(next != NULL){//output the link after reverse.
    printf("%d ", next->v);
    next = next->next;
  }
  printf("\n");
  return 0;
}

3 网易笔试

题目

有n堆杂物,每堆杂物有四个存放点,每个存放点绕着一个固定的点逆时针旋转90°则表示该点
的杂物被移动了一次,当4个杂物存放点形成一个面积不为0的正方形的时候,表示杂物整理结束。
求最少多少步移动可以将所有的杂物整理完毕?
<1 <= n <= 100, 点的坐标值的绝对值不会超过10000>
输入:
第一行一个整数n,表示有多少个测试用例,每个测试用例占4行,每一行是四个整形数字:x1 y1 x0 y0,分别
表示杂物的存放点(x1, y1),该杂物旋转时围绕的中心点(x0, y0)
输出:
一个整数,表示最少的移动次数,不存在则输出-1.

样例测试

输入:
4
1 1 0 0
-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)逆时针旋转:点A(a, b)绕点(x, y)逆时针旋转90°后的坐标为(x + y - b, y + a - x).。
(2)求出四个点分别旋转之后的所有可能,然后暴力所有的组合情况,求出满足条件的最小步数。

代码如下
#include<stdio.h>
#include<string.h>

typedef struct{
    int x, y;
    int c_x, c_y;
    int steps;
}point;

point pp[4][4];
/*
点A(a, b)绕点(x, y)逆时针旋转90°后的坐标为(x + y - b, y + a - x).
*/
void reverse(point *a, point *b){
    b->c_x = a->c_x;
    b->c_y = a->c_y;
    b->steps = a->steps + 1;
    b->x = a->c_x + a->c_y - a->y;
    b->y = a->c_y + a->x - a->c_x;
}

int cmp(const point *p1, const point *p2){
    if(p1->x != p2->x){
        return p1->x < p2->x ? 1 : 0;
    }else{
        return p1->y < p2->y ? 1 : 0;
    }
}

int is_or_not(int i, int j, int k, int z){
    point p[4];
    memcpy(&p[0], &pp[0][i], sizeof(point));
    memcpy(&p[1], &pp[1][j], sizeof(point));
    memcpy(&p[2], &pp[2][k], sizeof(point));
    memcpy(&p[3], &pp[3][z], sizeof(point));
    qsort(p, 4, sizeof(point), cmp);
    long d1 = (p[0].x - p[1].x) * (p[0].x - p[1].x) + (p[0].y - p[1].y) * (p[0].y - p[1].y);
    long d2 = (p[0].x - p[2].x) * (p[0].x - p[2].x) + (p[0].y - p[2].y) * (p[0].y - p[2].y);
    long d3 = (p[3].x - p[1].x) * (p[3].x - p[1].x) + (p[3].y - p[1].y) * (p[3].y - p[1].y);
    long d4 = (p[3].x - p[2].x) * (p[3].x - p[2].x) + (p[3].y - p[2].y) * (p[3].y - p[2].y);
    long d5 = (p[2].x - p[1].x) * (p[2].x - p[1].x) + (p[2].y - p[1].y) * (p[2].y - p[1].y);
    if((d1 == d2 && d2 == d3 && d3 == d4 && d1 != 0) && (d1 + d2 == d5)){
        return 1;
    }
    return 0;
}

int compute(){
    int i, j, k, z;
    int steps = -1;
    for(i = 0; i < 4; i ++){
        for(j = 0; j < 4; j ++){
            for(k = 0; k < 4; k ++){
                for(z = 0; z < 4; z ++){
                    if((steps < 0 || steps > pp[0][i].steps + pp[1][j].steps + pp[2][k].steps + pp[3][z].steps) && is_or_not(i, j, k, z)){
                        steps = pp[0][i].steps + pp[1][j].steps + pp[2][k].steps + pp[3][z].steps;
                    }
                }
            }
        }
    }
    return steps;
}

int main(int argc, char *argv[]){
    int n;
    scanf("%d", &n);
    int i, j, k;
    for(i = 0; i < n; i ++){
        for(j = 0; j < 4; j ++){
            scanf("%d%d%d%d", &pp[j][0].x, &pp[j][0].y, &pp[j][0].c_x, &pp[j][0].c_y);
            pp[j][0].steps = 0;
        }
        for(j = 0; j < 4; j ++){
            for(k = 0; k < 3; k ++){
                reverse(&pp[j][k], &pp[j][k + 1]);
            }
        }
        printf("%d\n", compute());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值