前言
阿里笔试比字节还难,如果你能 A 0-1道,那么你是个努力的臭弟弟,如果你能 A 一道,人上人,能 A 两道,你一定是Acmer。
祝所有秋招的同学offer拿到手软
如果你从本文中学习到丝毫知识,那么请您点点关注、点赞、评论和收藏
大家好,我是爱做梦的鱼,我是东北大学大数据实验班大三的小菜鸡,非常渴望优秀,羡慕优秀的人,个人博客为:爱做梦的鱼https://zihao.blog.csdn.net/,微信公众号、微信视频号为【程序猿干货铺】,qq交流群为:1107710098,
一、字符串翻转
对于一个01字符串,每次只能
- 交换任意两个元素
- 把一个0变成1或者把一个1变成0
- 翻转整个串
代码:A了0.7
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @Author: 张志浩 Zhang Zhihao
* @Email: 3382885270@qq.com
* @Date: 2020/8/28
* @Time: 19:13
* @Version: 1.0
* @Description: Description
*/
public class First {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
String t = sc.next();
sc.close();
int a1 = 0, a2 = 0, t1 = 0, t2 = 0;
for (int i = 0; i < n; i++) {
char p1 = s.charAt(i), p2 = t.charAt(i);
if (p1 != p2) {
if (p1 != '0') t1++;
else t2++;
}
}
int tmp = 0;
if (t1 < t2) {
tmp = t1;
t1 = t2;
t2 = tmp;
}
a1 = t1;
t1 = 0;
t2 = 0;
for (int i = 0; i < n; i++) {
char p1 = s.charAt(i), p2 = t.charAt(n - i - 1);
if (p1 != p2) {
if (p1 == '0') t1++;
else t2++;
}
}
if (t1 < t2) {
tmp = t1;
t1 = t2;
t2 = tmp;
}
a2 = t1 + t2;
System.out.println(Math.min(a1, a2));
}
}
二、数字排列
给定两个数 n
和 m
,对 n
中的数重新排列,计算出经过重新排列后所得到的数中满足不含有前导零并且能够整除 m
的数字有多少个?
例如,n = 520,那么重新排列后:520,502,250,205,025,052,能被 m = 2整除的,并且无前导零的为 520,502,250三个
代码:全A
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @Author: 张志浩 Zhang Zhihao
* @Email: 3382885270@qq.com
* @Date: 2020/8/28
* @Time: 19:42
* @Version: 1.0
* @Description: Description
*/
public class Second {
private static long[][] dp;
private static char[] s;
private static int m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
s = ("" + sc.nextLong()).toCharArray();
m = sc.nextInt();
sc.close();
dp = new long[(int) Math.pow(2, s.length)][m];
for (long[] a : dp)
for (int i = 0; i < a.length; i++)
a[i] = -1;
System.out.println(getDP(0, 0));
}
private static long getDP(int mask, int mod) {
if (dp[mask][mod] >= 0)
return dp[mask][mod];
if (mask == dp.length - 1) {
dp[mask][mod] = mod == 0 ? 1 : 0;
return dp[mask][mod];
}
dp[mask][mod] = 0;
boolean[] used = new boolean[10];
for (int i = 0; i < s.length; i++)
if (!used[s[i] - '0'] && (mask | 1 << i) != mask && (mask > 0 || s[i] != '0')) {
dp[mask][mod] += getDP(mask | 1 << i, (10 * mod + (s[i] - '0')) % m);
used[s[i] - '0'] = true;
}
return dp[mask][mod];
}
}
转载:https://blog.csdn.net/weixin_43124279/article/details/108286281
查看评论