题目1:年号字串
package 真题2;
public class 年号字符 {
public static void main(String[] args) {
//首先用一个变量val存储我们的年份
int val = 2019;
//创建一个含有26个字母的数组,记住数组下标0-25
char[] arr = new char[]{'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','N','W','X','Y'};
//保存我们获取的字母
String s = "";
//循环
while(val>0){
//val取余26就是这个字母按照26字母顺序所在位置,减1是其在数组中的位置,数组下标从0开始
s+=arr[(val%26)];
//2019首先取余,是17的位置,减1是16也就是Q
//除26,2019/26 = 77,77%26 = 25,减1是Y的位置
//77/26 = 2.9几,由于int,所以最后是2,减1由于数组下标1也就是B
val/=26;
}
//S最后是QYB
//创建这个是为反转字符串s
StringBuilder sb = new StringBuilder(s);
//调用reverse将字符串反转后用toString转为字符串
System.out.println(sb.reverse().toString());
}
}
题目2:货物摆放
package fytyfytyu;
import java.util.ArrayList;
import java.util.List;
public class 货物摆放 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long num = 2021041820210418l;
List <Long> list = new ArrayList<>();//创建一个集合用来存储num的因数
int count =0; //统计个数
for(long i = 1 ; i*i<=num;i++) {
if(num%i==0 ) {//如果能对i取余那么i肯定是num的因子,把i添加到list里面
list.add(i);
long n = num/i ;//既然能num%i=0那么num/i肯定能得到另一个因子
if(n!=i)list.add(n);
}
}
//循环找因子相乘==num的数 进行统计
for(long i :list)
for(long j :list) {
long ff=num/i/j;//可能有人会在这里有困惑为什么要这样多此一举但是仔细品一下这样就少了一个循环
if(ff*j*i==num)count++;
}
System.out.println(count);
}
}
题目3:数字三角形
代码:
package 真题2;
import java.util.Arrays;
import java.util.Scanner;
public class 三角形有条件的 {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a [] [] = new int [n+1][n+1];//下标从1开始,所以要加1
int dp [] [] = new int [n+1][n+1];
for(int i = 1; i <=n;i++){ //开始输入元素
for(int j = 1 ; j<=i ;j++){
a[i][j]=sc.nextInt();
}
}
for(int i = 1; i <=n;i++){ //开始判断,它总是从本身的上一行的前一列,和上一行的本列去判断谁大,然后加上原数组的对应的值
for(int j = 1;j<=i;j++){
dp[i][j]=Math.max(dp[i-1][j-1],dp[i-1][j])+a[i][j];
}
}
for(int x [] :dp) System.out.println(Arrays.toString(x));
if((n%2)==0){ //因为左下 到右下的次数相同那么 最后肯定是中间的位置,若是偶数是中间两个,奇数就是中间的一个
System.out.println(Math.max(dp[n][n/2],dp[n][n/2+1]));
}else System.out.println(dp[n][n/2+1]);
}
}
题目4:排序
package fytyfytyu;
import java.util.Arrays;
import java.util.Scanner;
public class 排序 {
static int count = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();// 要排序的字符串
getCompCount(str);
}
}
static void getCompCount(String ss) {
char[] a = ss.toCharArray();// 得到字符数组
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
count++;
char tem = a[j];
a[j] = a[j + 1];
a[j + 1] = tem;
}
}
}
System.out.println(Arrays.toString(a) + ",次数为:" + count);
count = 0;
}
}
//abcdefghijklmno
//onmlkjihgfedcba
//[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o],次数为:105
//jonmlkihgfedcba
//[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o],次数为:100
题目5:数的分解
public class Main {
//数的分解
/*
* 用2个for循环遍历数字,第3个数字为2019减前两个,要保证一个数比一个大,才能保证不会重。
* 用一个方法判断每个数都不等于2和4;
*/
public static void main(String[] args) {
int count= 0;
int k = 0;
//要保证前面比后面一个数小,0不能包括
for (int i = 1; i < 2019/3; i++) { //1 2 2016
for (int j = i+1; j < 2019/2; j++) {
k = 2019 - i - j;
//要保证不重复,不交换顺序,那么后面的要大于前面的
if (i<j && j<k ){
//判断是否出现2和4
if (notand24(i) && notand24(j) && notand24(k)){
count++;
}
}
}
}
System.out.println(count);
}
public static boolean notand24(int n){
String str = n+"";
if(str.contains("2")||str.contains("4")) {
return false;
}
return true;
}
}