2022蓝桥杯Java A组B组

Posted 钟钟终

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2022蓝桥杯Java A组B组相关的知识,希望对你有一定的参考价值。

[蓝桥杯2022初赛] 裁纸刀
443
[蓝桥杯2022初赛] 求和

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main 
    static Scanner cin;
    static PrintWriter cout;
    static long a[];
    static long sum[];
    public static void init() throws IOException
        cin=new Scanner(System.in);
        cout=new PrintWriter(System.out);
        a=new long[(int)(7e5+5)];
        sum=new long[(int)(7e5+5)];
    
    public static void solve() throws IOException
        int n=cin.nextInt();
        for(int i=1;i<=n;i++) 
            a[i] = cin.nextInt();
            sum[i] = sum[i - 1] + a[i];
        
        long ans=0;
        for(int i=1;i<n;i++)
            ans+=a[i]*(sum[n]-sum[i]);
        cout.println(ans);
    
    public static void main(String[] args) throws IOException 
        init();
        solve();
        cout.close();
    

[蓝桥杯2022初赛] 青蛙过河

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main 
    static Scanner cin;
    static PrintWriter cout;
    static long a[];
    static long sum[];
    static long n,x;
    public static void init() throws IOException
        cin=new Scanner(System.in);
        cout=new PrintWriter(System.out);
        a=new long[(int)(7e5+5)];
        sum=new long[(int)(7e5+5)];
    
    public static int check(long d)
        long tmp=0;
        for(long i=1;i+d<=n;i++)
            tmp= (int) (sum[(int) (i+d-1)]-sum[(int) (i-1)]);
            if(tmp<x)
                return 0;
        
        return 1;
    
    public static void solve() throws IOException
        n=cin.nextInt();
        x=cin.nextInt();
        x=x*2;
        for(int i=1;i<n;i++)
            a[i]=cin.nextInt();
            sum[i]=sum[i-1]+a[i];
        
        sum[(int) n]=sum[(int) (n-1)];
        long l=0,r=(int)(1e9),mid,ans = 0;
        while(l<=r)
            mid=(l+r)/2;
            if(check(mid)!=0) 
                r=mid-1;
                ans=mid;
            
            else
                l=mid+1;
            
        
        cout.println(ans);
    
    public static void main(String[] args) throws IOException 
        init();
        solve();
        cout.close();
    

[蓝桥杯2022初赛] GCD

import java.io.*;
import java.util.*;
public class Main 
    static Scanner cin;
    static PrintWriter cout;
    public static void init() throws IOException
        cin=new Scanner(System.in);
        cout=new PrintWriter(System.out);
    
    public static void solve()
        long a,b,c;
        a=cin.nextLong();
        b=cin.nextLong();
        c=Math.abs(a-b);
        cout.println(c-a%c);
    
    public static void main(String[] args) throws IOException 
        init();
        int t=cin.nextInt();
        while(t-->0)
            solve();
        
        cout.close();
    

2052: [蓝桥杯2022初赛] 全排列的价值
暴力全排列:10%

public static void dfs(int x)
        if(x==n+1)
            for(int i=2;i<=n;i++)
                int g=0;
                for(int j=1;j<i;j++)
                    if(a[j]<a[i]) g++;
                
                ans+=g;
            
        
        for(int i=1;i<=n;i++)
            if(vis[i]!=0) continue;
            a[x]=i;
            vis[i]=1;
            dfs(x+1);
            vis[i]=0;
        
    

动态规划:

import java.io.*;
import java.util.*;
public class Main 
    static Scanner cin;static PrintWriter cout;
    public static void init() throws IOException
        cin=new Scanner(System.in);cout=new PrintWriter(System.out);
    
    public static void solve()
        long n=cin.nextLong();
        long mod=998244353;
        long ans=0,g=1;
        for(long i=2;i<=n;i++) 
            ans=(ans*i%mod+i*(i-1)/2%mod*g%mod)%mod;
            g=g*i%mod;
        
        cout.println(ans);
    
    public static void main(String[] args) throws IOException 
        init();
        solve();
        cout.close();
    


JAVA B组
[蓝桥杯2022初赛] 星期计算
7
2058: [蓝桥杯2022初赛] 字符统计

import java.io.*;
import java.util.*;
public class Main 
    static Scanner cin;static PrintWriter cout;
    public static void init() throws IOException
        cin=new Scanner(System.in);cout=new PrintWriter(System.out);
    
    public static void solve()
        Map<Character,Integer> mp=new TreeMap<Character,Integer>();
        String str=cin.next();
        char s[]=str.toCharArray();
        int len=str.length();
        for(int i=0;i<len;i++) 
            if(mp.containsKey(s[i])==false)
                mp.put(s[i],1);
            else
                mp.put(s[i],mp.get(s[i])+1);
        
        int tmp=0;
        for(Map.Entry<Character,Integer> entry:mp.entrySet())
            if(tmp<entry.getValue())
                tmp=entry.getValue();
        
        String ans="";
        for(Map.Entry<Character,Integer> entry:mp.entrySet())
            if(tmp==entry.getValue())
                ans+=entry.getKey();
        
        cout.println(ans);
    
    public static void main(String[] args) throws IOException 
        init();
        solve();
        cout.close();
    

2059: [蓝桥杯2022初赛] 最少刷题数

import java.io.*;
import java.util.*;
public class Main 
    static Scanner cin=new Scanner(System.in);
    static PrintWriter cout=new PrintWriter(System.out);
    static int N=100010;
    static int a[]=new int[N],b[]=new int[N];
    public static void solve()
        int n=cin.nextInt();
        for(int i=1;i<=n;i++) 
            a[i]=cin.nextInt();
            b[a[i]]++;
        
        for(int i=1;i<=100000;i++) b[i]+=b[i-1];
        for(int i=1;i<=n;i++)
            if(b[100000]-b[a[i]]<=b[Math.max(0,a[i]-1)])
                cout.print(0+" ");continue;
            
            int l=a[i]+1,r=100000,mid,ans=0;
            while(l<=r)
                mid=l+r>>1;
                if(b[100000]-b[mid]<=b[mid-1]-1) //只处理大于情况,减一代表去除本身
                    r=mid-1;ans=mid;
                
                else
                    l=mid+1;
            
            cout.print(ans-a[i]+" ");
        
    
    public static void main(String[] args) throws IOException 
        solve();
        cout.close();
    

2057: [蓝桥杯2022初赛] 山

import java.io.*;
import java.math.*;
import java.util.*;
public class Main 
    static Scanner cin=new Scanner(System.in);
    static PrintWriter cout=new PrintWriter(System.out);
    static int N=100010;
    static int a[]=new int[N];
    public static boolean check(long x)
        String str=x+"";
        int len=str.length();
        char s[]=str.toCharArray();
        for(int i=0,j=len-1;i<j;i++,j--)
            if(s[i]!=s[j])
                return false;
        
        for(int i=1;i<(len+1)/以上是关于2022蓝桥杯Java A组B组的主要内容,如果未能解决你的问题,请参考以下文章

第六届蓝桥杯大赛个人赛省赛java b组试题 三羊献瑞怎么解

蓝桥杯javaabc组区别

2022年第十三届蓝桥杯比赛Java B组 全部真题答案解析-第一部分

2022年第十三届蓝桥杯省赛Java B组真题及题解

蓝桥杯题目难吗

第十三届蓝桥杯大赛软件赛省赛(Java 大学A组)