腾讯java工程师笔试题,腾讯开发岗笔试题解Java

第一题:就是一个找规律

public class Q1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

long n = sc.nextInt();

long m = sc.nextInt();

long count = m * m;

long sum = n / (2 * m) * count;

System.out.println(sum);

}

}

第二题:dp

public class Q2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int K = sc.nextInt();

int A = sc.nextInt();

int X = sc.nextInt();

int B = sc.nextInt();

int Y = sc.nextInt();

int[][] dp = new int[X+Y+1][K+1];

for (int i = 0; i < X+Y+1; i++)

dp[i][0] = 1;

for (int i = 1; i < X+Y+1; i++) {

for (int j = 1; j <= K; j++) {

if (i <= X) {

if (j >= A) dp[i][j] = (dp[i-1][j] + dp[i-1][j-A]) % 1000000007;

else dp[i][j] = dp[i-1][j] % 1000000007;

}

else if (i <= X+Y) {

if (j >= B) dp[i][j] = (dp[i-1][j] + dp[i-1][j-B]) % 1000000007;

else dp[i][j] = dp[i-1][j] % 1000000007;

}

}

}

System.out.println(dp[X+Y][K]);

}

}

第三题:贪心,排序求和

public class Q3 {

static class Pair {

int time;

int level;

public Pair(int time, int level) {

this.time = time;

this.level = level;

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int m = sc.nextInt();

Pair[] machine = new Pair[n];

Pair[] task = new Pair[m];

// input

for (int i = 0; i < n; i++)

machine[i] = new Pair(sc.nextInt(), sc.nextInt());

for (int i = 0; i < m; i++)

task[i] = new Pair(sc.nextInt(), sc.nextInt());

int[] cnt = new int[105];

// sort

Comparator comparator = (a, b) -> {

if (a.time == b.time) return b.level - a.level;

else return b.time - a.time;

};

Arrays.sort(machine, comparator);

Arrays.sort(task, comparator);

long sum = 0;

int j = 0, cnt1 = 0;

for(int i = 0;i < m;i++) {

while(j < n && machine[j].time >= task[i].time) {

cnt[machine[j].level]++;

j++;

}

for(int k = task[i].level; k < 101; k++) {

if(cnt[k] != 0) {

cnt[k]--;

sum += 200*task[i].time + 3*task[i].level;

cnt1++;

break;

}

}

}

System.out.println(cnt1 + " " + sum);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这道题是一道字符串处理题。给定一个字符串 s,它的所有的字符都是小写的英文字母。要求把这个字符串变成一个按照字典序排序的字符串,并且要求在变换过程中只能在原来的字符串中交换两个相邻的字符。 题思路: - 从前往后扫描字符串 s,找到第一个不满足字典序的字符,记为字符 x。 - 从 x 往后扫描,找到最后一个比 x 大的字符 y,将 x 与 y 交换。 - 将 x 后面的字符串倒序排列,这样就得到了字典序更大的字符串。 下面是 Java 代码的实现: ``` class Solution { public String nextPermutation(String s) { char[] chars = s.toCharArray(); // 从后往前找到第一个不满足字典序的字符 x int i = chars.length - 2; while (i >= 0 && chars[i] >= chars[i + 1]) { i--; } // 如果 i < 0,说明原来的字符串已经是字典序最大的字符串,直接返回倒序排列的字符串 if (i < 0) { reverse(chars, 0, chars.length - 1); return new String(chars); } // 从 x 往后扫描,找到最后一个比 x 大的字符 y int j = chars.length - 1; while (j > i && chars[j] <= chars[i]) { j--; } // 将 x 与 y 交换 swap(chars, i ### 回答2: 题目:LeetCode第38题:报数 题目描述: 给定一个正整数n,输出报数序列前n个数。 报数规则:从1开始报数,数到3的倍数时报Fizz,数到5的倍数时报Buzz,数到同时是3和5的倍数时报FizzBuzz,其他情况下则直接报数。 题思路: 使用循环遍历1到n的所有数字,按照报数规则进行判断并输出。 具体步骤如下: 1. 创建一个StringBuilder对象res,用于存储报数序列。 2. 使用for循环从1遍历到n。 3. 判断当前数字是否同时是3和5的倍数,如果是,则将"FizzBuzz"添加到res中。 4. 判断当前数字是否是3的倍数,如果是,则将"Fizz"添加到res中。 5. 判断当前数字是否是5的倍数,如果是,则将"Buzz"添加到res中。 6. 如果以上条件都不满足,则将当前数字转换为字符串并添加到res中。 7. 循环结束后,将res转换为字符串并返回。 Java代码如下: ```java public String countAndSay(int n) { StringBuilder res = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { res.append("FizzBuzz"); } else if (i % 3 == 0) { res.append("Fizz"); } else if (i % 5 == 0) { res.append("Buzz"); } else { res.append(Integer.toString(i)); } } return res.toString(); } ``` 以上代码可以将1到n的报数序列输出,并按照题目要求进行相应转换。 ### 回答3: 题目要求是根据给定的正整数 n,返回一个字符串,该字符串包含从 1 到 n 的所有数字对应的字符串,并且满足以下条件: 1. 如果数字能被 3 整除,则使用字母 "Fizz" 替代该数字。 2. 如果数字能被 5 整除,则使用字母 "Buzz" 替代该数字。 3. 如果数字能同时被 3 和 5 整除,则使用字母 "FizzBuzz" 替代该数字。 题思路: 利用循环遍历从 1 到 n 的所有数字,使用条件语句判断每个数字是否满足以上三个条件,然后根据条件替换数字并存入结果字符串中,最后返回结果。 Java代码如下: ```java class Solution { public String fizzBuzz(int n) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { result.append("FizzBuzz"); } else if (i % 3 == 0) { result.append("Fizz"); } else if (i % 5 == 0) { result.append("Buzz"); } else { result.append(i); } if (i != n) { result.append(" "); } } return result.toString(); } } ``` 这段代码使用StringBuilder来构建结果字符串,判断每个数字是否满足条件,并根据条件拼接对应的字符串,每个数字之间用空格隔开。最后将StringBuilder转换成String并返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值