【蓝桥杯总结--省一 】

注意事项

数组、字符串处理

  • 读入大量数据:Scanner sc = new Scanner (new BufferedInputStream(System.in))
    数组翻转:int[] intArray = { 1, 2, 3, 4, 5 };
    ArrayUtils.reverse(intArray);
    System.out.println(Arrays.toString(intArray));

  • 进制转换:Integer.valueOf(“str”,x); ///可以为任意进制的字符串str转换成x进制的10进制数
    String str1 = Integer.toHexString(x) ;
    String str2 = Integer.toOctalString(x) ; ///10进制转换成8进制的字符串

  • 读入String,存为char数组

    Scanner sc = new Scanner(System.in);
    String string=sc.next();
    string = String.valueOf(int a)
    char[]a=string.toCharArray();
    Character.isUpperCase(a[1]);
    a[1]>=‘0’;

  • 字符与数字转换
    a.charAt(i)-‘a’->int
    (char)(‘a’+index)

  • 去重
    HashSet set=new HashSet();
    for(int j=i;j<a.length;j++) {
    set.add(a[j]);
    num+=set.size();

      	}
    

BigInteger

浮点数的比较可以使用:BigDecimal.compareTo(f2) == 0

BigDecimal bigDecimal = new BigDecimal(12.52);
		BigInteger []aBigIntegers=new BigInteger[2030];
		aBigIntegers[0]=BigInteger.ZERO;
		aBigIntegers[1]=BigInteger.ONE;
		aBigIntegers[2]=BigInteger.ONE;
		for(int i=3;i<2030;i++) {
			aBigIntegers[i]=aBigIntegers[i-1].add(aBigIntegers[2]);
		}

日期问题

计算星期几:

      Calendar anCalendar=Calendar.getInstance();
    	anCalendar.set(2022, 3, 7);#月份从0开始
    	System.out.println(anCalendar.get(Calendar.DAY_OF_WEEK) - 1);

日期格式化:

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Date dat1=format.parse("1921-7-23");
		int b=(int)dat1.getTime();

有年/月/日的,有采用月/日/年的,还有采用日/月/年的000

Scanner cin = new Scanner(System.in);
		String a[] = cin.next().split("/");
		String t[] = new String[3];
		t[0] = ((Integer.valueOf(a[0]) >= 60) ? "19" + a[0] : "20" + a[0]) + "-" + a[1] + "-" + a[2];
		t[1] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[0] + "-" + a[1];
		t[2] = ((Integer.valueOf(a[2]) >= 60) ? "19" + a[2] : "20" + a[2]) + "-" + a[1] + "-" + a[0];
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		sf.setLenient(false);
		Set<String> s = new TreeSet<String>();
		for (String T : t) {
			try {
				sf.parse(T);
			} catch (ParseException e) {
				continue;
			}
			s.add(T);
		}
		for(String T:s)
		System.out.println(T);
	}

DFS

import java.io.BufferedInputStream;
import java.util.Scanner;

public class _04试剂问题 {

	static int sum=0 ,cnt=Integer.MAX_VALUE;
	static int []arr=new int[15];
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner scanner=new Scanner(new BufferedInputStream(System.in));
		for(int i=0;i<15;i++) {
			arr[i]=scanner.nextInt();
			sum+=arr[i];
		}
		dfs(0,0);
		System.out.println(cnt);

	}
	private static void dfs(int d,int v) {
		// TODO Auto-generated method stub
		if(d==15)
			cnt=Math.min(cnt, Math.abs(sum-v-v));
		else {
			dfs(d+1, v+arr[d]);
			dfs(d+1,v);
		}
		
	}

}

2021年真题

结果填空

1.计算ASC码(5’):

‘L’+0

2.卡片表示数字(5‘):

Arrays.fill(v,n);

3.直线(15’):

Set去重StringBuffer.append( )/.toString();
Set< String> set=new HashSet< String>()

4.货物堆放(10‘):

  • long-10^18 12byte=96bit
  • int 10^10
  • static long[] val = new long[101000];

**5.路径(15’)(最短路径问题)

  • Floyd算法
    for(int i,j,k;<2025;++)
    if (gragh[i][k] + gragh[k][j] < gragh[i][j])
    gragh[i][j] = gragh[i][k] + gragh[k][j];

  • 求最小公倍数:i/gcd(i,j)*j

  • 求最小公因数:if(j==0)
    return i;
    return gcd(j, i%j);

代码编程

6.时间显示(15‘)

  • 格式化输出: System.out.format(“%02d”, a);
    System.out.format(“%o\n”,i);//“o"表示格式化输出八进制整数
    System.out.format(”%x\n",i);//"x"表示格式化输出十六进制整数

  • BigInteger
    浮点数的比较可以使用:BigDecimal.compareTo(f2) == 0

BigDecimal bigDecimal = new BigDecimal(12.52);
		System.out.println(bigDecimal);
		BigInteger []aBigIntegers=new BigInteger[2030];
		aBigIntegers[0]=BigInteger.ZERO;
		aBigIntegers[1]=BigInteger.ONE;
		aBigIntegers[2]=BigInteger.ONE;
		for(int i=3;i<2030;i++) {
			aBigIntegers[i]=aBigIntegers[i-1].add(aBigIntegers[2]);
		}
		BigInteger mBigInteger=aBigIntegers[2020];
		BigInteger nBigInteger=aBigIntegers[520];
		System.out.println(f(mBigInteger,nBigInteger));

**7.最少砝码(15’)

*8.杨辉三角(20’)

  • 利用链表构造
    List list=new ArrayList()
    list.addAll(list2)/add(1)
  • 对于list进行遍历:for(Long l: list)
    list.indexOf(2021);

*9.双向排序(25’)==

  • 对于数组进行逆序排序
    Comparator comparator = new Comparator() {
    public int compare(Integer a, Integer b) {
    return b - a;
    }
    };
    Arrays.sort(r, 1, q + 1, comparator);

**10.括号排序(25’)

括号匹配问题
Map.get(‘(’)
Map.containsValue/containsKey
Map.put(‘(’),‘(’)
Stack.empty()/pop()/peek()

public class isMatch {
		public static void main(String[] args) {
		String string ="([a+b]-(rr{}))";
		boolean res =  match(string);
		System.out.println(res);
	}
	public static boolean match(String str) {
		Map<Character,Character> map = new HashMap<>();
		map.put(')', '(');
		map.put(']', '[');
		map.put('}', '{');
		Stack<Character> stack =new Stack<>();
		for(int i=0;i<str.length();i++) {
			Character c =str.charAt(i);
			if(map.containsValue(c)) {//左括号入栈
				stack.push(c);
			}
			else if(map.containsKey(c)) {//右括号出栈匹配
				if(stack.empty()) {
					return false;
				}
				if(stack.peek()==map.get(c)) {
					stack.pop();
				}else {
					return false;
				}
			}
		}
		return stack.empty()?true:false;
	}
}

2021年第二场

质因子与完全平方数

  • 当前数的所有质因子的指数为偶数,不是偶数就再乘个该质因子

DFS(递归回溯)

import java.util.Scanner;

public class Main {
    static int MOD = 1000000007;
    static int ans = 0;
    static int n, m, k;
    // 方向数组
    static int[] xx = new int[] {1, 1, -1, -1, 2, 2, -2, -2};
    static int[] yy = new int[] {2, -2, 2, -2, 1, -1, 1, -1};
    static int[][] cnt;
    // 是否有马
//    static boolean[][] vis; 不能用vis数组来标记不能放马的位置,因为棋盘上某一点可能有多个马共同进行限制,需要对限制数计数
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        m = scan.nextInt();
        k = scan.nextInt();
        cnt = new int[n][m];
        // 从左上角第一个格子开始放,初始已放马的个数 = 0
        dfs(0, 0, 0);
        System.out.println(ans);
    }
    static void dfs (int x, int y, int horse) {
        if (horse == k) {
            ans = (ans + 1) % MOD;
            return;
        }
        // 切换到下一行第一个元素
        if (y >= m) {
            y = 0;
            x++;
            if (x >= n) return;
        }
        // 当前(x,y)位置不放马
        dfs(x, y + 1, horse);

        // 当前(x,y)位置放马
        // 先判断能否放马
        if (cnt[x][y] == 0) {
            cnt[x][y]++;
            // 遍历当前位置的马能够跳到的棋盘位置,标记为true
            for (int i = 0; i < 8; i++) {
                int tmpx = x + xx[i];
                int tmpy = y + yy[i];
                if (tmpx < 0 || tmpy < 0 || tmpx >= n || tmpy >= m) {
                    continue;
                }
                cnt[tmpx][tmpy]++;
            }
            // 放了马之后继续遍历
            dfs(x, y + 1, horse + 1);
            // 别忘了回溯
            // 回溯:一切在之前change过的变量,全都要恢复
            cnt[x][y]--;
            for (int i = 0; i < 8; i++) {
                int tmpx = x + xx[i];
                int tmpy = y + yy[i];
                if (tmpx < 0 || tmpy < 0 || tmpx >= n || tmpy >= m) {
                    continue;
                }
                cnt[tmpx][tmpy]--;
            }
        }
    }
}
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值