测试是对的,提交说数组越界…先码一下
1.飞行棋
输入2行
第一行两个整数:K,N。K是指原始距离终点的距离,N是指投骰子的次数。
0<=K<=600,1<=N<=100
第二行:N个整数,每个整数表示当前投骰子的点数。
输出:最终距离终点的距离和棋子由于点数过大到达终点返回的次数。
如果中间某一步恰好停在终点. 返回 “paradox”.
输入
10 4 // 距离终点10, 骰子投4次
6 3 3 3
输出
1 2 // 最后距离为1,返回了两次
package pinduoduo;
import java.util.Scanner;
/**
* @author liu
* @Description
*/
public class main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int K = in.nextInt();
int N = in.nextInt();
int sum = 0;
if(N==0){
System.out.print(K + "" + 0);
return;
}
int[] b = new int[N+1];
Scanner in2 = new Scanner(System.in);
for(int i = 0;i<N;i++){
b[i] = in2.nextInt();
}
int k = 0;
sum = - K;
for(int j = 0;j<N+1;j++){
if(b[j]>-sum){
sum =- (b[j]+sum);
k++;
}else {
sum = sum + b[j];
}
}
if(sum%K==0){
System.out.print("paradox");
}else{
System.out.print(-sum +" " +k);
}
}
}
2.骰子旋转
骰子有6个面1-6点。
输入N:骰子数N
连续输入N行:每行6个数,代表骰子‘上下前后左右’的点数
求有多少骰子, 每种多少个
输入
3 // 色子个数
1 2 3 4 5 6 // 上下前后左右点数
1 2 6 5 3 4 // 可旋转为上面这个
1 2 3 4 6 5
输出
2
2 1
3.吃饭-热量和美味
第一行:N M T。中餐可选数量N 晚餐可选数量M 要求的美味值T
接下来的N行:中餐的热量 中餐的美味值
接下来的M行:晚餐的热量 晚餐的美味值
要求美味值>=T的最小热量
输入
3 3 10 // 中餐可选数量N 晚餐可选数量M 要求的美味值T
1 1 // 热量 美味值
2 5
3 7
2 4
4 8
6 9
输出
5 (中餐选3 7 晚餐选 2 4)
数组越界…
package pinduoduo;
import java.util.Scanner;
public class main3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
int T = in.nextInt();
Scanner in2 = new Scanner(System.in);
int[] a = new int[N];
int[] b = new int[M];
int[] a2 = new int[N];
int[] b2 = new int[M];
for(int i = 0;i<N;i++){
a[i] = in2.nextInt();
a2[i] = in2.nextInt();
}
for(int j = 0;j<M;j++){
b[j] = in2.nextInt();
b2[j] = in2.nextInt();
}
// int t =T;
if(T==0) {
System.out.println(0);
return;
}
int sum1 = 0;
int sum2 = 0;
int sum = 10000;
for(int p = 0;p<N;p++){
if(a2[p]>=T&&sum>a[p]){
sum = a[p];
}
}
for(int p = 0;p<M;p++){
if(b2[p]>=T&&sum>b[p]){
sum = b[p];
}
}
for(int p = 0;p<N;p++){
for(int q = 0;q<M;q++){
if(a2[p]+b2[q]>=T&&sum>a[p]+b[q]){
sum = b[q]+a[p];
}
}
}
if(sum==10000){
System.out.print(-1);
}else {
System.out.print(sum);
}
}
}
4.种菜
6种蔬菜种在6*6的土地上,其中有部分有建筑,要求上下左右的蔬菜种类不同。