蓝桥之旅----十三届蓝桥杯javac组省赛

声明:参考代码均表明出处,仅供参考,我的代码仅做比赛答题还原,无参考意义,欢迎大佬们指出错误 

目录

 

比赛试题

A:排列字母

正解代码

我的代码

B:特殊时间

正解代码

我的代码

C:纸张尺寸

正解代码

我的代码

D:求和

正解代码

我的代码

E:矩形拼接

正解代码

F:选数异或

正解代码(40%)

我的代码

I:GCD

正解代码(参考1)

正解代码(参考2)-更相减损术

我的代码

G:青蛙过河

正解代码(参考1)

正解代码(参考2)-二分 + 贪心

K:因数平方和

正解代码(参考1)

正解代码(参考2)-数论分块

L:最长不下降子序列

正解代码

我的代码

比赛结果


 

 

比赛试题

很奇怪哦 今年只有两道填空题 

第一道签到题 话不多说 上题!

声明:参考代码均表明出处,仅供参考,我的代码仅做比赛答题还原,无参考意义,欢迎大佬们指出错误

A:排列字母

0b76148231c944c69a4a47fb92954f03.png

 

正解代码

import java.util.Arrays;
 
public class 字母排列 {
    public static void main(String[] args) {
        String ss="WHERETHEREISAWILLTHEREISAWAY";
        char []arr=new char[ss.length()];
        for (int i=0;i<ss.length();i++) {
            arr[i]=ss.charAt(i);
        }
        Arrays.sort(arr);
        for (int i=0;i<ss.length();i++)
            System.out.print(arr[i]);
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客 

我的代码

不得不说比赛时没有想到用sort 只想着奋笔疾书pass一下这道题了 不管怎么说 应该也做对了 就是有点费眼睛。

B:特殊时间

21011c1a3f2341829913c07427ca4606.png

 

正解代码

212

我的代码

不会做emm

C:纸张尺寸

4498328525f24a78bd5d36292cae3721.png

 

正解代码

import java.util.Scanner;
 
public class 纸张尺寸 {
    public static void main(String[] args) {
        int [][]arr=new int[10][2];
        Scanner sc=new Scanner(System.in);
        int a=1189,b=841;
        arr[0][0]=1189;
        arr[0][1]=841;
        for (int i=1;i<10;i++){
            arr[i][0]=Math.min(a,b);
            if (a>b) {
                a /= 2;
            }
            else{
                b/=2;
            }
            arr[i][1]=Math.min(a,b);
        }
        String ss=sc.next();
        int t=ss.charAt(1)-'0';
        System.out.println(arr[t][0]);
        System.out.println(arr[t][1]);
    }
}

来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客

我的代码

import java.util.Arrays;
import java.util.Scanner;

public class aa {
	public static void main(String[] args) {
		int x =1189;
		int y =841;
		int arr [][] = new int [10][2]; 
		arr[0][0]=x;
		arr[0][1]=y;
		for (int i = 1; i < 10; i++) {
			if (x>y) {
				x/=2;
				arr[i][0]=Math.max(x, y);
				arr[i][1]=Math.min(x, y);
			}else if(y>x) {
				y/=2;
				arr[i][0]=Math.max(x, y);
				arr[i][1]=Math.min(x, y);
			}
		}
		Scanner sc =new Scanner(System.in);
		String str1=sc.next();
		switch (str1) {
		case "A0":System.out.println(arr[0][0]);
		System.out.println(arr[0][1]);
			break;
		case "A1":System.out.println(arr[1][0]);
		System.out.println(arr[1][1]);
			break;
		case "A2":System.out.println(arr[2][0]);
		System.out.println(arr[2][1]);
			break;
		case "A3":System.out.println(arr[3][0]);
		System.out.println(arr[3][1]);
			break;
		case "A4":System.out.println(arr[4][0]);
		System.out.println(arr[4][1]);
			break;
		case "A5":System.out.println(arr[5][0]);
		System.out.println(arr[5][1]);
			break;
		case "A6":System.out.println(arr[6][0]);
		System.out.println(arr[6][1]);
			break;
		case "A7":System.out.println(arr[7][0]);
		System.out.println(arr[7][1]);
			break;
		case "A8":System.out.println(arr[8][0]);
		System.out.println(arr[8][1]);
			break;
		case "A9":System.out.println(arr[9][0]);
		System.out.println(arr[9][1]);
			break;
		}
	}
}

D:求和

318b2100e84f4f158659a741b43fbb70.png

 

正解代码

import java.util.Scanner;
 
public class 求和 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        long s=0;
        int n=sc.nextInt();
        int []arr=new int[n];
        for (int i=0;i<n;i++){
            arr[i]=sc.nextInt();
            s+=arr[i];
        }
        long count=0;
        for (int i=0;i<n;i++){
            count+=arr[i]*(s-arr[i]);
            s-=arr[i];
        }
        System.out.println(count);
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客 

我的代码

import java.util.Scanner;

public class qiuhe {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long arr[]= new long [n];
		for (int i = 0; i < arr.length; i++) {
			arr[i]=sc.nextLong();
		}
		sc.close();
		long ans[]= new long [(n*n)];
		long max = 0;
		int k=0;
		for (int i = 0; i < n-1; i++) {
			for (int j = i; j < n-1; j++) {
				ans[k]=arr[i]*arr[j+1];
				k++;
			}
		}
		for (int i = 0; i < (n*n)-1; i++) {
			max += ans[i];
		}
		System.out.println(max);
	}
}

E:矩形拼接

dbb9f6aa0548454ca7ffc66e8bc213f2.png

 

正解代码

import java.util.Scanner;
 
public class 矩形拼接 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        int xa,xb,xc,ya,yb,yc;
        for (int i=0;i<T;i++){
            xa=sc.nextInt();
            ya=sc.nextInt();
            xb=sc.nextInt();
            yb=sc.nextInt();
            xc=sc.nextInt();
            yc=sc.nextInt();
            int t=0,s=0;
            if (pd(xa, xb, xc, ya, yb, yc)&&s==0) s=1;
            if (pd(ya, xb, xc, xa, yb, yc)&&s==0) s=1;
            if (xb==xc&&s==0){
                t=1;
                if (yb+yc==xa||yb+yc==ya){
                    System.out.println(4);
                    s=1;
                }
            }
            if (xb==yc&&s==0){
                t=1;
                if (yb+xc==xa||yb+xc==ya){
                    System.out.println(4);
                    s=1;
                }
            }
            if (yb==xc&&s==0){
                t=1;
                if (xb+yc==xa||xb+yc==ya){
                    System.out.println(4);
                    s=1;
                }
            }
            if (yb==yc&&s==0){
                t=1;
                if (xb+xc==xa||xb+xc==ya){
                    System.out.println(4);
                    s=1;
                }
            }
            if(xa+xb==xc||xa+xb==yc||xa+yb==xc||xa+yb==yc||xa+xc==xb||xa+xc==yb||xa+yc==xb||xa+yc==yb)
                t=1;
            if(ya+xb==xc||ya+xb==yc||ya+yb==xc||ya+yb==yc||ya+xc==xb||ya+xc==yb||ya+yc==xb||ya+yc==yb)
                t=1;
            if(xb+xc==xa||xb+xc==ya||xb+yc==xa||xb+yc==ya||yb+xc==xa||yb+xc==ya||yb+yc==xa||yb+yc==ya)
                t=1;
            if (s==0) {
                if (t == 1) {
                    System.out.println(6);
                } else {
                    System.out.println(8);
                }
            }
        }
    }
 
    private static boolean pd(int xa, int xb, int xc, int ya, int yb, int yc) {
        int t;
        if (xa==xb){
            t =1;
            if (ya+yb==xc||ya+yb==yc||xa==xc||xa==yc){
                System.out.println(4);
                return true;
            }
        }
        if (xa==yb){
            t =1;
            if (ya+xb==xc||ya+xb==yc||xa==xc||xa==yc){
                System.out.println(4);
                return true;
            }
        }
        if (xa==xc){
            t =1;
            if (ya + yc == xb || ya + yc == yb){
                System.out.println(4);
                return true;
            }
        }
        if (xa==yc){
            t =1;
            if (ya+xc==xb||ya+xc==yb){
                System.out.println(4);
                return true;
            }
        }
        return false;
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客

F:选数异或

d7dc65c3aa2c441985966b48e11e3d02.png

 

正解代码(40%)

import java.util.Scanner;
 
public class 选数异或 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int x=sc.nextInt();
        int []arr=new int[n+1];
        for (int i=1;i<=n;i++){
            arr[i]=sc.nextInt();
        }
        for (int i=0;i<m;i++){
            int a=sc.nextInt(),b=sc.nextInt(),s=0;
            for (int j=a;j<b;j++){
                for (int k=j+1;k<=b;k++){
                    if ((arr[j]^arr[k])==x) {
                        s=1;
                        System.out.println("yes");
                        break;
                    }
                }
                if (s==1)
                    break;
            }
            if (s==0)
                System.out.println("no");
        }
    }
}

  来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客

我的代码


import java.util.Scanner;

public class xuanshu {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();//长度
		int m=sc.nextInt();//m次查询
		int x=sc.nextInt();//ans
		int arr [] = new int [n];//数列
		int xq[][]=new int [m][2];//查询数
		//输入
		for (int i = 0; i < n; i++) {
			arr[i]=sc.nextInt();
		}
		for (int i = 0; i < m; i++) {
			xq[i][0]=sc.nextInt();
			xq[i][1]=sc.nextInt();
		}
		
		//取区间数
		
		for (int i = 0; i < m; i++) {//输出m行
			boolean b =false;
			//每一行输出yes/ no
			for (int j = xq[i][0]-1; j < xq[i][1]-1; j++) {//arr下标
				for (int j2 = j+1; j2 < xq[i][1]; j2++) {
					if (check(arr[j],arr[j2],x)) {
						b=true;
						break;
					}
				}
			}
			if (b) {
				System.out.println("yes");
			}else {
				System.out.println("no");
			}
		}	
	}
	
	//判断异或是否等于x
	private static boolean check(int a,int b,int x) {
		// TODO Auto-generated method stub
		int ans1 = a^b;
		if (ans1==x) {
			return true;
		}
		return false;
	}
}

I:GCD

7a8f5f4cd4af406e89134dee2b73b19f.png

 

正解代码(参考1)

import java.util.Scanner;
 
public class GCD {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        long a=sc.nextLong();
        long b=sc.nextLong();
        long max=Math.abs(a-b);
        if (a%max==0){
            System.out.print(0);
        }else {
            System.out.print(max-(a%max));
        }
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客 

正解代码(参考2)-更相减损术

import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) { new Main().run(); }

    void run() {
        InputReader in = new InputReader(System.in);
        long a = in.nextLong(), b = in.nextLong();
        long c = abs(a - b);
        if (c == 0 || c == 1) System.out.print("1");
        else System.out.print(min((-a % c + c) % c, (-b % c + c) % c));
    }

    long min(long arg1, long arg2) { return arg1 < arg2 ? arg1 : arg2; }

    long abs(long arg) { return arg > 0 ? arg : -arg; }

    class InputReader {

        BufferedReader reader;
        StringTokenizer token;

        InputReader(InputStream in) { this.reader = new BufferedReader(new InputStreamReader(in)); }

        String next() {
            if (token == null || !token.hasMoreTokens()) {
                try {
                    token = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return token.nextToken();
        }

        long nextLong() { return Long.parseLong(next()); }
    }
}

我的代码


import java.util.Scanner;

public class gcd {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		sc.close();
		for (long k = 1; k < Long.MAX_VALUE; k++) {
			if (gcd(a+k,b+k)) {
				System.out.println(k);
				return;
			}
		}
	}
	
	public static boolean gcd(long a,long b) {
		// TODO Auto-generated constructor stub
		for (int i = 2; i < (a*b)+1; i++) {
			if (a%i==0&&b%i==0) {
				return true;
			}
		}
		return false;
	}
}

G:青蛙过河

bc5eb1835c1b42269b144bc26c2d0af2.png

 

正解代码(参考1)

import java.util.Scanner;
 
public class 青蛙过河 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int x=sc.nextInt();
        int []arr=new int[n];
        int []brr=new int[n];
        int []crr=new int[n];
        for (int i=1;i<n;i++){
            arr[i]=sc.nextInt();
            brr[i]=brr[i-1]+arr[i];
        }
        for (int i=n-1;i>=1;i--){
         crr[n-i]=crr[n-i-1]+arr[i];
        }
        for (int i=1;i<n;i++){
            if (brr[i]>=2*x&&crr[i]>=2*x){
                System.out.print(i);
                return;
            }
        }
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客 

正解代码(参考2)-二分 + 贪心

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.io.IOException;

public class Main {

    public static void main(String[] args) { new Main().run(); }
    
    void run() {
    	int n = nextInt(), x = nextInt() << 1;
    	int[] H = new int[n + 1], S = new int[n + 1];
    	long[] V = new long[n + 1];
    	for (int i = 1; i < n; ++i) H[i] = nextInt();
    	int mid, ans = 1, right = n;
    	V[0] = Long.MAX_VALUE;
    	while (ans < right) {
    		mid = ans + right >> 1;
    		int l = 0, r = 0;
    		for (int i = 1; i <= n; ++i) {
    			while (l <= r && S[l] < i - mid) ++l;
    			if (H[i] > 0) {
    				int Hk = 0;
    				while (l <= r && Hk < H[i])
    					if (V[l] <= H[i] - Hk) Hk += V[l++];
    					else {V[l] -= H[i] - Hk; Hk = H[i];}
    				if (Hk > 0) { S[++r] = i; V[r] = Hk; }
    			}
    		}
    		long cnt = 0;
    		while (l <= r)cnt += V[l++];
    		if (cnt >= x) right = mid; else ans = mid + 1;
    	}
    	System.out.println(ans);
    }
    
    StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    
    int nextInt() {
    	try {
			in.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (int)in.nval;
    }
}

  来自第十三届蓝桥杯大赛软件赛省赛(Java 大学C组)_肖有量的博客-CSDN博客

 

K:因数平方和

b41b831351a243f88e0f600300c57be0.png

 

正解代码(参考1)

import java.math.BigInteger;
import java.util.Scanner;
 
public class 因数平方和 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        long s=0;
        long k;
        int t= n/2;
        for (int i=1;i<=t;i++){
            k=n/i;
            s+=(k*((long) i *i))%1000000007;
            s%=1000000007;
        }
        BigInteger x= BigInteger.valueOf(n),y= BigInteger.valueOf(t);
        BigInteger a= BigInteger.valueOf(1),b= BigInteger.valueOf(2),c= BigInteger.valueOf(6);
        x= x.multiply(x.add(a)).multiply(x.multiply(b).add(a)).divide(c);
        y= y.multiply(y.add(a)).multiply(y.multiply(b).add(a)).divide(c);
        x=(x.add(BigInteger.valueOf(s)).subtract(y)).mod(BigInteger.valueOf(1000000007));
        System.out.print(x);
    }
}

 来自 2022蓝桥杯javaC省赛_小怂很怂的博客-CSDN博客 

正解代码(参考2)-数论分块

import java.io.StreamTokenizer;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) { new Main().run(); }

    int p = 1000000007, inv6 = 166666668;

    void run() {
        int n = nextInt();
        long tmp, sum = 0, ans = 0;
        for (int l = 1, r; l <= n; l = r + 1) {
            r = n / (n / l);
            tmp = sum;
            sum = r * (r + 1L) % p * (2 * r + 1) % p * inv6 % p;
            ans = (ans + (n / l) * (sum - tmp) + p) % p;
        }
        System.out.println(ans);
    }

    StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    int nextInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int)in.nval;
    }
}

来自第十三届蓝桥杯大赛软件赛省赛(Java 大学C组)_肖有量的博客-CSDN博客

L:最长不下降子序列

1f255af0d8b541828c1e245c322407fc.png

 

正解代码

 

我的代码

import java.util.Scanner;

public class zuichangbuxiajiangzixulie {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n =sc.nextInt();
		int k =sc.nextInt();
		int cnt=0;
		int arr[]= new int [n];
		for (int i = 0; i < n; i++) {
			arr[i]=sc.nextInt();
		}
		//修改
		//把连续k个数改成小于等于后面一个数且大于等于前面数的值
		//统计
		
		for (int i = 0; i < arr.length-1-k; i++) {
			if (arr[i+1]<arr[i]) {
				for (int j = i+1; j < i+1+k; j++) {
					arr[j]=arr[i+k+1];
				}
			}
		}
		for (int i = 0; i < n-1; i++) {
			if (arr[i+1]>=arr[i]) {
				cnt++;
			}
		}
		System.out.println(cnt+1);
		
	}
}

比赛结果

荣誉上图

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBANDQ0NGw=,size_20,color_FFFFFF,t_70,g_se,x_16

 

 很荣幸,谢谢大家。

一个多月的集训让我学习到了很多认识了很多朋友,养成了很好的学习习惯。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

4444l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值