第一题 最小的十六进制(答案:2730)
问题描述
请找到一个大于 2022 的最小数,这个数转换成十六进制之后,所有的数位(不含前导 0)都为字母(A 到 F)。
请将这个数的十进制形式作为答案提交。
思路分析:2022对应的十六进制是7e6,那么题目的意思是想要所有数位最小而且不含前导0都为字母,仔细一想只有aaa了
public class lianxi {
public static void main(String[] args) {
int i = Integer.parseInt("aaa",16);
System.out.println(i);
}
}
那要是你想不到是aaa的话,那就只能枚举,下面贴出代码:
import java.util.Scanner;
public class lianxi {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 2022;
boolean visit = true;
while (true) {
visit = true;
String s = Integer.toString(n, 16);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ('0' <= c && c <= '9') {
visit = false;
break;
}
}
if (visit == true) {
System.out.println(s);
System.out.println(n);
break;
}
n++;
}
}
}
第二题 Excel的列(答案:BYT)
问题描述
在 Excel 中,列的名称使用英文字母的组合。前 26 列用一个字母,依次为 A 到 Z,接下来 26*26 列使用两个字母的组合,依次为 AA 到 ZZ。
请问第 2022 列的名称是什么?
思路分析:
方法一:可以使用excel工具,将其整理出来
方法二:
public class lianxi {
public static void main(String[] args) {
int cnt = 702;
for (int i = 1; i <= 26; i++) {
for (int j = 1; j <= 26; j++) {
for (int k = 1; k <= 26; k++) {
cnt++;
if (cnt == 2022) {
char a = (char) (i - 1 + 'A');
char b = (char) (j - 1 + 'A');
char c = (char) (k - 1 + 'A');
System.out.print(a + " " + b + " " + c);
break;
}
}
}
}
}
}
第三题 相等日期(答案:70910)
问题描述
对于一个日期,我们可以计算出年份的各个数位上的数字之和,也可以分别计算月和日的各位数字之和。请问从 1900 年 1 月 1 日至 9999 年 12 月 31 日,总共有多少天,年份的数位数字之和等于月的数位数字之和加日的数位数字之和。
例如,2022年11月13日满足要求,因为 2+0+2+2=(1+1)+(1+3) 。
请提交满足条件的日期的总数量。
思路分析:
方法一:使用循环,符合条件的计数,注意过程中闰年的判断
public class lianxi {
static int[] w = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//这里把0索引位置的值设成0,目的是方便后面的输出
static int y = 1900, m = 1, d = 1;
public static void main(String[] args) {
int ans = 0;
while (y != 9999 || m != 12 | d != 31) {
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) w[2] = 29;//判断闰年
else w[2] = 28;
if (check()) ans++;
d++;
if (d > w[m]) {
m++;
d = 1;
}
if (m > 12) {
m = 1;
y++;
}
}
if (check()) ans++;
System.out.println(ans);
}
static boolean check() {
int ans = 0;
int sum = 0;
// 计算年的和
int a = y;
while (a > 0) {
ans += a % 10;
a /= 10;
}
int b = m;
int c = d;
while (b > 0) {
sum += b % 10;
b /= 10;
}
while (c > 0) {
sum += c % 10;
c /= 10;
}
return ans==sum;
}
}
方法二:在Java中,LocalDate
是一个来自 java.time
包中的类,用于表示没有时区的日期。这个类提供了一系列方法来操作日期,例如获取日期的年、月和日,以及增加或减少日期等。不懂的小伙伴可以去搜索一下这个LocalDate
类的详细用法,在今后碰到类似的题目可以都用这个类。
import java.time.LocalDate;
public class lianxi {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(1900, 1, 1);
LocalDate date2 = LocalDate.of(9999, 12, 31);
int count = 0;
while(!date1.equals(date2)){
int year = date1.getYear();
int month = date1.getMonthValue();
int day = date1.getDayOfMonth();
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
while(year != 0){
sum1 += year % 10;
year /= 10;
}
while(month != 0){
sum2 += month % 10;
month /= 10;
}
while(day != 0){
sum3 += day % 10;