小红书笔试 小红书笔试题 0820
笔试时间:2025年8月20日
往年笔试合集:
第一题:小红薯的好数
小红薯定义一个点赞数n为好数,当且仅当这个数的所有本质不同质因子之和为奇数。例如,12的本质不同质因子为{2,3},且2+3=5为奇数,因此12是好数。特别地,当n=1时无质因子,本质不同质因子之和视为0。现在,首页上有好多Plog,请帮助小红薯判断每条Plog的点赞数都是不是好数。
输入描述
每个测试文件均包含多组测试数据。第一行输入一个整数代表数据组数T,每组测试数据描述如下:
第一行输入一个整数,表示Plog的点赞数。
输出描述
对于每组测试数据,新起一行,如果点赞数是好数,输出YES,否则输出NO。
样例输入
3
2
5
12
样例输出
NO
YES
YES
说明:对于样例数据,2的本质不同质因子只有2,而2不是奇数,因此不是好数
参考题解
一个数x需要寻找其质数因子,只需要寻找到sqrt(x),从小到大枚举2到sqrt(x)的每个数,如果当前x对枚举值取模为0,则代表是他的质因子,找出所有不同质因子后求和输出结果即可。
C++:
#include <bits/stdc++.h>
using namespace std;
long long get_sum_distinct_prime_factors(long long number) {
if (number <= 1) return 0;
long long total = 0;
if (number % 2 == 0) {
total += 2;
while (number % 2 == 0) number /= 2;
}
for (long long i = 3; i <= number / i; i += 2) {
if (number % i == 0) {
total += i;
while (number % i == 0) number /= i;
}
}
if (number > 1) total += number;
return total;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
if (!(cin >> t)) return 0;
while (t--) {
long long n;
cin >> n;
long long s = get_sum_distinct_prime_factors(n);
if (s % 2 == 1) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
Java:
import java.io.*;
import java.util.*;
public class Main {
// 计算“不同素因子之和”
static long get(long number) {
if (number <= 1) return 0L;
long total = 0L;
if (number % 2 == 0) {
total += 2;
while (number % 2 == 0) number /= 2;
}
for (long i = 3; i <= number / i; i += 2) {
if (number % i == 0) {
total += i;
while (number % i == 0) number /= i;
}
}
if (number > 1) total += number;
return total;
}
// 简单快速输入
static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
long nextLong() throws IOException {
int c; do { c = read(); } while (c <= 32);
int sign = 1;
if (c == '-') { sign = -1; c = read(); }
long x = 0;
while (c > 32) {
x = x * 10 + (c - '0');
c = read();
}
return x * sign;
}
int nextInt() throws IOException { return (int) nextLong(); }
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = fs.nextInt();
for (int k = 0; k < t; k++) {
long n = fs.nextLong();
long s = get(n);
out.println((s & 1L) == 1L ? "YES" : "NO");
}
out.flush();
}
}
Python:
import math
def get(number):
if number <= 1:
return0
total = 0
if number % 2 == 0:
total += 2
while number % 2 == 0:
number //= 2
i = 3
while i * i <= number:
if number % i == 0:
total += i
while number % i == 0:
number //= i
i += 2
if number > 1:
total += number
return total
def main():
import sys
data = sys.stdin.read().split()
t = int(data[0])
index = 1
for _ in range(t):
n = int(data[index])
index += 1
s = get(n)
if s % 2 == 1:
print("YES")
else:
print("NO")
if __name__ == "__main__":
main()
第二题
在小红书首页的两列中,小红喜欢独爱单一列。她将第一列每条的点赞状态从上到下用一个进制字符串s = (s1, s2, …, sn)表示,其中:字符si = '1'表示用户已点赞第i条;字符si = '0'表示用户未点赞第i条。小红定义一轮点赞行为如下:选择索引对1 ≤ l ≤ r ≤ n;从第l条Plog开始,到第r条Plog结束,进行一次重复点赞行为。这会使得原本未点赞的Plog变为已点赞,原本已点赞的Plog变为未点赞。小红希望使得这一列Plog的点赞状态调整为一个回文串,即第一条和最后一条Plog的点赞状态相同,第二条和倒数第二条的Plog点赞状态相同,以此类推。请计算她最少需要进行的点赞行为轮数。
输入描述
每个测试文件均包含多组测试数据。第一行输入一个整数T (1 ≤ T ≤ 10^4)代表数据组数,每组测试数据描述如下:第一行输入一个整数n (1 ≤ n ≤ 2 × 10^5),表示数量
第二行输入一个长度为n、由字符'0'和'1'构成的字符串s,表示点赞状态。除此之外,保证单个测试文件的n之和不超过2 × 10^5。
输出描述
对于每一组测试数据,新起一行,输出一个整数,代表使字符串s成为回文串所需的最少点赞行为轮数。
样例输入
2
2
01
3
010
样例输出
1
0
参考题解
最终期望组成一个回文字符串,那么首先将字符串分为前后两半,对应位置如果不同,则代表需要进行反转。把不同的位置标记为1,相同的位置标记为0,会得到一个新的01字符串,问题就简化为寻找这个字符串中有多少个连续的1子段,最后根据子段数量输出结果即可。
C++:
#include <bits/stdc++.h>
using namespace std;
// 统计二值数组中连续为 1 的段数
int count_segments(const vector<int>& arr) {
if (arr.empty()) return 0;
int cnt = 0;
int current_value = arr[0];
if (current_value == 1) cnt += 1;
for (size_t i = 1; i < arr.size(); ++i) {
int value = arr[i];
if (value != current_value) {
if (value == 1) cnt += 1;
current_value = value;
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
if (!(cin >> t)) return 0;
while (t--) {
int length;
string s;
cin >> length >> s;
if (length == 1) {
cout << 0 << "\n";
continue;
}
int half_length = length / 2;
vector<int> arr;
arr.reserve(half_length);
for (int i = 0; i < half_length; ++i) {
if (s[i] != s[length - 1 - i]) arr.push_back(1);
else arr.push_back(0);
}
cout << count_segments(arr) << "\n";
}
return 0;
}
Java:
import java.io.*;
import java.util.*;
public class Main {
// 统计二值数组中连续为 1 的段数
static int countSegments(int[] arr) {
if (arr.length == 0) return 0;
int cnt = 0;
int current = arr[0];
if (current == 1) cnt++;
for (int i = 1; i < arr.length; i++) {
int v = arr[i];
if (v != current) {
if (v == 1) cnt++;
current = v;
}
}
return cnt;
}
// 简单高效输入
static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
String next() throws IOException {
StringBuilder sb = new StringBuilder();
int c;
do { c = read(); } while (c <= 32 && c != -1);
while (c > 32 && c != -1) {
sb.append((char)c);
c = read();
}
return sb.toString();
}
int nextInt() throws IOException { return Integer.parseInt(next()); }
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = Integer.parseInt(fs.next());
for (int _case = 0; _case < t; _case++) {
int length = Integer.parseInt(fs.next());
String s = fs.next();
if (length == 1) {
out.println(0);
continue;
}
int half = length / 2;
int[] arr = new int[half];
for (int i = 0; i < half; i++) {
arr[i] = (s.charAt(i) != s.charAt(length - 1 - i)) ? 1 : 0;
}
out.println(countSegments(arr));
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南
查看11道真题和解析