1. 程式人生 > >2018 藍橋杯省賽 B 組模擬賽(一)

2018 藍橋杯省賽 B 組模擬賽(一)

1.

今天蒜頭君帶著花椰妹和朋友們一起聚會,當朋友們問起年齡的時候,蒜頭君打了一個啞謎(畢竟年齡是女孩子的隱私)說:“我的年齡是花椰妹年齡個位數和十位數之和的二倍”。

花椰妹看大家一臉懵逼,就知道大家也不知道蒜頭君的年齡,便連忙補充道:“我的年齡是蒜頭君個位數和十位數之和的三倍”。

請你計算:蒜頭君和花椰妹年齡一共有多少種可能情況?

提醒:兩位的年齡都是在 [10,100)

[10,100) 這個區間內。


這個題很水,直接上程式碼

package day2;

public class T1 {
	public static void main(String[] args) {
		int a,b,c,d;
		int i,j;
		int count=0;
		for(i=10;i<101;i++){
			for(j=10;j<101;j++){
				a=i/10;
				b=i%10;
				c=j/10;
				d=j%10;
				if(((c+d)*2==(a*10+b)) && ((a+b)*3)==(c*10+d)){
					count++;
					System.out.println(a+""+ b +" " + c+"" +d);
				}
			}
		}
	}
}
2.

蒜頭君今天回到了老家的大宅院,老家的燈還是那中拉線的燈(拉一次為亮,再拉一次就滅),蒜頭君覺得無聊。把 10001000 盞燈 33 的倍數拉了一次,55 的倍數拉了一次,7的倍數拉了一次(燈得的編號從 1-100011000,燈的初始狀態都是亮的)。這個時候蒜頭君在想還剩下幾盞燈還在亮著?

提示:請不要輸出多餘的符號。

package day2;

public class T2 {
	public static void main(String[] args) {
		int[] a = new int[1002];
		int count=0;
		for(int i=1;i<=1000;i++){
			a[i]=0;
		}
		for(int i=1;i<1001;i++){
			if(i%3==0){
				a[i]++;
			}
			
			if(i%5==0){
				a[i]++;
			}
			
			if(i%7==0){
				a[i]++;
			}
		}
		for(int i=1;i<=1000;i++){
			if(a[i]%2==0){
				count++;
			}
		}
		System.out.println(count);
	}
}
3.

最近蒜頭君喜歡上了U型數字,所謂U型數字,就是這個數字的每一位先嚴格單調遞減,後嚴格單調遞增。比如 212212 就是一個U型數字,但是 33333398985675673131331313,就是不是U型數字。

現在蒜頭君問你,[1,100000][1,100000] 有多少U型數字?

提示:請不要輸出多餘的符號。


package day2;

public class T3 {
	public static void main(String[] args) {
		int a,b,c;
		int d,e,f,g;
		int h,i,j,k,l;
		int count=0;
		for(a=0;a<10;a++)
		for(b=0;b<10;b++)
		for(c=0;c<10;c++){
			if(a>b && c>b)
				count++;
		}
		
		for(d=0;d<10;d++)
		for(e=0;e<10;e++)
		for(f=0;f<10;f++)
		for(g=0;g<10;g++){
			if(d>e && g>f && e!=f){
				count++;
			}
		}
		
		for(h=0;h<10;h++)
		for(i=0;i<10;i++)
		for(j=0;j<10;j++)
		for(k=0;k<10;k++)
		for(l=0;l<10;l++){
			if(i>j && h>i && k>j && l>k){
				count++;
			}
			if(l>k && j>k && i>j && h>i){
				count++;
			}
			if(h>i && j>i && k>j && l>k){
				count++;
			}
		}
		
		System.out.println(count);
	}
}


解法二:

package day2;  
  
import java.util.*;  
public class T3 {  
    public static void main(String[] args){  
        int a,b,c;  
        int sum = 0;  
        for (int i=100;i<=999;i++){  
            a=i%10;//個  
            b=i/10%10;//十  
            c=i/100%10;//百  
            if (c>b&&b<a){  
                sum++;  
            }  
  
        }  
        int x,y,z,n;  
        for (int i=1000;i<=9999;i++){  
            x = i%10;  
            y = i/10%10;  
            z = i/100%10;  
            n = i/1000%10;  
            if (n>z&&y<x&&z!=y){  
                sum++;  
                //System.out.println(i);  
            }  
  
        }  
        int q,w,e,r,t;  
        for (int i=10000;i<100000;i++){  
            q = i % 10;  
            w = i/10%10;  
            e = i/100%10;  
            r = i/1000%10;  
            t = i/10000%10;  
            if (t>r&&r>e&&e<w&&w<q){  
                sum++;  
            }  
            if (t>r&&r<e&&e<w&&w<q){  
                sum++;  
            }  
            if (t>r&&r>e&&e>w&&w<q){  
                sum++;  
            }  
        }  
        System.out.println(sum);  
  
    }  
}  

4.

LIS是最長上升子序列。什麼是最長上升子序列? 就是給你一個序列,請你在其中求出一段最長嚴格上升的部分,它不一定要連續。

就像這樣:22334477 和 22334466 就是序列 22 55 33 44 11 77 66 的兩個上升子序列,最長的長度是 44

動態規劃的最長上升子序列,動態規劃學的不行,回頭好好看看

import java.util.*;
import java.math.*;

public class Main {
    public static final int N = 1000;
    public static int[] f = new int[N];
    public static int[] b = new int[N];

    public static int max(int a, int b) {
        if (a > b) {
            return a;
        }
        return b;
    }

    public static int lis(int n) {
        int res = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (b[j] < b[i]) {
                    f[i] = max(f[i],f[j]+1);
                }
            }
            res = max(res, f[i]);
        }
        return res+1;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        int n = cin.nextInt();
        for (int i = 0; i < n; ++i) {
        	b[i] = cin.nextInt() ;
        }
        System.out.println(lis(n));
    }
}



補充:

5.

程式碼填空

相信大家都知道什麼是全排列,但是今天的全排列比你想象中的難一點。我們要找的是全排列中,排列結果互不相同的個數。比如:aab 的全排列就只有三種,那就是aab,baa,aba

程式碼框中的程式碼是一種實現,請分析並填寫缺失的程式碼。

import java.util.*;
import java.math.*;

public class Main {
    public static final int N = 1000;
    public static char[] str = new char[N];
    public static char[] buf = new char[N];
    public static int[] vis = new int[N];
    public static int total = 0;
    public static int len = 0;

    public static void arrange(int num) {
        if (num == len) {
            for (int i = 0; i < len; ++i) {
                System.out.print(buf[i]);
            }
            System.out.println();
            total++;
            return;
        }
        for (int i = 0; i < len; ++i) {
            if (vis[i] == 0) {
                int j = 0;
                for (j = i + 1; j < len; ++j) {
                    if (vis[j] && str[i]==str[j]) {//缺失的部分
                        break;
                    }
                }
                if (j == len) {
                    vis[i] = 1;
                    buf[num] = str[i];
                    arrange(num + 1);
                    vis[i]= 0;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        str = cin.next().toCharArray();
        total = 0;
        len = str.length;
        buf[len] = '\0';
        int i = 0, j = 0;
        for (i = 0; i < len; ++i) {
            for (j = i + 1; j < len; ++j) {
                if (str[i] > str[j]) {
            		char tmp = str[i];
            		str[i] = str[j];
            		str[j] = tmp;
        		}
            }
        }
        arrange(0);
        System.out.println("Total " + total);
    }
}


6.

蒜頭君今天突然開始還念童年了,想回憶回憶童年。他記得自己小時候,有一個很火的遊戲叫做數獨。便開始來了一局緊張而又刺激的高階數獨。蒜頭君做完發現沒有正解,不知道對不對? 不知道聰明的你能否給出一個標準答案?

標準數獨是由一個給與了提示數字的 9 \times 99×9 網格組成,我們只需將其空格填上數字,使得每一行,每一列以及每一個 3 \times 33×3 宮都沒有重複的數字出現。

輸出這個數獨得正解,輸出格式如下:


 
  
  
   
    
     
      
       
       
       
       
        
       
       
        
         
          
           1
          
         
         
* 2 6 * * * * * *
2
* * * 5 * 2 * * 4
3
* * * 1 * * * * 7
4
* 3 * * 2 * 1 8 *
5
* * * 3 * 9 * * *
6
* 5 4 * 1 * * 7 *
7
5 * * * * 1 * * *
8
6 * * 9 * 7 * * *
9
* * * * * * 7 5 *











把上面的 * 替換成 1 - 919 就可以了

提醒:兩個數字之間要有一個空格,其他地方不要輸出多餘的符號。

本題答案不唯一,符合要求的答案均正確

連結:https://blog.csdn.net/qq_32473657/article/details/50927383

傳送門