目录
第十二届
ASC
题目
题解
这道题只要知道ASCII码是什么就会迎刃而解。
答案:76
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(65+('L'-'A'));
scan.close();
}
}
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println((int) 'L');
scan.close();
}
}
空间
题目
题解
这道题考的是计算机网络——存储空间单位的转换:
转换公式:
位与字节转换
1B(byte字节)等于 8bit(比特或位)千字节与字节转换
1KB(kilobyte千字节)等于 1024B(byte字节)兆字节与千字节转换
1MB(兆字节)等于 1024KB(kilobyte千字节)答案:67108864
代码
public class Main {
public static void main(String[] args) {
//直接输出 256 * 1024 * 1024 * 8 / 32 会发现是负数
//并且编辑器在 256 * 1024 * 1024 * 8 处会报警告:表达式中出现数值溢出
//有下面四种解决方法
System.out.println(1024 * 1024 * 256 / 4);
System.out.println(1024 * 1024 * 256 / 32 * 8);
System.out.println((long) 256 * 1024 * 1024 * 8 / 32);
System.out.println(Math.abs(256 * 1024 * 1024 * 8 / 32));
}
}
卡片
题目
题解
看完题目的第一反应 不就是 把1消耗完(只要消耗完其中一种牌即可2/3/4...都可以只是 1 消耗的最快) 就行了吗
你也许就会得到下面的代码 很幸运你得到了正确的答案3181
不能说这道题出的不好,只能说出题的时间刚刚好好
代码
public class Main {
public static void main(String[] args) {
int count = 0;
String str;
for (int i = 1; ; i++) {
str = i + "";
for (int j = 0; j < str.length(); j++) {
if (str.charAt(j) == '1') {
count++;
}
}
if (count == 2021) {
System.out.println(str);
break;
}
}
}
}
仔细读题就能发现会出现某一张牌用不完的情况,当你将上面的程序中 count == 2021 改为count == 3 时就会发现程序陷入了死循环无法得到答案。这是因为 1 多了一张。所以我们要考虑到这种情况才能写出完美的程序。(当然考试碰到反正是填空题,如果改成编程题,那就只能希望自己是欧皇不会碰的用不完的测试用例 )。
代码
import java.util.HashMap;
public class Main {
static int N = 2021;
public static void main(String[] args) {
// 将卡片的值作为键,卡片的数量作为值,构成键值对
HashMap<Integer, Integer> card = new HashMap<>(10);
String s;
int t;
// 从 1 开始循环,没有结束条件,因为不知道能拼到什么数,得到正确答案时 return 即可
for (int i = 1; ; i++) {
// 将数字转换为字符串,方便操作
s = i + "";
for (int j = 0; j < s.length(); j++) {
t = Integer.parseInt(s.charAt(j) + "");
// 判断 card 中是否有改卡片,有则卡片数量加 1
// 否则将新的卡片加入 card 中,卡片数量初始化为 1
if (card.containsKey(t)) {
card.put(t, card.get(t) + 1);
} else {
card.put(t, 1);
}
}
// 遍历键(卡片种类),看哪种卡片消耗完了
for (int j : card.keySet()) {
if (card.get(j) == N) {
System.out.println(i);
return;
// 当卡片的数量超过拥有的数量时,则说明当前数字(i)无法拼成,
// 所以输出 i-1 (即遇到卡片有剩余又无法拼成当前数字,
// 能拼成的最大数字就是当前数字的上一个数(i-1))
} else if (card.get(j) > N) {
System.out.println(i - 1);
return;
}
}
}
}
}
参考:https://zhuanlan.zhihu.com/p/400260541
代码
public class Main {
public static void main(String[] args) {
int[] cardNum = new int[10];
// 初始化每张卡片的数量
for (int i = 0; i < 10; i++) {
cardNum[i] = 2021;
}
int temp;
int t;
// 从 1 开始循环,没有结束条件,因为不知道能拼到什么数,得到正确答案时 return 即可
for (int i = 1; ; i++) {
temp = i;
while (temp != 0) {
t = temp % 10;
if (cardNum[t] <= 0) {
System.out.println(i - 1);
return;
}
// 该数字卡片数量减 1
cardNum[t]--;
// 将数位缩小一位得到每一位数
temp /= 10;
}
}
}
}
相乘
题目
题解
这道题只要模拟题目描述的过程即可得到正确答案17812964
代码
public class Main {
public static void main(String[] args) {
for (long i = 1; i <= 1000000007; i++) {
if (i * 2021 % 1000000007 == 999999999) {
System.out.println(i);
return;
}
}
System.out.println(0);
}
}
代码
public class Main {
public static void main(String[] args) {
for (long i = 1; ; i++) {
if ((i * 1000000007 - 8) % 2021 == 0) {
System.out.println((i * 1000000007 - 8) / 2021);
break;
}
}
}
}
代码
public class Main {
public static void main(String[] args) {
for (long i = 1; ; i++) {
if ((i * 1000000007 + 999999999) % 2021 == 0) {
System.out.println((i * 1000000007 + 999999999) / 2021);
break;
}
}
}
}
时间显示
题目
题目描述
小蓝要和朋友合作开发一个时间显示的网站。
在服务器上,朋友已经获取了当前的时间,用一个整数表示,值为从 1970 年 1 月 1 日 00:00:00 到当前时刻经过的毫秒数。
现在,小蓝要在客户端显示出这个时间。小蓝不用显示出年月日,只需要显示出时分秒即可,毫秒也不用显示,直接舍去即可。
给定一个用整数表示的时间,请将这个时间对应的时分秒输出。
输入描述
输入一行包含一个整数,表示时间。
输出描述
输出时分秒表示的当前时间,格式形如
HH:MM:SS
,其中HH
表示时,值为 0 到 23,MM
表示分,值为 0 到 59,SS
表示秒,值为 0 到 59。时、分、秒 不足两位时补前导 0。输入输出样例
示例 1
输入
46800999
输出
13:00:00
示例 2
输入
1618708103123
输出
01:08:23
评测用例规模与约定
对于所有评测用例,给定的时间为不超过 10^{18} 的正整数。
运行限制
- 最大运行时间:1s
- 最大运行内存: 512M
题解
这道题只要知道时间单位的转换就没有问题
1h=60m=60*60s=60*60*1000ms
可能有人以为1s=60ms结果答案死活出不来,在这种地方被坑也是大写的惨!!!
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long num = scan.nextLong();
scan.close();
long allseconds = num / 1000;
long seconds = allseconds % 60;
long allmins = allseconds / 60;
long mins = allmins % 60;
long allhours = allmins / 60;
long hours = allhours % 24;
System.out.printf("%02d:%02d:%02d", hours, mins, seconds);
}
}
参考: https://www.lanqiao.cn/questions/222479/
如果你想使用JDK自带的时间类会发现有八个小时的时差是一个坑,但是在蓝桥云的OJ却可以通过,而蓝桥杯练习系统的试题集中无法通过(收这么多钱连个OJ都搞不明白)
代码
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//疑问就是:这个审题系统的Date日期好像默认的是从1970 01-01;00:00:00开始
Scanner sc = new Scanner(System.in);
long time = sc.nextLong();
//题目是从1970 01-01;00:00:00开始,Date 是从1970 01-01;08:00:00
//8个小时等于8*60*60*1000
Date date = new Date();
//设置毫秒值的时候减去8个小时的毫秒值
date.setTime(time - 8 * 60 * 60 * 1000);
//date.setTime(time);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(date));
}
}
参考: https://www.lanqiao.cn/questions/251715/
代码
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
Scanner scanner = new Scanner(System.in);
Calendar instance = Calendar.getInstance();
instance.setTimeInMillis(scanner.nextLong()- 8 * 60 * 60 * 1000);
String format = simpleDateFormat.format(instance.getTime());
System.out.println(format);
}
}
总结
这些题在第十二届中属于简单题
(我认为不是),在考试中应该是必拿的分,考的是我们模拟某种事件将其转换为程序的能力,此类题型一般是填空题,因为暴力模拟一般会超时。其他题目持续更新中。。。。。。