public class Jingdong {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//a辆坦克
int b = sc.nextInt();//b点HP
int c = sc.nextInt();//每个城堡可以攻击坦克数量
int d = sc.nextInt();//d座城堡
int[] chengbao = new int[d];
for (int i = 0; i < d; i++) {
chengbao[i] = b;
}
int count = 0;//轮次
int i = 0;
int k = d;
while (a > 0 && k > 0) {
//攻击
int tmpa = a;
if (tmpa >= chengbao[i]) {
for (; i < d; i++) {
if (tmpa >= chengbao[i]) {
tmpa -= chengbao[i];
chengbao[i] = 0;
} else break;
}
if (chengbao[d - 1] != 0) {
if (tmpa > 0) {
chengbao[i] -= tmpa;
tmpa = 0;
}
}
} else {
chengbao[i] -= tmpa;
tmpa = 0;
}
//反击
k = d - i;
a -= c * k;
count++;
}
if (chengbao[d - 1] == 0) {
System.out.println(count);
} else {
System.out.println(-1);
}
}
}
第一题比较简单,按照逻辑走就行。
第二题,第二个反转的K填错位置,可惜了,不然应该都能过的。
public class Jingdong2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] array = new int[m][3];
int mass =Integer.MAX_VALUE;
for (int k = 0; k < m; k++) {
for (int l = 0; l < 3; l++) {
array[k][l] = sc.nextInt();
}
}
int count = 0;
ArrayList<Integer> integers = new ArrayList<>();
for (int i = 1; i <= n; i++) {
for (int k = 0; k < m; k++) {
if (array[k][0] == i){
count++;
mass = Math.min(array[k][2], mass);
}else continue;
}
if (count==n-1) integers.add(mass);
count = 0;
mass = Integer.MAX_VALUE;
}
for (int i = 1; i <= n; i++) {
for (int k = 0; k < m; k++) {
if (array[k][1] == i){//这个位置提交的时候错了,麻了。
count++;
mass = Math.min(array[k][2], mass);
}else continue;
}
if (count==n-1) integers.add(mass);
count = 0;
mass = Integer.MAX_VALUE;
}
Integer max = Collections.max(integers);
System.out.println(max);
}
}
估计凉了,给大家参考一下吧,这应该是最普通最垃圾的写法了。