import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int n = in.nextInt();
int m = in.nextInt();
int nums[] = new int[n];
int re[] = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
int count = n/m; //需要多少个车
int res = n%m; //最后一个车坐多少人
int index = 0;
for (int i = 0; i < res; i++) { //最后一车人先上车
re[index++] = nums[count*m+i];
}
for (int i = count-1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
re[index++] = nums[i*m+j];
}
}
for (int i = 0; i < re.length; i++) {
System.out.print(re[i]+" ");
}
System.out.println();
}
}
}
是动态规划的思想,但是数字太大了,只过了70%肯定是因为数字太大然后溢出了。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//动态规划问题,dp[i][j]表示第i次击鼓的时候,0在手里1不在手里
Scanner in = new Scanner(System.in);
int mod = 1000000007;
while(in.hasNext()) {
int n = in.nextInt(); //击鼓次数
int k = in.nextInt(); //人个数
long[][] dp = new long[n+1][2];
//设置初始值,初始的时候在手里
dp[0][0] = 1;
dp[0][1] = 0;
for (int i = 1; i < dp.length; i++) {
dp[i][0] = dp[i-1][1]%mod; //如果这次在手里,那上次肯定不在手里,并且这一轮只有一种可能
dp[i][1] = (dp[i-1][0]%mod*(k-1) + dp[i-1][1]%mod*(k-2))%mod; //这次不在手里,上次可能在手里也可能不在手里
}
System.out.println(dp[n][0]);
}
}
}
这道题应该算力扣上的hard了吧!
明明我照着下面一个答案敲的,明明检查很多遍就是一样的,但是就是死活过不了,气死我了。最后直接copy,ac,完美
力扣有一道差不多的,但是没有可以用啥卡的说法,这个dp数组多了一个维度记录用或者不用求救卡的情况
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int m = in.nextInt();
int n = in.nextInt();
int k = in.nextInt();
int[][] matrix = new int[m][n];
int[][][] memo = new int[m][n][k+1]; //保存记录
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = in.nextInt();
}
}
int max = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) { //从迷宫的每个位置为起点开始搜索
int count = getNum(memo, matrix, m, n, i, j, k);
max = count > max?count:max;
}
}
System.out.println(max);
}
}
public static int getNum(int[][][] dp,int[][] group,int M,int N,int x,int y,int k){
if(k<0 || y<0 || x<0){ //越界判断
return 0;
}
if(dp[x][y][k] != 0){ //已经计算过
return dp[x][y][k];
}
int g1 = 0;
int g2 = 0;
int g3 = 0;
int g4 = 0;
//分别向上下左右四个方向进行尝试,需要判断是否用到紧急呼救按钮次数
if(x-1 >= 0) {
if(group[x-1][y] <= group[x][y]) {
g1 = getNum(dp,group,M,N,x-1,y,k-1);
}
else {
g1 = getNum(dp,group,M,N,x-1,y,k);
}
}
if(y-1 >= 0) {
if(group[x][y-1] <= group[x][y]) {
g2 = getNum(dp,group,M,N,x,y-1,k-1);
}
else {
g2 = getNum(dp,group,M,N,x,y-1,k);
}
}
if(x+1 < N){
if(group[x+1][y] <= group[x][y]) {
g3 = getNum(dp,group,M,N,x+1,y,k-1);
}
else {
g3 = getNum(dp,group,M,N,x+1,y,k);
}
}
if(y+1 < M){
if(group[x][y+1] <= group[x][y]) {
g4 = getNum(dp,group,M,N,x,y+1,k-1);
}
else {
g4 = getNum(dp,group,M,N,x,y+1,k);
}
}
dp[x][y][k] = Math.max(Math.max(g1, g2),Math.max(g3, g4)) +1;
return dp[x][y][k];
}
}