题目讲解
北京欢迎你
输出:
**************************
* Welcome to BeiJing! *
**************************
要注意的点应该就是输出换行, 以及这类题目直接复制题目中的字符串,避免手敲出错
public class Main {
public static void main(String[] args) {
System.out.println("**************************");
System.out.println("* Welcome to BeiJing! *");
System.out.println("**************************");
}
}
A+B
Scanner固定用法: import java.util.Scanner; + Scanner scanner = new Scanner(System.in);
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + b);
}
}
四则运算
注意: 整数除法会直接取整,运算符前后要有空格
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a + " + " + b + " = " + (a + b));
System.out.println(a + " - " + b + " = " + (a - b));
System.out.println(a + " * " + b + " = " + (a * b));
System.out.println(a + " / " + b + " = " + (a / b));
}
}
温度单位的转换
给定一个整数C,表示摄氏度。你需要将C转换为华氏度。 给出了转换公式F=C*九分之五+32。 输出转换后的华氏度,结果只保留整数部分。
关键点: 使用 9.0而不是 9来确保进行浮点数运算
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int C = scanner.nextInt();
int F = (int)(C * 9.0 / 5 + 32); // 使用9.0避免整数除法
System.out.println(F);
}
}
输出格式的练习
格式化输出技巧:
%02d:整数输出,宽度为2,不足补0%.3f:浮点数输出,保留3位小数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取输入的分钟数a
int a = scanner.nextInt();
// 读取c和d
int c = scanner.nextInt();
int d = scanner.nextInt();
// 最终目的是获得a分钟后的时和分
// 首先a/60的商就是有多少小时
int hours = a / 60;
// 余数就是剩多少分钟
int minutes = a % 60;
// 但是小时有可能超过24小时, 所以小时/24的余数就是最终的小时数
hours %= 24;
// 输出最终结果, 格式为HH:MM
System.out.printf("%02d:%02d%n", hours, minutes);
double result = (double) c / d; // 也可以用c*1.0/d
// 保留三位小数
System.out.printf("%.3f%n", result);
}
}
时间显示
重要: 注意数据范围,10^18必须用 long类型
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//在服务器上,朋友已经获取了当前的时间,用一个整数表示,值为从 1970 年 11 月 11 日 00:00:00 到当前时刻经过的毫秒数。
// 要注意给定的数值是不超过10^18的正整数, 而int只能表示到10^9, 所以要使用long
long n = scanner.nextLong();
// 输出格式为HH:MM:SS
n /= 1000;// 先将毫秒转换为秒
// 取余得到秒
long secs = n % 60;
n /= 60; // 秒转成分钟
long minutes = n % 60;
n /= 60; // 分钟转成小时
// 得到小时
long hours = n % 24;
// 输出结果
System.out.printf("%02d:%02d:%02d%n", hours, minutes, secs);
}
}
40的倍数
给定一个正整数 N,如果 N 是 40 的倍数,输出 N÷40 的结果,否则输出 -1。
取模运算: n % m == 0 表示n是m的倍数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n % 40 == 0) { // 如果n是40的倍数
// 输出n/40的结果
System.out.println(n / 40);
} else {
// 输出-1
System.out.println(-1);
}
}
}
判断闰年
知识点: 逻辑运算符、复合条件判断
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// 如果α是闰年,需满足以下条件之一:
//·α是4的倍数且不是100的倍数
//·α是400的倍数
if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) {
System.out.println("Y");
} else {
System.out.println("N");
}
}
}
整数大小的比较
知识点: 数据类型范围、溢出问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 要注意数据范围是2^32,int类型只能表示-2^31到2^31-1,会导致溢出
// 所以要使用long类型
long x = scanner.nextInt();
long y = scanner.nextInt();
if (x > y) {
System.out.println(">");
} else if (x == y) {
System.out.println("=");
} else {
System.out.println("<");
}
}
}
分段函数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double y = 0;
if (x >= 0 && x < 5) {
y = -x + 2.5;
} else if (x < 10) {
y = 2 - 1.5 * (x - 3) * (x - 3);
} else if (x < 30) {
y = x / 2 - 1.5;
}
// 保留小数点后三位
System.out.printf("%.3f", y);
}
}
数位计数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 通过对10整除我们可以将整数整体向右移动一位
// 可以通过记录移动次数来获取整数有多少个数位
int n = scanner.nextInt();
int count = 0;
while (n != 0) { // 当n为0时,说明已经移动到了最高位
n /= 10;
count++;
}
System.out.println(count);
}
}
妮妮的蓝桥果园2
防溢出技巧: 累加操作使用 long类型
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long sum = 0; // 一般求和操作用long的话可以防止溢出
// for循环的初始值和终止条件界定了遍历范围, 这里的初始值为0, 终止条件为n, 所以遍历范围为[0, n)
for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
sum += num;
}
System.out.println(sum);
}
}
反倍数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (i % a != 0 && i % b != 0 && i % c != 0) {
// 反倍数
cnt++;
}
}
System.out.println(cnt);
}
}
逃离高塔
优化技巧: 只关心最后一位,可以避免大数运算
public class Main {
public static void main(String[] args) {
int cnt = 0;
for (int i = 1; i <= 2025; i++) {
if ((i * i % 10 * i) % 10 == 3) { // 因为最大的2025^3已经超过了int范围所以在中途使用取余操作, 也可以声明为long来解决
cnt++;
}
}
System.out.println(cnt);
}
}
25之和
数学优化: 使用等差数列公式避免循环
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long sum = 0;
// n + (n + 1) + (n + 2) + ... + (n + 24)
for (int i = 0; i <= 24; i++) {
sum += (n + i);
}
// 另一种方法// 等差数列求和:首项n,末项n+24,共25项
// long sum = (long)(n + (n + 24)) * 25 / 2;
System.out.println(sum);
}
}
斐波那契数列
递推思想: 用前两项推导后一项
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 1;
int c = 1;
if (n <= 1) {
c = n;
} else {
for (int i = 2; i <= n; i++) {
// 计算F(i)的值
c = a + b;
// 更新F(i-1)和F(i-2)的值
a = b;
b = c;
}
}
System.out.println(c);
}
}
斐波那契数列求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 2, b = 3;
int c;
double sum = 0;
for (int i = 1; i <= n; i++) {
// 当前项, fib(i) / fib(i+1)
double cur = (double) a / b;
sum += cur;
// 计算斐波那契数列的下一项
c = a + b;
a = b;
b = c;
}
// 保留5位小数
System.out.printf("%.5f", sum);
}
}
竞赛技巧总结
- 输入输出优化:
- 熟练使用Scanner的基本方法
- 掌握
printf格式化输出
- 数据类型选择:
- 注意数据范围,及时使用long
- 浮点数运算注意精度问题
- 代码规范:
- 变量命名要有意义
- 适当添加注释
- 注意代码缩进
- 调试技巧:
- 使用打印语句调试
- 测试边界情况