public class Main2 {
public static void main(String[] args) {
for (int i = 1; ; i++) {
int sum = 0;
int p = (int) Math.pow(i,3);
while (p != 0) {
sum += p%10;
p/=10;
}
if (sum == i) {
System.out.println(i);
}
}
}
}
import java.util.HashSet;
import java.util.Set;
public class Main2 {
public static void main(String[] args) {
int a;//祥
int b;//瑞
int c;//生
int d;//辉
int e;//三
int f;//羊
int g;//献
int h;//气
Set set = new HashSet<>();
for (a = 1; a <= 9; a++) {
for ( b = 0; b <= 9; b++) {
for ( c = 0; c <= 9; c++) {
for ( d = 0; d <= 9; d++) {
for ( e = 1; e <= 9; e++) {
for ( f = 0; f <= 9; f++) {
for ( g = 0; g <= 9; g++) {
for ( h = 0; h <= 9; h++) {
set.clear();
set.add(a);
set.add(b);
set.add(c);
set.add(d);
set.add(e);
set.add(f);
set.add(g);
set.add(h);
int w = a*1000+b*100+c*10+d;
int q = e*1000+f*100+g*10+b;
int z = e*10000+f*1000+c*100+b*10+h;
if(w+q == z && set.size() == 8) {
System.out.println(q);
}
}
}
}
}
}
}
}
}
}
}
public static int f(int n, int m){
n = n % m;
Vector v = new Vector();
for(;;){
v.add(n);
n *= 10;
n = n % m;
if(n==0) return 0;
if(v.indexOf(n)>=0) _________________________________ ; //填空
}
}
注意,只能填写缺少的部分,不要重复抄写已有代码。不要填写任何多余的文字。
public class A
{
public static void test(int[] x)
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) System.out.println(a + " " + b);
}
public static void f(int[] x, int k)
{
if(k>=x.length){
test(x);
return;
}
for(int i=k; i
注意,只能填写缺少的部分,不要重复抄写已有代码。不要填写任何多余的文字。
public class Main
{
public static void main(String[] args) {
for (int i = 1; i < 49; i++) {
for (int j = 1; j < 49; j++) {
if (i*(i+1) + j*(j+1) -(2*i+2*j+2)== 2015-1225) {
System.out.println(i);
}
}
}
}
}
public class Main {
static int sum;
static int ans;
public static void main(String[] args) {
dfs(1);
System.out.println(ans);
}
public static void dfs(int n) {
if (sum > 13) {
return;
}
if ( n > 13) {
if (sum == 13) {
ans++;
return;
}
} else {
for (int i = 0; i < 5; i++) {
sum += i;
dfs(n+1);
sum -= i;
}
}
}
}
解析:题目难点在于想到使用矩阵存放数据,想到这种方法之后还要懂得如何求矩阵快速幂,不然肯定是会超时的。
关于矩阵快速幂可以参考下面的文章斐波那契数列(二)--矩阵优化算法
import java.util.Scanner;
public class Main2 {
static final double MOD = 10e9-7;
static int[][] arr = new int[6][6];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
/**
* 初始化arr数组
* */
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j] = 1;
}
}
for (int i = 0; i < m; i++) {
int a = input.nextInt();
int b = input.nextInt();
arr[a-1][b-1] = 0;
arr[b-1][a-1] = 0;
}
/**
*
* */
int[][] ans = pow(arr, n-1);
int sum = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
sum += ans[i][j]%MOD;
}
}
/**
* 旋转情况 4^n
* */
sum *= Math.pow(4, n)%MOD;
System.out.println((int)(sum%MOD));
}
private static int[][] pow(int[][] arr, int k) {
/**
* 单位矩阵
* */
int[][] ans = new int[6][6];
for (int i = 0; i < 6; i++) {
ans[i][i] = 1;
}
/**
* 矩阵快速幂核心算法
* */
while (k != 0) {
if (k % 2 != 0) {
ans = Multiply(arr, ans);
}
/**
* 每次算多加一颗骰子,这样算比单纯n次相乘要快
* */
arr = Multiply(arr, arr);
k >>= 1;
}
return ans;
}
private static int[][] Multiply(int[][] m, int[][] n) {
// 标准计算矩阵乘法算法
int rows = m.length;
int cols = n[0].length;
int[][] r = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < m[i].length; k++) {
r[i][j] += (m[i][k] * n[k][j])%MOD;
}
}
}
return r;
}
}
import java.util.Scanner;
public class Main2 {
static Scanner in = new Scanner(System.in);
static final int N = 10005;
static int[] v = new int[N];
static int[] vis = new int[N];
static int[][] dp = new int[N][2];
static int[][] node = new int[N][N];
static int n, sum = 0, max = 0;
public static void main(String[] args) {
init();
dfs(1);
int ans = -1;
for(int i=1 ;i<=n ;i++){
ans = Math.max(ans,dp[i][1]);
ans = Math.max(ans,dp[i][0]);
}
System.out.println(ans);
}
private static void init() {
n = in.nextInt();
for (int i = 1; i <= n; i++) {
v[i] = in.nextInt();
}
for (int i = 1; i < n; i++) {
int a = in.nextInt();
int b = in.nextInt();
node[a][b] = 1;
node[b][a] = 1;
}
}
private static void dfs(int u) {
/**
* 选择 dp[u][1],存放选择u能得到的最大权值
* 不选择 dp[u][0],存放不选择u能得到的最大权值
* */
dp[u][1] = v[u];
dp[u][0] = 0;
vis[u] = 1;
for(int i=1; i