阿里巴巴2021秋招笔试题20210806
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里巴巴2021秋招笔试题20210806相关的知识,希望对你有一定的参考价值。
源代码:https://gitee.com/shentuzhigang/mini-project/tree/master/exam-alibaba/exam-alibaba-20210806
第一题
题目描述
大概定义了一年
m
m
m个月,一个月
d
d
d天,一周
w
w
w天
已知
m
m
m,
d
d
d,
w
w
w
求第
k
k
k年,第
j
j
j月,第
i
i
i天是星期几
星期几用小写字母表示
1<=w<=26
其他数据范围忘了,反正很正常。
解决方案
JAVA版本
package io.shentuzhigang.exam.alibaba;
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-08-06 19:16
*/
public class Exam2021080601 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
long d = scanner.nextLong();
long m = scanner.nextLong();
long w = scanner.nextLong();
long i = scanner.nextLong();
long j = scanner.nextLong();
long k = scanner.nextLong();
long result = ((k - 1) * m * d % w + (j - 1) * d % w + i % w) % w;
System.out.println((char) (96 + (result == 0 ? w : result)));
}
}
}
第二题
题目描述
有说明,没截到
样例输入
7
2 3 4 2
1 3 4
2 3 4 2
2 3 4 5
1 3 1
2 6 4 5
2 6 12 6
样例输出
NO
NO
YES
YES
YES
解决方案
C++版本
并查集
#include <stdio.h>
#include <iostream>
using namespace std;
int t[20000];
int find(int x) {
if (t[x] == 0) {
return x;
}
return find(t[x]);
}
void merge(int a, int b) {
int q = find(a);
int p = find(b);
if (q != p ){
t[q] = p;
}
}
int main(){
int n;
scanf("%d",&n);
for (int i = 0; i < n; i++) {
int op ,a,b,c;
scanf("%d",&op);
if (op == 1) {
scanf("%d%d",&a,&b);
merge(a, b);
} else if (op == 2) {
scanf("%d%d%d",&a,&b,&c);
int s = a % c;
int t = b % c;
printf( "%s\\n",(find(s) == find(t) ? "YES" : "NO"));
}
}
}
TLE的JAVA版本
package io.shentuzhigang.exam.alibaba;
import java.util.Scanner;
/**
* @author ShenTuZhiGang
* @version 1.0.0
* @email 1600337300@qq.com
* @date 2021-008-06 19:46
*/
public class Exam2021080602 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
bcj z = new bcj();
for (int i = 0; i < n; i++) {
int op = scanner.nextInt();
if (op == 1) {
int a = scanner.nextInt();
int b = scanner.nextInt();
z.merge(a, b);
} else if (op == 2) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int s = a % c;
int t = b % c;
System.out.println(z.find(s) == z.find(t) ? "YES" : "NO");
}
}
}
}
static class bcj {
int[] t = new int[20000];
public int find(int x) {
if (t[x] == 0) {
return x;
}
return find(t[x]);
}
public void merge(int a, int b) {
int q = find(a);
int p = find(b);
if (q != p) {
t[q] = p;
}
}
}
}
以上是关于阿里巴巴2021秋招笔试题20210806的主要内容,如果未能解决你的问题,请参考以下文章