java 洛谷P1042 乒乓球
第一次没满分,上网搜了一下规则,达到满分,思路规则如下
题解
设华华得分sumW,对手得分sumL;
题意:
- 给定字符串,出现W时sumW++,出现L时sumL++;
- 结束比赛标志(比赛规则):
11分制,当一方先达到11分,且双方成绩差大于2,结束比赛,输出双方分数;
21分制,当一方先达到21分,且双方成绩差大于2,结束比赛,输出双方分数;
或遇见E,比赛结束,输出双方分数
代码:
import java.util.Scanner;
public class P1042乒乓球 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNext()) {
String string = scanner.nextLine();
stringBuilder.append(string);
if (string.contains("E")) {
break;
}
}
char[] chars = stringBuilder.toString().toCharArray();
sort2(chars,11);
System.out.println();
sort2(chars,21);
}
//分制
private static void sort2(char[] chars, int b) {
int sumW = 0, sumL = 0;
for(int i=0;chars[i]!='E';i++) {
if(chars[i]=='W'){
sumW++;
}else {
sumL++;
}
if(Math.abs(sumW-sumL)>=2 && (sumW>=b||sumL>=b)) {
System.out.println(sumW+":"+sumL);
sumW=0;sumL=0;
}
}
System.out.println(sumW+":"+sumL);
}
}
P2670扫雷游戏
- 题意比较简单,所以就直接附上代码啦
- 下面代码涉及到数组越界问题的思路
- 注意一下的i是从1开始的,数组总长度是arr.length+2;
/**扫描模拟时,避免数组越界问题
* 数组整体向右平移
*/
import java.util.Scanner;
public class P2670扫雷游戏_01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(), m = scanner.nextInt();
char[][] arr = new char[n + 2][m + 2];
scanner.nextLine();
for (int i = 1; i < arr.length - 1; i++) {
char[] chars = scanner.nextLine().toCharArray();
for (int j = 1; j < arr[i].length-1 ; j++) {
arr[i][j] = chars[j-1];
}
}
//初始化已成功
mum(arr);
for (int i = 1; i < arr.length -1; i++) {
for (int j = 1; j < arr[i].length-1; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
//检测周围八个有几个雷
private static void mum(char[][] arr) {
for (int i = 1; i < arr.length - 1; i++) {
for (int j = 1; j < arr[i].length - 1; j++) {
int num = 0;
if (arr[i][j]=='*') {
continue;
}else{
for (int m = i - 1; m <= i + 1; m++) {
for (int s = j - 1; s <= j + 1; s++) {
if ( arr[m][s] == '*') {
num++;
}
}
}
}
arr[i][j] = (char) (num + '0');
}
}
}
}
评论区