2016年 蓝桥杯省赛测试题(Java)

一、

java中提供了对正则表达式的支持。
有的时候,恰当地使用正则,可以让我们的工作事半功倍!

如下代码用来检验一个四则运算式中数据项的数目,请填写划线部分缺少的代码。

注意:只填写缺少代码,不要写任何多余内容,例如,已有的双引号。

public class A
{
	public static int f(String s)
	{
		return s.split("________________").length;
	}
	
	public static void main(String[] args)
	{
		System.out.println(f("12+35*5-2*18/9-3")); //7
		System.out.println(f("354*12+3-14/7*6")); //6
	}
}

public class A
{
	public static int f(String s)
	{
		return s.split("\\p{Punct}").length;
	}
	
	public static void main(String[] args)
	{
		System.out.println(f("12+35*5-2*18/9-3")); //7
		System.out.println(f("354*12+3-14/7*6")); //6
	}
}


二、

1/1 + 1/2 + 1/3 + 1/4 + ... 在数学上称为调和级数。

它是发散的,也就是说,只要加上足够多的项,就可以得到任意大的数字。

但是,它发散的很慢:

前1项和达到 1.0
前4项和才超过 2.0
前83项的和才超过 5.0

那么,请你计算一下,要加多少项,才能使得和达到或超过 15.0 呢?

请填写这个整数。

注意:只需要填写一个整数,不要填写任何多余的内容。比如说明文字。

1835421

public class Main {
    public static void main(String[] args){
    	Double sum = 0.0;
    	boolean flag = true;
    	for(int i = 1; ; ++i){
    		sum += 1.0/i;
    		if(sum >= 15.0){
    			System.out.println(i);
    			break;
    		}
    	}
    }
}

三、


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

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

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

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


2.506184

public class Main {
	static double eps = 1e-7;
    public static void main(String[] args){
    	double l = 2,r = 3,mid;
    	while(l+eps < r){
    		mid = (l+r)/2;
    		if(Math.pow(mid,mid) < 10)
    			l = mid;
    		else
    			r = mid;
    	}
    	System.out.printf("%.6f\n",l);
    }
}

四、

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

17126425374635

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

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

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

74151643752


五、

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

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

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

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

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

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

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

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

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

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

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sca = new Scanner(System.in);
		int n = sca.nextInt(),cnt = 0;
		int t = (int) Math.sqrt(n*n/2.0);
		for(int i = 1; i <= t; ++i){
			int j = (int) Math.sqrt(n*n*1.0-i*i);
			if(i*i+j*j == n*n)
				++cnt;
		}
		System.out.println(cnt);
	}
}


六、

你一定听说过“数独”游戏。
如下图,玩家需要根据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,否则按无效代码处理。


import java.util.ArrayList;  
import java.util.List;  
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		List<int[][]> solutions;
		Scanner sca = new Scanner(System.in);
		int data[][] = new int[9][9];
		String str = new String();
		for(int i = 0; i < 9; i++){
			str = sca.nextLine();
			for(int j = 0; j < 9; j++){
				data[i][j] = str.charAt(j)-'0';
			}
		}
		int n = 9;
		DLX dlx = new DLX(n*n*n+1,4*n*n);
		dlx.setNum(5);
		dlx.solve(data);
		solutions = dlx.getSolutions();
		int solution[][] = solutions.get(0); 
		for(int i = 0; i < 9; i++){
			for(int j = 0; j < 9; j++){
				System.out.print(Integer.toString(solution[i][j]));
			}
			System.out.println();
		}
	}
}

class DLX{  
    private static final int ROW = 4096 + 50;  
    private static final int COL = 1024 + 50;  
    private static final int N = 4 * 9 * 9;  
    private static final int m = 3;  
  
    DLXNode row[] = new DLXNode[ROW];  
    DLXNode col[] = new DLXNode[COL];  
    DLXNode head;  
  
    private int n;  
    private int num = 2;  
    private int size[] = new int[COL];  
    int data[][] = new int[9][9];  
    List<int[][]> solutions;  
  
    public DLX(int r, int c){  
        n = m * m;  
        head = new DLXNode(r, c);  
        head.U = head.D = head.L = head.R = head;  
        for(int i = 0; i < c; ++i){  
            col[i] = new DLXNode(r, i);  
            col[i].L = head;  
            col[i].R = head.R;  
            col[i].L.R = col[i].R.L = col[i];  
            col[i].U = col[i].D = col[i];  
            size[i] = 0;  
        }  
  
        for(int i = r - 1; i > -1; i--){  
            row[i] = new DLXNode(i, c);  
            row[i].U = head;  
            row[i].D = head.D;  
            row[i].U.D = row[i].D.U = row[i];  
            row[i].L = row[i].R = row[i];  
        }  
    }  
  
    public void addNode(int r, int c){  
        DLXNode p = new DLXNode(r, c);  
        p.R = row[r];  
        p.L = row[r].L;  
        p.L.R = p.R.L = p;  
        p.U = col[c];  
        p.D = col[c].D;  
        p.U.D = p.D.U = p;  
        ++size[c];  
    }  
  
    public void addNode(int i, int j, int k){  
        int r = (i * n + j) * n + k;  
        addNode(r, i * n + k - 1);  
        addNode(r, n * n + j * n + k - 1);  
        addNode(r, 2 * n * n + block(i, j) * n + k - 1);  
        addNode(r, 3 * n * n + i * n + j);  
    }  
  
    int block(int x, int y){  
        return x / m * m + y / m;  
    }  
  
    public void cover(int c){  
        if(c == N)  
            return;  
  
        col[c].delLR();  
        DLXNode R, C;  
        for(C = col[c].D; C != col[c]; C = C.D){  
            if (C.c == N)  
                continue;  
            for (R = C.L; R != C; R = R.L){  
                if (R.c == N)  
                    continue;  
                --size[R.c];  
                R.delUD();  
            }  
            C.delLR();  
        }  
    }  
  
    public void resume(int c){  
        if (c == N)  
            return;  
  
        DLXNode R, C;  
        for(C = col[c].U; C != col[c]; C = C.U){  
            if (C.c == N)  
                continue;  
            C.resumeLR();  
            for (R = C.R; R != C; R = R.R){  
                if (R.c == N)  
                    continue;  
                ++size[R.c];  
                R.resumeUD();  
            }  
        }  
        col[c].resumeLR();  
    }  
  
    public boolean solve(int depth){  
        if(head.L == head){  
            int solution[][] = new int[n][n];  
            for (int i = 0; i < n; ++i)  
                for (int j = 0; j < n; ++j)  
                    solution[i][j] = data[i][j];  
            solutions.add(solution);  
  
            if (solutions.size() == num)  
                return true;  
            return false;  
        }  
        int minSize = 1 << 30;  
        int c = -1;  
        DLXNode p;  
        for(p = head.L; p != head; p = p.L){
            if (size[p.c] < minSize){  
                minSize = size[p.c];  
                c = p.c;  
            }  
        }
        cover(c);  
  
        for(p = col[c].D; p != col[c]; p = p.D){  
            DLXNode cell;  
            p.R.L = p;  
            for (cell = p.L; cell != p; cell = cell.L){  
                cover(cell.c);  
            }  
            p.R.L = p.L;  
            int rr = p.r - 1;  
            data[rr / (n * n)][rr / n % n] = rr % n + 1;  
            if (solve(depth + 1))  
                return true;  
  
            p.L.R = p;  
            for (cell = p.R; cell != p; cell = cell.R)  
                resume(cell.c);  
            p.L.R = p.R;  
        }  
  
        resume(c);  
        return false;  
    }  
  
    public boolean solve(int data[][]){  
        init(data);  
        return solve(0);  
    }  
  
    public void init(int data[][]){  
        solutions = new ArrayList<int[][]>();  
        int i, j, k;  
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                if (data[i][j] > 0){
                    addNode(i, j, data[i][j]);  
                }else{
                    for (k = 1; k <= n; ++k)  
                        addNode(i, j, k);  
                }  
            }  
        }
    } 
  
    public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public List<int[][]> getSolutions(){  
        return solutions;  
    }  
}  
  
class DLXNode{  
    int r,c;  
    DLXNode U,D,L,R;  
      
    DLXNode(){  
        r = c = 0;  
    }  
      
    DLXNode(int r, int c){  
        this.r = r;  
        this.c = c;  
    }  
  
    DLXNode(int r, int c, DLXNode U, DLXNode D, DLXNode L, DLXNode R){  
        this.r = r;  
        this.c = c;  
        this.U = U;  
        this.D = D;  
        this.L = L;  
        this.R = R;  
    }  
  
    public void delLR(){  
        L.R = R;  
        R.L = L;  
    }  
      
    public void delUD(){  
        U.D = D;  
        D.U = U;  
    }  
      
    public void resumeLR(){  
        L.R = R.L = this;  
    }  
      
    public void resumeUD(){  
        U.D = D.U = this;  
    }  
}  


七、

G将军有一支训练有素的军队,这个军队除开G将军外,每名士兵都有一个直接上级(可能是其他士兵,也可能是G将军)。现在G将军将接受一个特别的任务,需要派遣一部分士兵(至少一个)组成一个敢死队,为了增加敢死队队员的独立性,要求如果一名士兵在敢死队中,他的直接上级不能在敢死队中。
请问,G将军有多少种派出敢死队的方法。注意,G将军也可以作为一个士兵进入敢死队。
输入格式
输入的第一行包含一个整数n,表示包括G将军在内的军队的人数。军队的士兵从1至n编号,G将军编号为1。
接下来n-1个数,分别表示编号为2, 3, ..., n的士兵的直接上级编号,编号i的士兵的直接上级的编号小于i。
输出格式
输出一个整数,表示派出敢死队的方案数。由于数目可能很大,你只需要输出这个数除10007的余数即可。
样例输入1
3
1 1
样例输出1
4
样例说明
这四种方式分别是:
1. 选1;
2. 选2;
3. 选3;
4. 选2, 3。
样例输入2
7
1 1 2 2 3 3
样例输出2
40

数据规模与约定
对于20%的数据,n ≤ 20;
对于40%的数据,n ≤ 100;
对于100%的数据,1 ≤ n ≤ 100000。

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

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

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




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值