部分和
public class _部分和 {
private static int kk;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0;i < n;i++) {
a[i] = sc.nextInt();
}
int k = sc.nextInt();
kk = k;
dfs(a,k,0,new ArrayList<Integer>());
}
public static void dfs(int[] a, int k, int cur, ArrayList<Integer> ints) {
if(k == 0) {
System.out.print("Yes("+kk+" = ");
int size = ints.size();
for(int i = 0;i < size;i++) {
System.out.print(ints.get(i)+(i==size-1?"":" + "));
}
System.out.println(")");
System.exit(0);
}
if(k < 0 || cur == a.length)
return;
dfs(a,k,cur+1,ints);
ints.add(a[cur]);
int index = ints.size()-1;
dfs(a,k-a[cur],cur+1,ints);
ints.remove(index);
}
}
机器人走方格
public class _机器人走方格 {
public static void main(String[] args) {
System.out.println(solve_1(3,3));
System.out.println(solve_2(3,3));
}
public static int solve_1(int i, int j) {
if(i == 1 || j == 1)
return 1;
return solve_1(i,j-1) + solve_1(i-1,j);
}
public static int solve_2(int i, int j) {
int[][] arr = new int[i][j];
for(int m = 0; m < j;m++) {
arr[m][0] = 1;
}
for(int n = 0; n < i; n++) {
arr[0][n] = 1;
}
for(int m = 1; m < i ;m++) {
for(int n = 1;n < j; n++) {
arr[m][n] = arr[m][n-1] + arr[m-1][n];
}
}
return arr[i-1][j-1];
}
}
困难的串
public class _困难的串 {
public static void main(String[] args) {
int n = 10;
int l = 4;
dfs(l,n,"");
}
static int count;
public static void dfs(int l, int n, String prefix) {
for(char i = 'A'; i < 'A' + l; i++) {
if(isHard(prefix, i)) {
String x = prefix + i;
System.out.println(x);
count++;
if(count == n)
System.exit(0);
dfs(l,n,x);
}
}
}
public static boolean isHard(String prefix, char i) {
int count = 0;
for(int j = prefix.length()-1; j >= 0; j -= 2) {
final String s1 = prefix.substring(j, j + count +1);
final String s2 = prefix.substring(j + count + 1) + i;
if(s1.equals(s2))
return false;
count++;
}
return true;
}
}
数独游戏
public class _数独游戏 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] table = new char[9][];
for(int i = 0;i<9;i++) {
table[i] = sc.nextLine().toCharArray();
}
dfs(table,0,0);
}
public static void dfs(char[][] table, int x, int y) {
if(x == 9) {
print(table);
System.exit(0);
}
if(table[x][y] == '0') {
for(int k = 1;k<10;k++) {
if(check(table,x,y,k)) {
table[x][y] = (char)('0'+k);
dfs(table,x+(y+1)/9,(y+1)%9);
}
}
table[x][y] = '0';
}else {
dfs(table,x+(y+1)/9,(y+1)%9);
}
}
public static boolean check(char[][] table, int x, int y, int k) {
for(int l = 0;l < 9;l++) {
if(table[x][l] == (char)('0'+k))
return false;
if(table[l][y] == (char)('0'+k))
return false;
}
for(int l = (x/3)*3;l<(x/3+1)*3;l++) {
for(int m = (y/3)*3;m<(y/3+1)*3;m++) {
if(table[l][m] == (char)('0'+k))
return false;
}
}
return true;
}
public static void print(char[][] table) {
for(int i = 0;i<table.length;i++) {
System.out.println(new String(table[i]));
}
}
}
素数环
public class _素数环 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] r = new int[n];
r[0] = 1;
dfs(n,r,1);
}
public static void dfs(int n, int[] r, int cur) {
if(cur == n && isP(r[0] + r[n-1])) {
print(r);
return;
}
for(int i = 2; i <= n; i++) {
if(check(r,i,cur)) {
r[cur] = i;;
dfs(n,r,cur+1);
r[cur] = 0;
}
}
}
public static boolean check(int[] r, int i, int cur) {
for(int e : r) {
if(e == i || !isP(r[cur-1] + i))
return false;
}
return true;
}
public static boolean isP(int k) {
for(int i = 2; i*i <= k; i++) {
if(k % i == 0)
return false;
}
return true;
}
public static void print(int[] r) {
for(int i = 0; i < r.length; i++) {
System.out.print(r[i] + (i==r.length-1?"":" "));
}
System.out.println();
}
}
硬币表示
public class _硬币表示 {
public static void main(String[] args) {
System.out.println(countWays(100));
}
public static int countWays(int n) {
if(n <= 0)
return 0;
return countWaysCore(n,new int[]{1,5,10,25},3);
}
public static int countWaysCore(int n, int[] coins, int cur) {
if(cur == 0)
return 1;
int res = 0;
for(int i = 0; i*coins[cur] <= n;i++) {
int left = n - i*coins[cur];
res += countWaysCore(left,coins,cur-1);
}
return res;
}
}