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

Posted 白鳯

tags:

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

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

文章目录

1、熊怪吃核桃

解法:暴力模拟

package sixSession;

/*** 2015第六届 1、熊怪吃核桃 ***/
public class test1 
    public static void main(String[] args) 
        int n = 1543;
        int throwNum = 0;
        while (n > 0) 
            if (n % 2 == 1) 
                n -= 1;
                throwNum += 1;
            
            n /= 2;
        
        System.out.println(throwNum);
    


答案:5


2、星系炸弹

解法:时间函数

package sixSession;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/*** 2015第六届 2、星系炸弹 ***/
public class test2 
    public static void main(String[] args) 
        Calendar calendar = Calendar.getInstance();
        // 注意 month 是从0开始的
        calendar.set(2014, 10, 9);
        calendar.add(calendar.DATE, 1000);
        SimpleDateFormat smdate = new SimpleDateFormat("yyyy-MM-dd");
        String sdate = smdate.format(calendar.getTime());
        System.out.println(sdate);
    

答案:2017-08-05


3、九数分三组

解法:全排列+剪枝

package sixSession;

/*** 2015第六届 3、九数分三组 ***/
public class test3 
    public static void main(String[] args) 
        int[] a = 1, 2, 3, 4, 5, 6, 7, 8, 9;
        backtrack(a, 0, 9);
    

    private static void backtrack(int[] a, int begin, int end) 
        if (begin > 6) 
            int A = a[0] * 100 + a[1] * 10 + a[2];
            int B = a[3] * 100 + a[4] * 10 + a[5];
            if (2 * A != B) return;
        
        if (begin == end) 
            helper(a);
            return;
        

        for (int i = begin; i < end; i++) 
            int t = a[begin]; a[begin] = a[i]; a[i] = t;
            backtrack(a, begin + 1, end);
            t = a[begin]; a[begin] = a[i]; a[i] = t;
        
    

    private static void helper(int[] a) 
        int A = a[0] * 100 + a[1] * 10 + a[2];
        int B = a[3] * 100 + a[4] * 10 + a[5];
        int C = a[6] * 100 + a[7] * 10 + a[8];
        if (2 * A != B || 3 * A != C) return;
        System.out.print(A + " ");
    

答案:192 219 273 327


4、循环节长度

package sixSession;

import java.util.Vector;

/*** 2015第六届 4、循环节长度 ***/
public class test4 
    public static int f(int n, int m)
    
        n = n % m;
        Vector v = new Vector();
        for(;;)
        
            v.add(n);
            n *= 10;
            n = n % m;
            if(n==0) return 0;
            if(v.indexOf(n)>=0) return v.size() - v.indexOf(n);
        
    
    public static void main(String[] args)
    
        System.out.println(f(1, 8));
        System.out.println(f(8, 3));
        System.out.println(f(11, 13));
        System.out.println(f(39, 190));
    

答案:return v.size() - v.indexOf(n)


5、打印萎形

找规律

package sixSession;

/*** 2015第六届 5、打印萎形 ***/
public class test5 
    public static void f(int n) 
        String s = "*";
        for (int i = 0; i < 2 * n - 3; i++) s += ".";
        s += "*";

        String s1 = s + "\\n";
        String s2 = "";

        for (int i = 0; i < n - 1; i++) 
            s = "." + s.substring(0, s.length() - 3) + "*";
            s1 = s + "\\n" + s1;
            s2 += s + "\\n";
        
        System.out.println(s1 + s2);
    

    public static void main(String[] args) 
        f(7);
    

答案:s.substring(0, s.length() - 3)


6、加法变乘法

解法一:排列组合+回溯法

package sixSession;

/*** 2015第六届 6、加法变乘法 ***/
public class test6 
    public static void main(String[] args) 
        int[] a = new int[48];
        backtrack(a, 0, 48, 2);
    

    private static void backtrack(int[] a, int begin, int end, int k) 
        if (k == 0) 
            int pos = check(a);
            if (pos != -1 && pos != 10) System.out.println(pos);
            return;
        

        for (int i = begin; i < end; i++) 
            a[i] = 1;
            k -= 1;
            backtrack(a, i + 1, end, k);
            k += 1;
            a[i] = 0;
        
    

    private static int check(int[] a) 
        int sum = 1, pos = -1;
        boolean multi = false;
        for (int i = 0; i < 48; i++) 
            if (a[i] == 0) 
                sum += i + 2;
                multi = false;
             else 
                if (multi == false) 
                    if (pos == -1) pos = i + 1;
                    sum -= (i + 1);
                    sum += (i + 1) * (i + 2);
                 else 
                    return -1;
                
                multi = true;
            
        
        return sum == 2015 ? pos : -1;
    

解法二:枚举

package sixSession;

/*** 2015第六届 6、加法变乘法 枚举法 ***/
public class test6_2 
    public static void main(String[] args) 
        for (int i = 1; i <= 46; i++) 
            for (int j = i + 2; j <= 48; j++) 
                if (i * (i + 1) - (i + i + 1) + j * (j + 1) - (j + j + 1) == 2015 - 1225) 
                    if (i != 10) System.out.println(i);
                
            
        
    

答案:16


7、牌型种数

排列组合+递归

package sixSession;

/*** 2015第六届 7、牌型种数 ***/
public class test7 
    private static int ans;
    public static void main(String[] args) 
        f(0, 0);
        System.out.println(ans);
    

    private static void f(int k, int cnt) 
        if (k > 13 || cnt > 13) return;
        if (k == 13 && cnt == 13) 
            ans++;
            return;
        
        for (int i = 0; i < 5; i++) 
            f(k + 1, cnt + i);
        
    

答案:3598180


8、移动距离

推理坐标

package sixSession;

import java.util.Scanner;

/*** 2015第六届 8、移动距离 ***/
public class test8 
    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int m = sc.nextInt();
        int n = sc.nextInt();
        sc.close();

        int rm = m % w == 0 ? m / w : m / w + 1;
        int rn = n % w == 0 ? n / w : n / w + 1;
        int cm = rm % 2 == 0 ? ((w - m % w + 1) % w) : m % w;
        int cn = rn % 2 == 0 ? ((w - n % w + 1) % w) : n % w;

        int ans = Math.abs(rm - rn) + Math.abs(cm - cn);
        System.out.println(ans);
    


9、垒骰子

解法一:递归(3/5),heap超出内存

package sixSession;

import java.util.Scanner;

/*** 2015第六届 9、垒骰子 递归 ***/
public class test9 
    static final int mod = (int)1e9 + 7;
    static final int[] op = 0, 4, 5, 6, 1, 2, 3;
    static boolean[][] conflict = new boolean[7][7];

    public static void main(String[] args) 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();
        for (int i = 0; i < m; i++) 
            int x = sc.nextInt(), y = sc.nextInt();
            conflict[x][y] = true;
            conflict[y][x] = true;
        
        sc.close();

        long ans = 0;
        for (int up = 1; up <= 6; up++) 
            ans = (ans +  4 * f(up, n - 1)) % mod;
        
        System.out.println(ans);
    

    private static long f(int up, int cnt) 
        if (cnt == 0) 
            return 1;
        
        long ans = 0;
        for (int u = 1; u <= 6; u++) 
            if (conflict[op[up]][u]) continue;
            ans = (ans 蓝桥日记①2017第八届省赛(软件类)JavaA组❤️答案解析

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

蓝桥日记③2016第七届省赛(软件类)JavaA组✿答案解析

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

蓝桥杯赛前冲刺-枚举暴力和排序专题2(包含历年蓝桥杯真题和AC代码)

第六届蓝桥杯JavaA组解析(2015年)