字节笔试
测开,笔试终于ak了一次
个位数求和
----------------------------------
public class Main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int T = in.nextInt();
for(int i = 0; i < T; i++){
long n = in.nextLong();
System.out.println(fun(n));
}
}
public static long fun(long n){
long res = 0L;
long tp = n/10;
res += 1L*tp*45L;
long mod = n % 10;
res += mod*(mod+1L)/2L;
return res;
}
}
---------------------------------
买东西的那一题
---------------------------------
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
long m = scanner.nextLong();
int[] wallets = new int[n];
long res = 0L;
//余数为k-1,需要补1个,以此类推
TreeMap<Integer, Integer> map = new TreeMap<>((a,b)->b.compareTo(a));
for (int i = 0; i < n; i++) {
wallets[i] = scanner.nextInt();
//取余前直接计算能买的数量,后续对余数操作
res += wallets[i] / k;
wallets[i] = wallets[i] % k;
map.put(wallets[i],map.getOrDefault(wallets[i],0)+1);
}
//遍历直接扣减
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
//key为余数,val为数量
int key = entry.getKey();
int val = entry.getValue();
//System.out.println(key +&quot; &quot;+val);
//p为当前余数需要补的数量与m比较
long p = 1L*(k-key)*(val);
if(m >= p){
m -= p;
res += val;
}else{
res = res + m/(k-key);
m = 0;
break;
}
}
//遍历完成且m>0,补在任何位置都可以
if(m > 0){
res += m/k;
}
System.out.println(res);
}
}
个位数求和
----------------------------------
public class Main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int T = in.nextInt();
for(int i = 0; i < T; i++){
long n = in.nextLong();
System.out.println(fun(n));
}
}
public static long fun(long n){
long res = 0L;
long tp = n/10;
res += 1L*tp*45L;
long mod = n % 10;
res += mod*(mod+1L)/2L;
return res;
}
}
---------------------------------
买东西的那一题
---------------------------------
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
long m = scanner.nextLong();
int[] wallets = new int[n];
long res = 0L;
//余数为k-1,需要补1个,以此类推
TreeMap<Integer, Integer> map = new TreeMap<>((a,b)->b.compareTo(a));
for (int i = 0; i < n; i++) {
wallets[i] = scanner.nextInt();
//取余前直接计算能买的数量,后续对余数操作
res += wallets[i] / k;
wallets[i] = wallets[i] % k;
map.put(wallets[i],map.getOrDefault(wallets[i],0)+1);
}
//遍历直接扣减
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
//key为余数,val为数量
int key = entry.getKey();
int val = entry.getValue();
//System.out.println(key +&quot; &quot;+val);
//p为当前余数需要补的数量与m比较
long p = 1L*(k-key)*(val);
if(m >= p){
m -= p;
res += val;
}else{
res = res + m/(k-key);
m = 0;
break;
}
}
//遍历完成且m>0,补在任何位置都可以
if(m > 0){
res += m/k;
}
System.out.println(res);
}
}
全部评论
个位数求和,用等差数列求和0-9,然后看看有多少个,最后的单独处理一下
买东西的,取余之后,贪心先补余数最大的,为了方便计算,用treemap记录,遍历map,扣减m,遍历完还有剩余直接res += m/k
哥,是双机位吗
只有一题吗 后面三题都没ac

佬,第二题咋做
相关推荐
xiaowl:你这个简历“条目上”都比较有深度性,但是实际上面试官又没法很好的评估你是怎么达到很多看上去很厉害的结果的。要避免一些看上去很厉害的包装,比如高效的内存复用策略的表达,如果仅是简单的一些内存共享机制,而且面试上也没有深挖的空间,就不要这样表达。比如,工程化模式本质上可能就是定义了一些abstract class,那也就没特别多值得讲的内容。建议简历上应该侧重那些你花了大量时间和精力解决、研究的问题,不要过分追求“丰富”,而是关注在技术深入度、问题解决能力的表现上。 点赞 评论 收藏
分享
