灵犀互娱笔试 灵犀互娱笔试题 0420
笔试时间:2024年04月20日
历史笔试传送门:2023秋招笔试合集
第一题
题目:zorder
给定两个无符号4字节长度整型x和y,将x和y经过zorder计算后得出—个无符号8字节长度整型值z。
zorder的计算规则如下: 1.将x和y转成二进制,z二进制数值的每两位分别从x和y中取2.每次获取,x作为二进制的高位,y作为低位 3.直到x和y的32位取完,即得出z值。
输入描述
第—行只有—个数n,表示后面有多少行数据第二行开始有n行,每行有两组数据分别是x和y。
输出描述
有n行,每行只有—个数据为Z。
说明
如:x是18,二进制00010010。y是52,二进制00110100。则z为1816,二进制为011100011000.
第一轮:从x取0,从y 取0,则z:00
第二轮:从x取1,从y取0,则z:1000
第三轮:从x取0,从y 取1,则z:011000
第四轮:从x取0,从y取0,则z:00011000
第五轮:从x取1,从y 取1,则z:1100011000
第六轮:从x取0,从y取1,则z:011100011000
样例输入
3
18 52
178 532
321 943
样例输出
1816
297752
484439
参考题解
穿插两个数字的二进制位。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream>
using namespace std;
using ull = unsigned long long;
int main() {
ull n, x, y; cin >> n;
while (n--) {
cin >> x >> y;
ull ans = 0;
for (int i = 0; i < 32; ++i) {
int a = (y >> i) & 1;
if (a) ans |= ((ull)1 << (2 * i));
a = (x >> i) & 1;
if (a) ans |= ((ull)1 << (2 * i + 1));
}
cout << ans << "\n";
}
}
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
while (n-- > 0) {
long x = sc.nextLong();
long y = sc.nextLong();
long ans = 0;
for (int i = 0; i < 32; ++i) {
int a = (int)((y >> i) & 1);
if (a == 1) {
ans |= (1L << (2 * i));
}
a = (int)((x >> i) & 1);
if (a == 1) {
ans |= (1L << (2 * i + 1));
}
}
System.out.println(ans);
}
sc.close();
}
}
Python:[此代码未进行大量数据的测试,仅供参考]
def main():
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
index = 1
results = []
for _ in range(n):
x = int(data[index])
y = int(data[index + 1])
index += 2
ans = 0
for i in range(32):
a = (y >> i) & 1
if a == 1:
ans |= (1 << (2 * i))
a = (x >> i) & 1
if a == 1:
ans |= (1 << (2 * i + 1))
results.append(ans)
for result in results:
print(result)
if __name__ == "__main__":
main()
第二题
题目:翻转token
给定一段英文字符串,以单词为单位进行翻转字符串输出。
输入描述
第—行包含整数T,表示共有T组测试数据。每组数据占一行,为将要翻转的字符串,每行字符串的长度不会超过1023个字节。
输出描述
每组数据输出—行结果,表示答案。
样例输入
3
game
hello world
welcome to lingxi
样例输出
game
world hello
lingxi to welcome
参考题解
力扣原题。
计算每个根节点的子树所有严重问题数和一般问题数,然后判断即可。
C++:[此代码未进行大量数据的测试,仅供参考]
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int T; cin >> T;
getchar();
while (T--) {
string s, t; getline(cin, s);
stringstream ss(s);
vector<string> v;
while (ss >> t) v.push_back(t);
reverse(v.begin(), v.end());
for (auto& e : v) cout << e << " ";
cout << "\n";
}
}
Java:[此代码未进行大量数据的测试,仅供参考]
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
sc.nextLine(); // consume the newline character after
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
持续收录字节、腾讯、阿里、美团、美团、拼多多、华为等笔试题解,包含python、C++、Java多种语言版本,持续更新中。