第九届蓝桥杯Java B组

标题:第几天

答案:125

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 31 + 29 + 31 + 30 + 4;
        System.out.println(sum);
    }
} 

标题:方格计数

答案:3137548

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ans = 0;
        for(int i = 0; i <= 1000; i++)
        {
            for(int j = 0; j <= 1000; j++)
            {
                if(check(i, j)) ans++;
            }
        }
        System.out.println(ans * 4);
    }
    public static boolean check(int i, int j)
    {
        if(i*i + j*j > 1000 * 1000) return false;
        if((i-1)*(i-1) + j*j > 1000 * 1000) return false;
        if(i*i + (j+1)*(j+1) > 1000 * 1000) return false;
        if((i+1)*(i+1) + (j+1)*(j+1) > 1000 * 1000) return false;
        return true;
    }
} 

标题:复数幂

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger a = new BigInteger("2");
        BigInteger b = new BigInteger("3");
        BigInteger c = new BigInteger("2");
        BigInteger d = new BigInteger("3");
        for(int i = 1; i <= 12345; i++)
        {
            BigInteger A = a.multiply(c).subtract(b.multiply(d));
            BigInteger B = a.multiply(d).add(b.multiply(c));
            a = A;
            b = B;
        }
        System.out.println(a + "
" + b);
    }
} 

标题:测试次数

标题:递增三元组

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];
        for(int i = 0; i < n; i++) a[i] = sc.nextInt();
        for(int i = 0; i < n; i++) b[i] = sc.nextInt();
        for(int i = 0; i < n; i++) c[i] = sc.nextInt();
        Arrays.sort(a);
        Arrays.sort(b);
        Arrays.sort(c);
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            int x = lower_bounder(a, b[i]);
            int y = uppder_bounder(c, b[i]);
            sum += (x + 1) *  (n - y);
        }
        System.out.println(sum);
    }
    public static int lower_bounder(int[] nums, int target)
    {
        int l = 0, r = nums.length - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        return l;
    }
    public static int uppder_bounder(int[] nums, int target)
    {
        int l = 0, r = nums.length - 1;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(nums[mid] <= target) l = mid + 1;
            else r = mid;
        }
        return l;
    }
} 

标题:螺旋折线

import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Long x = sc.nextLong();
        Long y = sc.nextLong();
        Long n = Math.max(Math.abs(x), Math.abs(y));
        Long sum1 = 4 * n * (n-1); //计算内圈的步数
        Long d1 = x + n, d2 = y + n;
        Long sum2 = 0L; //计算当前圈的步数
        if(y > x) sum2 = d1 + d2;
        else sum2 = 8 * n - (d1 + d2);
        System.out.println(sum1 + sum2);
    }
} 

标题:日志统计

import java.math.*;
import java.util.*;

class node
{
    int ts, id;
    node(int ts, int id)
    {
        this.ts = ts;
        this.id = id;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int d = sc.nextInt();
        int k = sc.nextInt();
        List<node> L = new ArrayList<>();
        for(int i = 0; i < n; i++)
        {
            int ts = sc.nextInt();
            int id = sc.nextInt();
            L.add(new node(ts, id));
        }
        Collections.sort(L, new Comparator<node>()
        {
            public int compare(node a, node b)
            {
                return a.ts - b.ts;
            }
        });
        int len = L.size();
        int[] sum = new int[100010];
        Set<Integer> ans = new TreeSet<>();
        for(int i = 0, j = 0; i < len; i++)
        {
            sum[L.get(i).id]++;
            while(L.get(i).ts - L.get(j).ts >= d)
            {
                sum[L.get(j).id]--;
                j++;
            }
            if(sum[L.get(i).id] >= k)
                ans.add(L.get(i).id);
        }
        for(int i : ans)
            System.out.println(i);
    }
}

标题:全球变暖

import java.math.*;
import java.util.*;

public class Main {
    static boolean flag = false;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] mp = new String[n];
        int[][] vis = new int[n][n];
        for(int i = 0; i < n; i++)
            mp[i] = sc.next();
        int ans = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                flag = false;
                if(mp[i].charAt(j) == '#' && vis[i][j] == 0)
                {
                    dfs(i, j, mp, vis);
                    if(flag == false) 
                        ans++;
                }
            }
        }
        System.out.println(ans);
    }
    public static void dfs(int i, int j, String[] mp, int[][] vis)
    {
        int n = mp.length;
        int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int cnt = 0;
        vis[i][j] = 1;
        for(int k = 0; k <= 3; k++)
        {
            int tmpi = i + dir[k][0];
            int tmpj = j + dir[k][1];
            if(tmpi < 0 || tmpi >= n || tmpj < 0 || tmpj >= n)
            {
                cnt++;
                continue;
            }
            if(mp[tmpi].charAt(tmpj) == '#')
                cnt++;
            if(mp[tmpi].charAt(tmpj) == '#' && vis[tmpi][tmpj] == 0)
                dfs(tmpi, tmpj, mp, vis);
            if(cnt == 4)
                flag = true;
        }
    }
}
原文地址:https://www.cnblogs.com/benzikun/p/14641596.html