算法笔记_213:第七届蓝桥杯软件类决赛部分真题(Java语言C组)

目录

1 平方末尾

2 七星填数

3 打印数字

4 赢球票

  前言:以下代码仅供参考,若有错误欢迎指正哦~


1 平方末尾

平方末尾

能够表示为某个整数的平方的数字称为“平方数”
比如,25,64
虽然无法立即说出某个数是平方数,但经常可以断定某个数不是平方数。
因为平方数的末位只可能是:[0, 1, 4, 5, 6, 9] 这6个数字中的某个。
所以,4325435332必然不是平方数。

如果给你一个2位或2位以上的数字,你能根据末位的两位来断定它不是平方数吗?

请计算一下,一个2位以上的平方数的最后两位有多少种可能性?

注意:需要提交的是一个整数,表示2位以上的平方数最后两位的不同情况数。
不要填写任何多余内容(比如,说明解释文字等)

答案:22
 1 import java.util.HashSet;
 2 
 3 public class Main {
 4     public static HashSet<String> set = new HashSet<String>();
 5     
 6     public static void main(String[] args) {  
 7         for(long i = 10;i <= 1000000;i++) {
 8             String a = "" + (i * i / 10 % 10) * 10 + "" +i * i % 10;
 9             set.add(a);
10         }
11         System.out.println(set.size());
12     }
13 }

2 七星填数

七星填数

如图【图1.png】所示。

在七角星的14个节点上填入1~14 的数字,不重复,不遗漏。
要求每条直线上的四个数字之和必须相等。

图中已经给出了3个数字。
请计算其它位置要填充的数字,答案唯一。

填好后,请提交绿色节点的4个数字(从左到右,用空格分开)

比如:12 5 4 8
当然,这不是正确的答案。

注意:只提交4个用空格分开的数字,不要填写任何多余的内容。


答案:10 3 9 8

 

 1 import java.util.HashSet;
 2 
 3 public class Main {
 4     public static int sum = 0;
 5     
 6     public void swap(int[] A, int i, int j) {
 7         int temp = A[i];
 8         A[i] = A[j];
 9         A[j] = temp;
10     }
11     
12     public void dfs(int[] A, int step) {
13         if(step == A.length) {
14             int[] count = new int[7];
15             count[0] = A[0] + A[1] + A[2] + A[3];
16             count[1] = A[0] + A[4] + A[6] + A[9];
17             count[2] = A[1] + A[4] + 6 + 14;
18             count[3] = A[2] + A[5] + 6 + 11;
19             count[4] = A[6] + A[8] + A[10] + 14;
20             count[5] = A[7] + A[8] + A[9] + 11;
21             count[6] = A[3] + A[5] + A[7] + A[10];
22             HashSet<Integer> set = new HashSet<Integer>();
23             for(int i = 0;i < 7;i++)
24                 set.add(count[i]);
25             if(set.size() == 1) {
26                 for(int i = 0;i < A.length;i++)
27                     System.out.print(A[i]+" ");
28                 System.out.println();
29             }
30             sum++;
31             return;
32         } else {
33             for(int i = step;i < A.length;i++) {
34                 swap(A, i, step);
35                 dfs(A, step + 1);
36                 swap(A, i, step);
37             }
38         }
39     }
40     
41     public static void main(String[] args) {
42         Main test = new Main();
43         int[] A = {1,2,3,4,5,7,8,9,10,12,13};
44         test.dfs(A, 0);
45         System.out.println(sum);
46     }
47 }

3 打印数字

打印数字

小明写了一个有趣的程序,给定一串数字。
它可以输出这串数字拼出放大的自己的样子。

比如“2016”会输出为:
 22222   00000       1   6666
2     2 0     0    1 1  6
      2 0     0      1  666666
     2  0     0      1  6     6
   2    0     0      1  6     6
 2    2 0     0      1  6     6
2222222  00000     1111  66666

请仔细分析代码,填写划线部分缺少的内容。


public class Main
{
    static void f(int n)
    {
        String[][] di = 
    {{" 00000 ",
    "0     0",
    "0     0",
    "0     0",
    "0     0",
    "0     0",
    " 00000 "},
    {"     1 ",
    "   1 1 ",
    "     1 ",
    "     1 ",
    "     1 ",
    "     1 ",
    "   1111"},
    {" 22222 ",
    "2     2",
    "      2",
    "     2 ",
    "   2   ",
    " 2    2",
    "2222222"},
    {" 33333 ",
    "3     3",
    "      3",
    "  3333 ",
    "      3",
    "3     3",
    " 33333 "},
    {"   44  ",
    "  4 4  ",
    " 4  4  ",
    "4   4  ",
    "4   4  ",
    "4444444",
    "    4  "},
    {" 55555 ",
    " 5     ",
    "555555 ",
    "      5",
    "      5",
    "5     5",
    " 55555 "},
    {" 6666  ",
    "6      ",
    "666666 ",
    "6     6",
    "6     6",
    "6     6",
    " 66666 "},
    {"7777777",
    "7    7 ",
    "    7  ",
    "   7   ",
    "  7    ",
    " 7     ",
    " 7     "},
    {" 88888 ",
    "8     8",
    "8     8",
    " 88888 ",
    "8     8",
    "8     8",
    " 88888 "},
    {" 99999 ",
    "9     9",
    "9     9",
    " 999999",
    "      9",
    "9     9",
    " 99999 "}};
                       
        char[] cc = (""+n).toCharArray(); 
                  
        for(int i=0; i<di[0].length; i++){
            for(int j=0; j<cc.length; j++){
                System.out.print( ____________________ + " ");  //填空位置
            }
            System.out.println();
        }    
    }
    
    public static void main(String[] args)
    {
        f(2016);
    }
}

注意:只提交划线部分缺少的代码,不要添加任何题面已有代码或符号。
也不要提交任何说明解释文字等。

答案:di[cc[j]-'0'][i]

4 赢球票

赢球票

某机构举办球票大奖赛。获奖选手有机会赢得若干张球票。

主持人拿出 N 张卡片(上面写着 1~N 的数字),打乱顺序,排成一个圆圈。
你可以从任意一张卡片开始顺时针数数: 1,2,3.....
如果数到的数字刚好和卡片上的数字相同,则把该卡片收入囊中,从下一个卡片重新数数。
直到再无法收获任何卡片,游戏结束。囊中卡片数字的和就是赢得球票的张数。

比如:
卡片排列是:1 2 3
我们从1号卡开始数,就把1号卡拿走。再从2号卡开始,但数的数字无法与卡片对上,
很快数字越来越大,不可能再拿走卡片了。因此这次我们只赢得了1张球票。

还不算太坏!如果我们开始就傻傻地从2或3号卡片数起,那就一张卡片都拿不到了。

如果运气好,卡片排列是 2 1 3
那我们可以顺利拿到所有的卡片!

本题的目标就是:已知顺时针卡片序列。
随便你从哪里开始数,求最多能赢多少张球票(就是收入囊中的卡片数字之和)

输入数据:
第一行一个整数N(N<100),表示卡片数目
第二行 N 个整数,表示顺时针排列的卡片

输出数据:
一行,一个整数,表示最好情况下能赢得多少张球票

比如:
用户输入:
3
1 2 3

程序应该输出:
1

比如:
用户输入:
3
2 1 3

程序应该输出:
6


资源约定:
峰值内存消耗 < 256M
CPU消耗  < 1000ms



请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static int n;
 5     public static int max = 0;
 6     public static int[] value;
 7     
 8     public void getResult() {
 9         for(int i = 0;i < n;i++) {
10             int[] temp = new int[n];
11             for(int k = 0;k < n;k++)
12                 temp[k] = value[k];
13             int sum = 0;
14             int count = 1;
15             int start = i;
16             while(true) {
17                 boolean judge = true;
18                 for(int k = 0;k < n;k++)
19                     if(temp[k] >= count) {
20                         judge = false;
21                         break;
22                     }
23                 if(judge)
24                     break;
25                 int j = start % n;
26                 if(temp[j] == count) {
27                     sum = sum + count;
28                     temp[j] = -1;
29                     count = 1;
30                 } else if(temp[j] != -1)
31                     count++;
32                 start++;
33             }
34             max = Math.max(max, sum);
35         }
36         System.out.println(max);
37     }
38     
39     public static void main(String[] args) {
40         Main test = new Main();
41         Scanner in = new Scanner(System.in);
42         n = in.nextInt();
43         value = new int[n];
44         for(int i = 0;i < n;i++)
45             value[i] = in.nextInt();
46         test.getResult();
47     }
48 }

 

原文地址:https://www.cnblogs.com/liuzhen1995/p/6882752.html