备战蓝桥杯JAVA B组Day10
目录
- 备战蓝桥杯JAVA B组Day10
- P1428 小鱼比可爱
- P1427 小鱼的数字游戏
- P5727 【深基5.例3】冰雹猜想
- P1047 [NOIP2005 普及组] 校门外的树
- P5728 【深基5.例5】旗鼓相当的对手
前言
零基础小白备战蓝桥杯第十天,刷题内容为:洛谷题单【入门3】循环结构。
P1428 小鱼比可爱
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
count++;
}
}
System.out.print(count+" ");
}
}
}
补充:
双层循环。
P1427 小鱼的数字游戏
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[100];
int count = 0;
for (int i = 0; ; i++) {
arr[i] =sc.nextInt();
if(arr[i]==0){
break;
}
count = i;
}
for (int i = count; i >= 0; i--) {
System.out.print(arr[i]+" ");
}
}
}
补充:
创建数组,利用循环输入,以输入数是否为0作为判断条件跳出循环,再反向循环打印。
P5727 【深基5.例3】冰雹猜想
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[1000];
int count = 0;
arr[0] = n;
for (int i = 1; n != 1;i++) {
if (n % 2 == 1) {
n = n * 3 + 1;
} else {
n = n / 2;
}
arr[i] = n;
count++;
}
for (int j = count;j >= 0; j--) {
System.out.print(arr[j]+" ");
}
}
}
补充:
注意数组设置稍微大一些,设置100不能AC。
P1047 [NOIP2005 普及组] 校门外的树
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int m = sc.nextInt();
int []arr = new int[l+1];
int count = 0;
for (int i = 0; i <= l; i++) {
arr[i] = i;
}
while (m!=0){
int p = sc.nextInt();
int q = sc.nextInt();
for (int i = p; i <= q; i++) {
arr[i] = -1;
}
m--;
}
for (int i = 0; i <=l; i++) {
if(arr[i] >= 0){
count++;
}
}
System.out.print(count);
}
}
补充:
注意创建的数组长度l+1。
P5728 【深基5.例5】旗鼓相当的对手
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [][]arr = new int[n+1][n+1];
int []sum = new int[n+1];
int count = 0;
for (int i = 1; i <= n; i++) {
arr[i][1] = sc.nextInt();
arr[i][2] = sc.nextInt();
arr[i][3] = sc.nextInt();
sum[i] = arr[i][1] + arr[i][2] + arr[i][3];
}
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
if(Math.abs(arr[i][1]-arr[j][1])<=5&&
Math.abs(arr[i][2]-arr[j][2])<=5&&
Math.abs(arr[i][3]-arr[j][3])<=5&&
Math.abs(sum[i]-sum[j])<=10){
count++;
}
}
}
System.out.print(count);
}
}
补充:
使用了二维数组,注意创建的数组长度n+1,循环时,不和自己比,所以第二个变量要从第一个变量+1开始。
标签:count,arr,JAVA,Scanner,int,蓝桥,nextInt,Day10,sc From: https://blog.csdn.net/2401_88077378/article/details/143272050