蓝桥日记②2018第九届省赛(软件类)JavaA组★答案解析

Posted 白鳯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥日记②2018第九届省赛(软件类)JavaA组★答案解析相关的知识,希望对你有一定的参考价值。

【蓝桥日记②】2018第九届省赛(软件类)JavaA组🏅答案解析

文章目录


题目链接:第九届蓝桥杯大赛个人赛省赛(软件类)Java大学A组

官网题库中搜索相应题目

1、分数

考点:等比数列求和

  • 等差数列求和公式
    S n = n ( a 1 + a n ) 2 = n a 1 + n ( n − 1 ) d 2 , n ∈ N ∗ S_n = \\fracn(a_1+a_n)2=na_1+\\fracn(n-1)d2,n ∈ N^* Sn=2n(a1+an)=na1+2n(n1)d,nN

  • 等比数列求和公式
    a n = a 1 q n − 1 S n = 1 − q n 1 − q , q ≠ 1 a_n=a_1q^n-1 \\\\ S_n=\\frac1-q^n1-q, q\\neq1 an=a1qn1Sn=1q1qn,q=1

方法一:Java大整数运算

package nineSession;
import java.math.BigInteger;

/*** 2017第九届  1、分数 ***/
public class test1 
    public static void main(String[] args) 
        // 等比数列通项和 S = a1(1 - q^n) / (1 - q)
        // 将a1 = 1, q = 1 / 2, n = 20 代入化解
        BigInteger e = BigInteger.TWO;  // BigInteger.valueOf(2)
        BigInteger a = e.pow(20).subtract(BigInteger.ONE);
        BigInteger b = e.pow(19);
        BigInteger g = a.gcd(b);
        a.divide(g);
        b.divide(g);
        System.out.println(a + "/" + b);
    

方法二:位运算

package nineSession;

/*** 2017第九届  1、分数 ***/
public class test1_3 
    private static long gcd(long a, long b) 
        return b == 0 ? a : gcd(b, a % b);
    

    public static void main(String[] args) 
        long a = (1 << 20) - 1;
        long b = (1 << 19);
        long g = gcd(a, b);
        System.out.println(a / g + "/" + b / g);
    

方法三:快速幂+最大公约数

package nineSession;

/*** 2017第九届  1、分数 ***/
public class test1_2 
    private static long gcd(long a, long b) 
        return b == 0 ? a : gcd(b, a % b);
    

    private static long fastPow(long e, int q) 
        long res = 1;
        while (q > 0) 
            if ((q & 1) == 1) res = res * e;
            e = e * e;
            q >>= 1;
        
        return res;
    

    public static void main(String[] args) 
        long a = fastPow(2, 20) - 1;
        long b = fastPow(2, 19);
        long g = gcd(a, b);
        a /= g;
        b /= g;
        System.out.println(a + "/" + b);
    

答案:1048575/524288


2、星期一

package nineSession;

/*** 2017第九届  2、星期一 ***/
public class test2 
    private static boolean isLeap(int y) 
        return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
    

    public static void main(String[] args) 
        int t = -6; // 2000-12-31 为星期日
        for (int i = 1901; i <= 2000; i++) 
            t += isLeap(i) ? 366 : 365;
        
        int res = 0;
        for (int i = t; i > 0; i -= 7) res += 1;
        System.out.println(res);
    

答案:5217


3、复数幂

考点:BigInteger

package nineSession;

import java.math.BigInteger;

/*** 2017第九届  3、复数幂 ***/
public class test3 
    public static void main(String[] args) 
        // (a + bi)(x + yi) = ax - by + (ay + bx)i
        BigInteger a = BigInteger.valueOf(2);
        BigInteger b = BigInteger.valueOf(3);
        BigInteger x = BigInteger.valueOf(2);
        BigInteger y = BigInteger.valueOf(3);
        for (int i = 1; i < 123456; i++) 
            BigInteger a1 = a.multiply(x).subtract(b.multiply(y));
            BigInteger b1 = a.multiply(y).add(b.multiply(x));
            a = a1;
            b = b1;
        
        String sign = b.compareTo(BigInteger.ZERO) > 0 ? "+" : "";
        System.out.println(a + sign + b + "i");
    

答案:太长了~


4、方格计数

考点:计算点到圆心距离是否小于半径

package nineSession;

/*** 2017第九届  4、方格计数 ***/
public class test4 
    public static void main(String[] args) 
        long res = 0, range = 50000 * 50000L;
        for (int i = 1; i < 50000; i++) 
            for (int j = 1; j < 50000; j++) 
                if ((long)i * i + (long)j * j <= range) res += 4;
            
        
        System.out.println(res);
    

答案:7853781044


5、打印图形

考点:递归

package nineSession;

/*** 2017第九届  5、打印图形 ***/
public class test5 
    static void show(byte[][] buf) 
        for (int i = 0; i < buf.length; i++) 
            for (int j = 0; j < buf[i].length; j++) 
                System.out.print(buf[i][j] == 0 ? ' ' : 'o');
            
            System.out.println();
        
    

    static void draw(byte[][] buf, int x, int y, int size) 
        if (size == 1) 
            buf[y][x] = 1;
            return;
        
        int n = size / 3;
        draw(buf, x, y, n);
        draw(buf, x - n, y, n);
        draw(buf, x + n, y, n);
        draw(buf, x, y - n, n);
        draw(buf, x, y + n, n);
    

    public static void main(String[] args) 
        final int N = 3;
        int t = 1;
        for (int i = 0; i < N; i++) t *= 3;

        byte[][] buf = new byte[t][t];
        draw(buf, t / 2, t / 2, t);
        show(buf);
    

答案:size / 3


6、航班时间

考点:时间函数+字符串处理

package nineSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*** 2017第九届  6.航班时间 ***/
public class test6 
    public static void main(String[] args) throws ParseException 
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        sc.nextLine();
        String[] lines = new String[T * 2];
        for (int i = 0; i < T * 2; i++) 
            lines[i] = sc.nextLine();
        
        sc.close();

        for (int i = 0; i < T; i++) 
            long time1 = getTime(lines[i * 2]);
            long time2 = getTime(lines[i * 2 + 1]);
            long t = (time1 + time2) / 2;
            System.out.printf("%02d:%02d:%02d\\n", t / 3600, t / 60 % 60, t % 60);
        
    

    private static long getTime(String line) throws ParseException 
        String[] split = line.split(" ");
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date t1 = format.parse(split[0]);
        Date t2 = format.parse(split[1]);
        long d = 0l;
        if (split.length == 3) 
            d = (long)(split[2].charAt(2) - '0');
        
        return d * 24 * 3600 + t2.getTime() / 1000 - t1.getTime() / 1000;
    


7、三体攻击

方法一:暴力模拟(超时 3/4)

package nineSession;
import java.util.Scanner;

/*** 2017第九届  7.三体攻击 ***/
public class test7 
    static int A, B, C, m蓝桥日记③2016第七届省赛(软件类)JavaA组✿答案解析

第九届蓝桥杯JavaA组(2018年)省赛真题解析

蓝桥日记⑤2014第五届省赛(软件类)JavaA组❆答案解析

蓝桥日记④2015第六届省赛(软件类)JavaA组➤答案解析

蓝桥日记⑥2013第四届省赛(软件类)JavaA组@答案解析

蓝桥杯单片机第九届省赛客观题(深夜学习——单片机)