灵犀互娱笔试 灵犀互娱笔试题 0915

笔试时间:2024年09月15日 秋招

历史笔试传送门:2023秋招笔试合集

第一题

题目

给定两个无符号4字节长度整型x和y,将x和y经过zorder计算后得出一个无符号8字节长度整型值z。zorder的计算规则如下:

1、将x和y转成二进制,z二进制数值的每两位分别从x和v中取;

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;
typedef unsigned long long ull;

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 << endl;
    }
    return 0;
}

Java:[此代码未进行大量数据的测试,仅供参考]

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();

        while (n-- > 0) {
            long x = scanner.nextLong();
            long y = scanner.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);
        }
        
        scanner.close();
    }
}

Python:[此代码未进行大量数据的测试,仅供参考]

def main():
    n = int(input())
    
    for _ in range(n):
        x, y = map(int, input().split())
        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)
        
        print(ans)

第二题

题目

给定一段英文字符串,以单词为单位进行翻转字符串输出。

输入描述

第一行包含整数T,表示共有T 组测试数据。

每组数据占一行,为将要翻转的字符串,每行字符串的长度不会超过1023个字节。

输出描述

每组数据输出一行结果,表示答案。

样例输入

3  

game  

hello world  

welcome to lingxi

样例输出

game  

world hello  

lingxi to welcome

参考题解

按照题意倒序输出单词。

C++:[此代码未进行大量数据的测试,仅供参考]

#include <algorithm>  
#include <iostream>  
#include <string>  
#include <vector>  
#include <sstream>  
using namespace std;  

int main() {  
    int T;  
    cin >> T;  
    cin.ignore();  
    while (T--) { 
        string input; 
        getline(cin, input);  
        stringstream ss(input);  
        vector<string> words;  
        string word;  
        while (ss >> word) {  
            words.push_back(word);  
        }  
        reverse(words.begin(), words.end());  
        for (int i = 0; i < words.size(); i++) {  
            cout << words[i] << ' ';  
        }  
        cout << endl;  
    }  
    return 0;  
}

Java:[此代码未进行大量数据的测试,仅供参考]

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        scanner.nextLine(); // consume the newline character after the integer input
        
        while (T-- > 0) {
            String input = scanner.nextLine();
            String[] words = input.split(" ");
            List<String> wordList = Arrays.asList(words);
            Collections.reverse(wordList);

            for (int i = 0; i < wordList.size(); i++) {
                System.out.print(wordList.get(i));
                if (i < wordList.size() - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        
        scanner.close();
    }
}

Python:[此代码未进行大量数据的测试,仅供参考]

def main():
    T = int(input())
    
    for _ in range(T):
        input_line = input().strip()
        words = input_line.split()
        words.reverse()
        
        print(" ".join(words))

第三题

题目

灵小层踏上了一场充满神秘的符文之旅。他手中拥有记录着神秘符文的字符串  s1 。经过漫长的探索,灵小层找到了另外一串神秘符文序列  s2 。他的任务是解开这两个符文之间的联系:确定是否可以在  s2  中找到  s1  的一个排列。

输入描述

第一行是一个正整数 T;

接下来T组数据,每组包含两个字符串s1和s2 。

输出描述

对于每组数据,输出 s2 是否包含s1的排列,输出 true 或 false。

样例输入

2

lingxi

aerlxngii

lingxi

aerlxngixi

样例输出

true

false

参考题解

用哈希表存下s1串每个字母出现的次数,用滑动窗口枚举s2串,保持窗口大小为s1.size(),如果窗口内的字符出现次数和s1是一致的,说明找到了答案。

C++:[此代码未进行大量数据的测试,仅供参考]

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
int T;

string s, t;

int main()
{
    cin >> T;
    
    while(T --)
    {
        cin >> s;
        unordered_map<char, int> mp1, mp2;
        for(char c : s ) mp1[c] ++;
        cin >> t;
        int n = s.size(), m = t.size();
        if(n > m)
        {
            puts("false");
            continue;
        }
        bool flag = false;
        for(int i = 0, j = 0; i < m; i ++)
        {
            while(j < m && j - i < n)
            {
                mp2[t[j]] ++;
                j ++;
            }
            if(j - i < n) break;
            if(mp1 == mp2) 
            {
                flag = true;
                break;
            }
            mp2[t[i]] --;
            if(mp2[t[i]] == 0) mp2.erase(t[i]);
        }
        if(flag) puts("true");
        else puts("false");
        
    }
    return 0;
}

Java:[此代码未进行大量数据的测试,仅供参考]

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();

        while (T-- > 0) {
            String s = scanner.next();
            String t = scanner.next();
            HashMap<Character, Integer> mp1 = new HashMap<>();
            HashMap<Character, Integer> mp2 = new HashMap<>();
            
            // Populate mp1 with the character frequencies of str

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

2024 BAT笔试合集 文章被收录于专栏

持续收录字节、腾讯、阿里、美团、美团、拼多多、华为等笔试题解,包含python、C++、Java多种语言版本,持续更新中。

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务