蓝桥杯12省赛真题_java.C组试题学习

考点:

ASCⅡ码考察: 

ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符。标准ASCII 码也叫基础ASCII码,使用7 位二进制数(剩下的1位二进制为0)来表示所有的大写和小写字母,数字0 到9、标点符号,以及在美式英语中使用的特殊控制字符 [1]  

0~31及127(共33个)是控制字符或通信专用字符(其余为可显示字符)

32~126(共95个)是字符(32是空格),其中48~57为0到9十个阿拉伯数字。

65~90为26个大写英文字母,97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。

考点:

 计算机数值转换考察:

先将MB转换为字节Byte,也就是Byte(B),1MB = 1024KB, 1KB = 1024B,1B = 8bit(位)所以256 MB = 256 * 1024 * 1024B,32位二进制整数的存储空间也即32bit = 32 / 8 = 4B,所以可以存在 256 * 1024 * 1024 / 4,使用程序计算出结果为:67108864

if __name__ == '__main__':
    print(256 * 1024 * 1024 // 4)

public class Main {
	public static void main(String[] args) {
	    //定义变量,创建卡片
        int[] num = new int[10];
		int temp;
		int t;
		int i;
		boolean flag = false;
		for(i = 0; i < 10; i++) {
			num[i] = 2021;
		}
        //数字构建
		for(i = 1;; i++) {
			temp = i; 
            //数字判断,从个位开始判断
			while(temp != 0) {
				t = temp % 10;
                //判断数字卡片是否用完
				if(num[t] <= 0) {
					flag = true;
					break;//中断while循环
				}
				num[t]--;
				temp /= 10;
			}
			if(flag) {
				break;//中断for循环
			}
		}
		System.out.println(i-1);
	}
}
//	#C 卡片
	public static int arr[]=new int[10];
	public static void main(String[] args) {
//		0-9的数字各2021张
		for(int i=0;i<10;i++)arr[i]=2021;
//		循环看能拼到多少数字
		for(int i=1;i<5000;i++){
//			判断能否组成,组成则数字会减去一个
			if(!del(i)){
				System.out.println(i-1);
				break;
			}
		}
	}
//	进行判断是否能够组成数字
	public static boolean del(int x){
		while(x!=0){
			arr[x%10]--;
			if(arr[x%10]<0)return false;
			x/=10;
		}
		return true;
	}

考点:

判断语句,数的表达形式转换,中断语句                                                                         算法思想

//	#D 相乘
	public static void main(String[] args) {
		// a控制判断是否存在该数
		int a=1;
//		循环1-1000000007的数
		for(int i=1;i<=1000000007;i++) {
//			将每个数都与2021相乘
			int t=i*2021;
//			然后判断与1000000007相除之后的余数是否为999999999
			if((long)t%1000000007==999999999) {
				a=a+1;
//				若是则输出该数
				System.out.println(i);
				break;
			}
		}
//		若a没有变化则说明数不存在,若有变化则证明数存在
		if(a!=2) {
			 System.out.println(0);
		}
	}

考点:

for循环遍历,考虑到是否存在进行差别输出

import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.List;
public class questionE {
// #E 路径
	int N = 2021;
	 void main() {
		// TODO Auto-generated method stub
		List<Edge>[] graph = new List[N + 1];
        long[] visited = new long[N + 1];
        for (int i = 1; i <= N; i++)
            graph[i] = new ArrayList();
        for (int v = 1; v <  N; v++)
            for (int w = v + 1; w <= min(v + 21, N);  w++) {
                graph[v].add(new Edge(w, lcm(v, w)));
                graph[w].add(new Edge(v, lcm(v, w)));
            }
        Queue<Vertex> queue = new PriorityQueue();
        Arrays.fill(visited, Long.MAX_VALUE);
        queue.offer(new Vertex(1, 0));
        Vertex V = null;
        while (queue.size() > 0) {
            V = queue.poll();
            if (V.v == N) break;
            if (V.weight >= visited[V.v]) continue;
            visited[V.v] = V.weight;
            for (Edge edge : graph[V.v])
                queue.offer(new Vertex(edge.w, edge.weight + V.weight));
        }
        System.out.println(V.weight);
	}
	 int min(int a, int b) { return a < b ? a : b; }
 
	    int lcm(int a, int b) { return a * b / gcd(a, b); }
 
	    int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
 
	    class Edge {
 
	        int w, weight;
 
	        Edge(int w, int weight) {
	            this.weight = weight;
	            this.w = w;
	        }
	    }
 
	    class Vertex implements Comparable<Vertex> {
 
	        int v;
	        long weight;
 
	        Vertex(int v, long weight) {
	            this.weight = weight;
	            this.v = v;
	        }
 
	        @Override
	        public int compareTo(Vertex V) { return Long.compare(this.weight, V.weight); }
	    }
 
}
public class Main{
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int a =21;
		for (int i = 21; i+21<2021 ; i+=21) {
			a=a+i*(i+21)/21;
		}
		a=a+2016*2021;
				System.out.println(a);
	}
}

(55条消息) 蓝桥杯第十二届java/C组试题 E: 路径_数据结构做不对的博客-CSDN博客(这个方法针对填空更加合适,暂未理解)

 图的变种,最简单的路径比较进行暴力记录比对

import java.util.*;
 
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
		long time = sc.nextLong();
		long hours = time / 1000 / 3600 % 24;
		long min = time / 1000 / 60 % 60;
		long miao = time / 1000 % 60;
		if (hours < 10) {
			System.out.print("0" + hours + ":");
		} else {
			System.out.print(hours + ":");
		}
		if (min < 10) {
			System.out.print("0" + min + ":");
		} else {
			System.out.print(min + ":");
		}
		if (miao < 10) {
			System.out.print("0" + miao);
		} else {
			System.out.print(miao);
		}
    }
}

对于时间表示进行学习

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        scan.close();
        //从砝码本身出发(从解出发去找解的角度)
        //一个砝码-只有N=1的情况
        //二个砝码-只有N=2-4的情况   1 3-1 3 3+1
        //三个砝码-只有N=5-13的情况
        //   [ (3^(n-1)-1)/2 + 1 , (3^n-1)/ 2 ] 规律区间
        int n = 1;
        if (N != 1) {
          while (true) {
            if ((Math.pow(3,n-1)-1)/2 <= N && N <= (Math.pow(3,n)-1)/2) {
              break;
            }
            n++;
          }
        }
        System.out.println(n);
    }
}

 数学规律

 

118. 杨辉三角 - 力扣(Leetcode)

输出杨辉三角,修改只需进行保存for循环搜索返回行值即可

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> row = new ArrayList<Integer>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    row.add(1);
                } else {
                    row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
                }
            }
            ret.add(row);
        }
        return ret;
    }
}
package blueBridgeCB2021First;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * 杨辉三角形
 * AC
 * https://www.acwing.com/problem/content/3421/
 *不能过样例:1,因为N=1,本来想的是开始二分最小是C(k,2k),但是r=N=1,没有经过二分,C(1,1)却=1,因此把3输出了。需要添加条件N>=2*k,
 */
public class T3418 {
	static String[] s;
	static int N;
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		s = in.readLine().split(" ");
		N = Integer.valueOf(s[0]);
 
		for (int i = 16; i >=0; i--) {
			if(check(i))
				break;
		}
	}
	static long C(int a,int b) {
		long res = 1;
		for (int i = 1,j=b; i<=a; i++,j--) {
			res=res*j/i;
			if(res>N)
				return res;
		}
		return res;
	}
	static boolean check(int k) {
		long l = 2*k,r=N;
		while(l<r) {
			long mid = (l+r)>>1;
			if(C(k,(int) mid)>=N)
				r = mid;
			else
				l = mid+1;
		}
		if(C(k,(int) r)!=N||N<2*k)
			return false;
		else {
			System.out.println(r*(r+1)/2+k+1);
			return true;
		}
	}
}
 

 

 

 

(55条消息) 蓝桥杯2021年真题演练——7、 左hai子右兄弟(JavaA组)_韩跳跳、的博客-CSDN博客_左孩子右兄弟

import java.util.*;
public class Main {
	public static  Edge[] edge;//存储边的信息
	public static int[] head;//存储各节点在edge中的起点
	public static int[] count;//计数各节点的孩子节点的个数
	public static int tot;//addEdge用到的一个变量
	public static int[] dp;
	public static void main(String[] args) {
		//采用邻接表存储树(数组统一以1为起点)
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();//节点个数(边个数为n-1)
		edge=new Edge[n+1];
		for(int i=1;i<=n;i++) edge[i]=new Edge();
		head=new int[n+1];
		count=new int[n+1];
		dp=new int[n+1];
		//录入边的信息
		for(int son=2;son<=n;son++) {
			int farther=scan.nextInt();
			addEdge(farther,son);
			count[farther]++;
		}
		getMax(1);
		System.out.println(dp[1]);
		scan.close();
	}
	public static void getMax(int u) {
		int max=0;
		for(int i=head[u];i!=0;i=edge[i].next) {
			int v=edge[i].to;
			getMax(v);
			max=Math.max(max, dp[v]);
		}
		dp[u]=count[u]+max;
		return ;
	}
	public static void addEdge(int farther,int son) {
		//头插法
		tot++;
		edge[tot].to=son;//建立边:farther->son
		edge[tot].next=head[farther];//新添加的边指向原来的头
		head[farther]=tot;//新边作为新的头
	}
}
class Edge{
	int next;//存储下一个孩子节点的下标
	int to;//存储孩子节点
}



import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        Map<Integer, List<Integer>> map = new HashMap<>(); // k:父节点 v:子节点
        for (int i = 2; i <= N; i++) {
            int x = scan.nextInt();
            map.putIfAbsent(x, new ArrayList<>());
            map.get(x).add(i);
        }
        int ans = dfs(1, map);
        System.out.println(ans);
    }
 
    private static int dfs(int i, Map<Integer, List<Integer>> map) {
        if (!map.containsKey(i)) return 0; // 递归终止条件,当该节点没有子节点,高度为0
        List<Integer> children = map.get(i);
        int size = children.size();
        int max = 0; // 子节点的最大高度
        for (Integer child : children) {
            max = Math.max(dfs(child, map), max);
        }
        return size + max;
    }
}

考点:二叉树的数据结构,如何存储树?

 

(55条消息) 蓝桥杯2021省赛双向排序—JAVA_Crazy-GONG的博客-CSDN博客_java 双向排序

import java.util.Arrays;
import java.util.Scanner;
 
public class _30_双向排序{
public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();  //表示序列的长度
        int m=sc.nextInt();	 //表示操作次数
        int a[]=new int[n];   //创建一个数组,用于存储数据
 
        //给数组赋值,实现从a1————an
        for(int i=0;i<n;i++){
            a[i]=i+1;
        }
 
        //m次操作
        for(int i=0;i<m;i++){
            int pi=sc.nextInt();  //操作类型
            int qi=sc.nextInt();  //参数
            if(pi==0){
                Arrays.sort(a,0,qi);   //知识点如下
 
                for(int x=0,y=qi-1;x<y;x++,y--){
                    int temp=a[x];
                    a[x]=a[y];
                    a[y]=temp;
                }  //通过for语句,进行数组内数据的大小排序,按降序
 
            }else if(pi==1){
                Arrays.sort(a,qi-1,n);   //知识点如下
            }
        }
        //输出数组
        for(int a1:a){  //a1获取的是数组a中的每一个元素的值,而不是数组的下标
            System.out.print(a1+" ");
        }
    }
}

Array.sort()用法

1.Arrays.sort(int[] a);

这种形式是对一个数组的所有元素进行排序,并且是按从小到大的顺序。

2.Arrays.sort(int[] a, int fromIndex, int toIndex)

这种形式是对数组部分排序,也就是对数组a的下标从fromIndex到toIndex-1的元素排序,注意:下标为toIndex的元素不参与排序
 

以上均为摘选

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值