算法笔记_214:第六届蓝桥杯软件类校赛真题(Java语言A组)

目录

1 题目一

2 题目二

3 题目三

4 题目四

5 题目五

6 题目六

7 题目七

 

 

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


1 题目一

一个串的子串是指该串的一个连续的局部。如果不要求连续,则可称为它的子序列。
比如对串: "abcdefg" 而言,"ab","abd","bdef" 等都是它的子序列。
特别地,一个串本身,以及空串也是它的子序列。

对两个串而言,可以有许多的共同的子序列,我们关心的是:它们所共同拥有的长度最大的子序列是多长。以下代码实现了这个问题的求解。请填写划线部分缺失的代码。

注意:只填写缺少的代码,不要写任何多余的内容,比如注释或说明文字。

public class Zixulie
{
    public static int f(String x, String y)
    {
        if(x.length()==0) return 0;
        if(y.length()==0) return 0;
        
        String x1 = x.substring(1);
        String y1 = y.substring(1);    
        
        if(x.charAt(0)==y.charAt(0)) return f(x1,y1)+1;
        
        return __________________________;
    }
    
    public static void main(String[] args)
    {
        System.out.println(f("ac","abcd")); //2
        System.out.println(f("acebbcde1133","xya33bc11de")); //5
    }
}



答案:Math.max(f(x, y1),f(x1, y))

 

 

 


2 题目二

历史上有许多计算圆周率pai的公式,其中,格雷戈里和莱布尼茨发现了下面的公式:

pai = 4*(1-1/3+1/5-1/7 ....)

参见【图1.png】

这个公式简单而优美,但美中不足,它收敛的太慢了。
如果我们四舍五入保留它的两位小数,那么:

累积1项是:4.00
累积2项是:2.67
累积3项是:3.47
。。。

请你写出它累积100项是多少(四舍五入到小数后两位)。

注意:只填写该小数本身,不要填写任何多余的说明或解释文字。


答案:3.13

 

 

 1 public class Main {
 2     
 3     public static void main(String[] args) {
 4         double result = 0;
 5         for(int i = 1;i <= 100;i++) {
 6             double b = 2 * i - 1;
 7             if(i % 2 == 1) {
 8                 result = result + 1 / b;
 9             } else {
10                 result = result - 1 / b;
11             }
12         }
13         System.out.printf("%.2f", (4*result));
14     }
15     
16 }

 

 

 

 


3 题目三

如果x的x次幂结果为10(参见【图1.png】),你能计算出x的近似值吗?

显然,这个值是介于2和3之间的一个数字。

请把x的值计算到小数后6位(四舍五入),并填写这个小数值。

注意:只填写一个小数,不要写任何多余的符号或说明。



答案:2.506184

 

 

 1 public class Main {
 2     
 3     public static double min = 10;
 4     
 5     public static void main(String[] args) {
 6         double result = 0;
 7         for(double i = 2.5;i <= 2.6;i = i + 0.00000001) {
 8             double r = Math.abs(10 - Math.pow(i, i));
 9             if(min > r) {
10                 min = r;
11                 result = i;
12             }
13         }
14         System.out.printf("%.6f", result);
15         System.out.println("\n"+result);
16         System.out.println(Math.pow(result, result));
17     }
18     
19 }

 

 

 

 


4 题目四

今有7对数字:两个1,两个2,两个3,...两个7,把它们排成一行。
要求,两个1间有1个其它数字,两个2间有2个其它数字,以此类推,两个7之间有7个其它数字。如下就是一个符合要求的排列:

17126425374635

当然,如果把它倒过来,也是符合要求的。

请你找出另一种符合要求的排列法,并且这个排列法是以74开头的。

注意:只填写这个14位的整数,不能填写任何多余的内容,比如说明注释等。



答案:74151643752362

 

 1 public class Main {
 2     
 3     public void swap(int[] A, int i, int j) {
 4         int temp = A[i];
 5         A[i] = A[j];
 6         A[j] = temp;
 7     }
 8     
 9     public void dfs(int[] A, int step) {
10         if(step == A.length) {
11             StringBuffer s = new StringBuffer("");
12             for(int i = 0;i < A.length;i++)
13                 s.append(A[i]);
14             boolean judge = true;
15             String s1 = s.toString();
16             for(int i = 1;i <= 7;i++) {
17                 int a = s1.indexOf(i+"");
18                 int b = s1.lastIndexOf(""+i);
19                 if(b - a != i + 1) {
20                     judge = false;
21                     break;
22                 }
23             }
24             if(judge)
25                 System.out.println(s1);
26             return;
27         } else {
28             for(int i = step;i < A.length;i++) {
29                 if(A[step] == 7 || A[step] == 4)
30                     dfs(A, step + 1);
31                 if(A[i] == 7 || A[i] == 4)
32                     continue;
33                 swap(A, i, step);
34                 dfs(A, step + 1);
35                 swap(A, i, step);
36             }
37         }
38     }
39     
40     public static void main(String[] args) {
41         Main test = new Main();
42         int[] A = {7,4,1,1,2,2,4,3,7,3,5,5,6,6};
43         test.dfs(A, 0);
44     }
45 }

 

 

 

 


5 题目五

 
  勾股定理,西方称为毕达哥拉斯定理,它所对应的三角形现在称为:直角三角形。

  已知直角三角形的斜边是某个整数,并且要求另外两条边也必须是整数。

  求满足这个条件的不同直角三角形的个数。

【数据格式】
输入一个整数 n (0<n<10000000) 表示直角三角形斜边的长度。
要求输出一个整数,表示满足条件的直角三角形个数。

例如,输入:
5
程序应该输出:
1

再例如,输入:
100
程序应该输出:
2

再例如,输入:
3
程序应该输出:
0


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms


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

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

 

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static long n;
 5     public static int result;
 6     
 7     public void getResult() {
 8         for(long i = 1;i < n;i++) {
 9             for(long j = n - i + 1;j < n;j++) {
10                 long temp = i * i + j * j;
11                 if(temp == n * n && j >= i)
12                     result++;
13             }
14         }
15         System.out.println(result);
16     }
17     
18     public static void main(String[] args) {
19         Main test = new Main();
20         Scanner in = new Scanner(System.in);
21         n = in.nextLong();
22         test.getResult();
23     }
24           
25 }

 

 

 

 


6 题目六

你一定听说过“数独”游戏。
如【图1.png】,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。

数独的答案都是唯一的,所以,多个解也称为无解。

本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。

本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。

格式要求,输入9行,每行9个字符,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。

例如:
输入(即图中题目):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700

程序应该输出:
145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764

再例如,输入:
800000000
003600000
070090200
050007000
000045700
000100030
001000068
008500010
090000400

程序应该输出:
812753649
943682175
675491283
154237896
369845721
287169534
521974368
438526917
796318452


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 2000ms


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

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

 

 

 

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static int[][] group = new int[9][9];
 5     public static int[][] value = new int[9][9];
 6     public static int[][] row = new int[9][9];
 7     public static int[][] ring = new int[9][9];
 8     public static int[][] get_group = new int[9][9];
 9     
10     public void init() {
11         String[] S = new String[9];
12         for(int i = 0;i < 3;i++) {
13             S[i] = "000111222";
14             S[3 + i] = "333444555";
15             S[6 + i] = "666777888";
16         }
17         for(int i = 0;i < 9;i++)
18             for(int j = 0;j < S[i].length();j++)
19                 group[i][j] = S[i].charAt(j) - '0';
20         for(int i = 0;i < 9;i++)
21             for(int j = 0;j < 9;j++) {
22                 row[i][j] = -1;
23                 ring[i][j] = -1;
24                 get_group[i][j] = -1;
25             }
26     }
27     
28     public void dfs(int step) {
29         if(step == 81) {
30             for(int i = 0;i < 9;i++) {
31                 for(int j = 0;j < 9;j++)
32                     System.out.print(value[i][j]);
33                 System.out.println();
34             }
35             return;
36         } else {
37             int x = step / 9;
38             int y = step % 9;
39             if(value[x][y] > 0)
40                 dfs(step + 1);
41             else {
42                 for(int i = 1;i <= 9;i++) {
43                     if(row[x][i - 1] == -1 && ring[y][i - 1] == -1 && 
44                              get_group[group[x][y]][i - 1] == -1 && value[x][y] == 0) {
45                         value[x][y] = i;
46                          row[x][i - 1] = 1;
47                         ring[y][i - 1] = 1;
48                         get_group[group[x][y]][i - 1] = 1;
49                         dfs(step + 1);
50                          value[x][y] = 0;
51                         row[x][i - 1] = -1;
52                          ring[y][i - 1] = -1;
53                         get_group[group[x][y]][i - 1] = -1;
54                     }
55                 }
56             }
57         }
58     }
59     
60     public static void main(String[] args) {
61         Main test = new Main();
62         test.init();
63         Scanner in = new Scanner(System.in);
64         for(int i = 0;i < 9;i++) {
65             String s = in.next();
66             for(int j = 0;j < s.length();j++) {
67                 int t = s.charAt(j) - '0';
68                 value[i][j] = t;
69                 if(t != 0) {
70                     row[i][t - 1] = 1;
71                     ring[j][t - 1] = 1;
72                     get_group[group[i][j]][t - 1] = 1;
73                 }
74             }
75         }
76         test.dfs(0);
77     }
78 }

 

 

 

 


7 题目7

给定平面内平行于坐标轴的一个矩形,从矩形内选

择一些点,从这些点向右和向上各射出一条射线,

请问:这些射线将矩形分成了多少份。

数据格式:

输入的第一行包含两个整数x, y,表示矩形是由(0, 

0), (x, 0), (x, y), (0, y)四个点围成的。
第二行包含一个整数n,表示矩形内的点的数量。
接下来n行,每个两个整数xi, yi,表示矩形内的一

个点。输入保证所有的点都在矩形内部而且没有两

个点有相同的横坐标或纵坐标。

输出一个整数,表示从给定的点射出的射线将矩形

分成的份数。

例如,输入:
10 10
3
1 3
3 1
2 4
程序应该输出:
6

【数据规模和约定】
对于10%的数据,1<=n<=10,1<=x, y<=100;
对于30%的数据,1<=n<=10001<=x,y<=1000;
对于60%的数据,1<=n<=100001<=x,y<=1000000;
对于100%的数据,1<=n<=1000001<=x,y<=1000000000,1<xi<x,1<yi<y。



资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“

请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝

提交该源码。
注意:不要使用package语句。不要使用jdk1.7及

以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码

处理。


以下代码仅供参考,不保证答案的正误哦。

 

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 import java.util.Collections;
 4 import java.util.Comparator;
 5 import java.util.Scanner;
 6 
 7 public class Main {
 8      public static long x, y;
 9      public static int n;
10      public static Point[] P;
11      public static long count = 1;
12      public static ArrayList<Integer> list = new ArrayList<Integer>();
13      
14      class MyComparator implements Comparator<Point> {
15 
16         public int compare(Point arg0, Point arg1) {
17             if(arg0.x > arg1.x)
18                 return 1;
19             else if(arg0.x < arg1.x)
20                 return -1;
21             else if(arg0.x == arg1.x) {
22                 if(arg0.y > arg0.y)
23                     return 1;
24                 else if(arg0.y < arg0.y)
25                     return -1;
26                 else
27                     return 0;
28             }
29             return 0;
30         }
31          
32      }
33      
34      static class Point {
35          public int x;
36          public int y;
37          
38          public Point(int x, int y) {
39              this.x = x;
40              this.y = y;
41          }
42      }
43      
44      public void getResult() {
45          Arrays.sort(P, new MyComparator());
46          list.add(P[n - 1].y);
47          count++;
48          for(int i = n - 2;i >= 0;i--) {
49              if(P[i].x == P[i + 1].x) {
50                  if(P[i + 1].y > P[i].y) {
51                      int j = list.indexOf(P[i + 1].y);
52                      list.remove(j);
53                      list.add(P[i].y);
54                  }
55                  count++;
56              }
57              else {
58                  Collections.sort(list);
59                  int j = 0;
60                  for(;j < list.size();j++)
61                      if(list.get(j) >= P[i].y)
62                          break;
63                  count = count + j + 1;
64                  if(!list.contains(P[i].y))
65                      list.add(P[i].y);
66              }
67          }
68          System.out.println(count);
69      }
70      
71      public static void main(String[] args) {
72          Main test = new Main();
73          Scanner in = new Scanner(System.in);
74          x = in.nextLong();
75          y = in.nextLong();
76          n = in.nextInt();
77          P = new Point[n];
78          for(int i = 0;i < n;i++) {
79              int x = in.nextInt();
80              int y = in.nextInt();
81              P[i] = new Point(x, y);
82          }
83          test.getResult();
84      }
85 }

 

 

 

 

 

 

posted @ 2017-05-21 10:48  舞动的心  阅读(1383)  评论(0编辑  收藏  举报