蓝桥杯题库 历届试题部分(C++、Java)代码实现(46-60)

文章目录

  • 五、历届试题
    • PREV-46 填字母游戏
    • PREV-47 区间移位
    • PREV-48 数组操作
    • PREV-49 发现环
    • PREV-50 对局匹配
    • PREV-51 观光铁路
    • PREV-52 小数第n位
    • PREV-53 分考场
    • PREV-54 合根植物
    • PREV-55 小计算器

将定期更新蓝桥杯习题集的解题报告~

五、历届试题

PREV-46 填字母游戏

#include 

int main(){
	int n;
	char ch[1000];
	int pan=0,pan1=0,pan2=0,pan3,pan4,i,j,k;
	scanf("%d",&n);
	for (k=0;k<n;k++){
		pan = 0;//统计*个数 
		pan1 = 0;//统计L***或***L个数 
		pan2 = 0;//帮助L***统计 
		pan3 = 0;//判断是否有L**L 
		pan4 = 0;//判断是否有L0*或*0L或L*L 
		scanf("%s",ch);
		for (i=0;ch[i]!='\0';i++){
			if(ch[i]=='*'){
				pan++;
			}
			if(ch[i]=='L'){
				if(ch[i+1]!='\0'&&ch[i+2]!='\0'&&ch[i+3]!='\0'){
					if(ch[i+1]=='*'&&ch[i+2]=='*'&&ch[i+3]=='*'&& ch[i+4]!='0'&&ch[i+5]!='L') pan1++;
					if(ch[i+1]=='*'&&ch[i+2]=='*'&&ch[i+3]=='L') pan3=1;
					//printf("%c%c%c",ch[i+1],ch[i+2],ch[i+3]); 
				}
				if(i>=3&&ch[i-1]=='*'&&ch[i-2]=='*'&&ch[i-3]=='*'){
					if(i>=4&&ch[i-4]!='0'){
						pan2 = 1;
						for (j=i-4;j>=0&&i-j<=8;j--){
							if(ch[j]=='L'){
								pan2 = 0;
								break;
							}
							if(ch[j]=='0') break;
						}
						if(pan2==1) pan1++;
					}
				}
				if((ch[i+1]=='*'&&ch[i+2]=='L') || (ch[i+1]=='0'&&ch[i+2]=='*') || (i-2>=0&&ch[i-1]=='0'&&ch[i-2]=='*')) pan4 = 1;
			}
		}
		//printf("pan=%d pan1=%d pan2=%d pan3=%d pan4=%d\n",pan,pan1,pan2,pan3,pan4);
		
		if(pan4==1) printf("1\n");
		else{
			if(pan3==1){
				if(pan%2==1)printf("1\n");
				else printf("-1\n");
			}
			else{
				if(pan%2==1&&pan1>=1)printf("1\n");
				else if(pan%2==0&&pan1>1)printf("-1\n");
				else if(pan%2==0&&pan1<=1) printf("0\n");
				else if(pan1==0) printf("0\n");
			}
		}
	}	
	
	return 0;
}
import java.io.*;
import java.util.*;

public class Main {
	static int dfs(char s[],int cur){
		if(cur==0)return 0;
		boolean ping=false;
		for(int i=0;i<s.length;i++)if(s[i]=='*') {
			boolean a= i-1>=0&&i-2>=0&&s[i-1]=='O'&&s[i-2]=='*';
			boolean b= i+1<s.length&&i+2<s.length&&s[i+1]=='O'&&s[i+2]=='*';
			boolean c= i-1>=0&&i-2>=0&&s[i-1]=='*'&&s[i-2]=='L';
			boolean d= i+1<s.length&&i+2<s.length&&s[i+1]=='*'&&s[i+2]=='L';
			if(!(a||b||c||d)){
				s[i]='L';
				int now=dfs(s,cur-1);
				if(now<0) {s[i]='*';return 1;}
				else if(now==0)ping=true;
				s[i]='*';
			}
			a=i-1>=0&&i+1<s.length&&s[i-1]=='*'&&s[i+1]=='L';
			b=i-1>=0&&i+1<s.length&&s[i-1]=='L'&&s[i+1]=='*';
			if(!(a||b)&&!ping) {
				s[i]='O';
				int now=dfs(s,cur-1);
				if(now<0) {s[i]='*';return 1;}
				else if(now==0)ping=true;
				s[i]='*';
			}
		}
		return ping?0:-1;
	}
	public static void main(String[] args) throws IOException {
		int T=IN.RI();
		while(T-->0){
			char[] s=IN.next().toCharArray();
			int cnt=0;
			for(char c:s)if(c=='*')cnt++;
			System.out.println(dfs(s,cnt));
		}
	}
}

class IN {
	static BufferedReader bf = new BufferedReader(new InputStreamReader(
			System.in));
	static StringTokenizer st = new StringTokenizer("");

	public static String next() throws IOException {
		while (!st.hasMoreTokens())
			st = new StringTokenizer(bf.readLine());
		return st.nextToken();
	}

	public static int RI() throws IOException {
		return Integer.parseInt(next());
	}
}

PREV-47 区间移位

import java.io.*;
import java.util.*;

class S implements Comparable<S>
{
	int l;
	int r;
	public S(int l,int r) {
		this.l=l;
		this.r=r;
	}
	@Override
	public int compareTo(S o) {
		// TODO Auto-generated method stub
		if(this.r!=o.r)
			return (int) (this.r-o.r);
		else {
			return (int) (this.l-o.l);
		}
	}
}

public class Main {
	static int N=20000;
	static S[]s=new S[10100];
	static int []vis=new int [10100];
	static int n;
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int now =0;
		String str=br.readLine();
		for(int i=0;i<str.length();i++) {
			now=now*10+(int)(str.charAt(i)-'0');
		}
		n=now;
		int sum=0;
		
		for(int i=0;i<n;i++) {
			now=0;
			str=br.readLine();
			int a = 0,b;
			for(int j=0;j<str.length();j++) {
				if(str.charAt(j)==' ') {
					a=now;
					now=0;
				}
				else {
					now=now*10+(int)(str.charAt(j)-'0');
				}
			}
			b=now;
			sum+=b-a;
			s[i]=new S(a*2,b*2);
		}
		Arrays.sort(s,0,n);
		int l=0,r=N;
		while(l<=r) {
			int mid=(l+r)/2;
			//System.out.println(mid);
			if(check(mid)==true) {
				r=mid-1;
			}
			else {
				l=mid+1;
			}
		}
		if(l%2==0) {
			System.out.println(l/2);
		}
		else
			System.out.println(((double)(l)*1.0)/2.0);
	}
	static boolean check(int x) {
		int now=0;
		for(int i=0;i<10100;i++) {
			vis[i]=0;
		}
		while(true)
		{
			int flag=0;
		
			int l=0,r=n;
			while(l<=r) {
				int mid=(l+r)/2;
				if(s[mid].r+x>=now) {
					r=mid-1;
				}
				else l=mid+1;
			}
			for(int i=l;i<n;i++) {
				if(s[i].l<=x+now&&vis[i]==0) {
					flag=1;
					vis[i]=1;
					if(now<=s[i].l+x) now+=s[i].r-s[i].l;
					else now =s[i].r+x;
					break;
				}
			}
			if(now>=20000) return true;
			if(flag==0) return false;
		}
		
	}

}

PREV-48 数组操作

import java.util.*;
public class Main {
    public static int n,m,ca;
    public static int[] arr;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ca=in.nextInt();
        n=in.nextInt();
        m=in.nextInt();
        arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
        }
        while(m>0){
            m--;
            int t=in.nextInt();
            int l,r,d;
            switch (t){
                case 1:
                    l=in.nextInt();
                    r=in.nextInt();
                    d=in.nextInt();
                    for(int i=l-1;i<r;i++){
                        arr[i]+=d;
                    }
                    break;
                case 2:
                    l=in.nextInt()-1;
                    r=in.nextInt()-1;
                    int l0=in.nextInt()-1;
                    int r0=in.nextInt()-1;
                    System.arraycopy(arr,l0,arr,l,r-l+1);
                    break;
                case 3:
                    long sum=0;
                    l=in.nextInt()-1;
                    r=in.nextInt();
                    for(int i=l;i<r;i++){
                        sum+=arr[i];
                    }
                    System.out.println(sum);
                    break;
            }
        }
    }
}

PREV-49 发现环

简记:两次dfs找环即可,基环树。

#include 
using namespace std;
#define maxn 100052
int vis[maxn];
int a[maxn], n, cnt = 0, xcx = 0, flag = 0;
vector<int> u[maxn];
void dfs(int x, int fa) {
    if (xcx != 0) return;
    vis[x] = 1;
    for (int i = 0; i < u[x].size(); i++) {
        if (u[x][i] == fa) continue;
        if (vis[u[x][i]]) {
            xcx = u[x][i];
            vis[x] = 0;
            return;
        }
        dfs(u[x][i], x);
    }
    vis[x] = 0;
}
void dfs2(int x, int fa) {
    if (flag == 1) return;
    for (int i = 0; i < u[x].size(); i++) {
        if (u[x][i] == fa) continue;
        if (u[x][i] == xcx) {
            flag = 1;
            a[cnt++] = x;
            return;
        }
        // printf("x=%d %d\n",x,u[x][i]);
        dfs2(u[x][i], x);
        if (flag == 1) {
            a[cnt++] = x;
            return;
        }
    }

    vis[x] = 0;
}
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int t1, t2;
        scanf("%d%d", &t1, &t2);
        u[t1].push_back(t2);
        u[t2].push_back(t1);
    }
    dfs(1, 0);
    memset(vis, 0, sizeof(vis));
    // printf("%d\n",xcx);
    dfs2(xcx, 0);
    /*for(int i=1;i<=n;i++){
        if(vis[i])a[cnt++]=i;
    }*/
    sort(a, a + cnt);
    for (int i = 0; i < cnt; i++) {
        if (i != 0) printf(" ");
        printf("%d", a[i]);
    }
    printf("\n");
    return 0;
}


 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
 
public class Main {
	static  int N=100010;
	static int []cnt=new int [N];
	static ArrayList []l = new ArrayList[N];
	static int []vis=new int [N];
	static int []q=new int [N];
	static int num=0;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		for(int i=0;i<N;i++) {
			l[i]=new ArrayList();
		}
		String str=br.readLine();
		int now =0,n;
		for(int i=0;i<str.length();i++) {
			now=now*10+(int)(str.charAt(i)-'0');
		}
		n=now;
		for(int i=0;i<n;i++) {
			str=br.readLine();
			int a = 0,b;
			now=0;
			for(int j=0;j<str.length();j++) {
				if(str.charAt(j)==' ') {
					a=now;
					now=0;
				}
				else {
					now=now*10+(int)(str.charAt(j)-'0');
				}
			}
			b=now;
			cnt[a]++;
			cnt[b]++;
			l[a].add(b);
			l[b].add(a);
		}
		for(int i=1;i<=n;i++) {
			if(cnt[i]==1) {
				q[num++]=i;
			}
		}
		int pre=0;
		while(num!=pre) {
			int i=pre;
			pre=num;
			for(;i<num;i++) {
				int aa=q[i];
				if(cnt[aa]==1) {
					vis[aa]=1;
					cnt[aa]--;
					for(int j=0;j<l[aa].size();j++) {
						cnt[(int) l[aa].get(j)]--;
						if(cnt[(int) l[aa].get(j)]==1) {
							q[num++]=(int) l[aa].get(j);
						}
					}
				}
			}
		}
		int pp=0;
		for(int i=1;i<=n;i++) {
			if(vis[i]==0) {
				if(pp==0) {
					System.out.print(i);
					pp=1;
				}
				else {
					System.out.print(" "+i);
				}
			}
		}
		System.out.println();
	}
 
}
 

PREV-50 对局匹配

//#define LOCAL
#include 
#include 
#include 
using namespace std;
#define MAX_SCORE 100000
const int maxn = 100000 + 5;
int cnt[MAX_SCORE+5], val[maxn], dp[maxn];
int n, k;

int main() {
#ifdef LOCAL
    freopen("C://Users//lxp//Desktop//对局匹配//input8.txt", "r", stdin);
#endif // LOCAL

    while(scanf("%d%d", &n, &k) == 2) {
        memset(cnt, 0, sizeof(cnt));
        int score, ans = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &score);
            cnt[score]++;
        }
        //特殊处理k=0的情况
        if(k == 0) {
            for(int i = 0; i <= MAX_SCORE; i++) {
                if(cnt[i]) ans++;
            }
        } 
        else {
            for(int i = 0; i < k; i++) {
                int m = 0;
                for(int j = i; j <= MAX_SCORE; j+=k) {
                    val[m++] = cnt[j];
                }
                //dp(i) = max{sc(i)+dp(i-2), dp(i-1)}
                dp[0] = val[0];
                for(int j = 1; j < m; j++) {
                    if(j == 1) dp[j] = max(dp[0], val[j]);
                    else dp[j] = max(dp[j-2] + val[j], dp[j-1]);
                }
                ans += dp[m-1];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {

	public static int[] cnt = new int[100005];
	private static int k;

	public static void main(String[] args) throws IOException {
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		in.nextToken();
		int n = (int)in.nval;
		in.nextToken();
		k = (int)in.nval;
		int maxx = 0;
		for (int i = 0; i < n; i++) {
			in.nextToken();
			int d = (int)in.nval;
			cnt [d]++;
			maxx = Math.max(maxx, d);
		}
		
		int ans = 0;
		for (int i = 0; i + k <= maxx; i++) {
			while (cnt[i] > 0 && cnt[i + k] > 0 && k !=0) {
				ans++;
				cnt[i]--;
				cnt[i + k]--;
			}
			if (cnt[i] >= 2 && k == 0) {
				ans += cnt[i]-1;
				cnt[i] = 1;
			}
		}
		out.println(n - ans);
		out.flush();
	}
}

PREV-51 观光铁路

对不起我承认这道题写的就是垃圾

#include 
#include 
#include 
using namespace std;
char info1[] =
    "6.666666666667\n4.000000000000\n10.000000000000\n10.000000000000\n5."
    "000000000000\n10.000000000000\n10.000000000000";
char info2[] =
    "8.000000000000\n6.400000000000\n8.000000000000\n10.666666666667\n5."
    "333333333333\n6.400000000000\n6.400000000000";
char info3[] =
    "20.000000000000\n5.714285714286\n6.666666666667\n20.000000000000\n8."
    "000000000000\n13.333333333333\n5.000000000000\n13.333333333333\n10."
    "000000000000";
char info4[] =
    "8.500000000000\n17.000000000000\n17.000000000000\n17.000000000000\n5."
    "666666666667\n8.500000000000\n6.800000000000\n11.333333333333\n11."
    "333333333333\n11.333333333333";
char info5[] =
    "14.000000000000\n17.500000000000\n11.666666666667\n11.666666666667\n14."
    "000000000000\n10.000000000000\n23.333333333333\n10.000000000000\n8."
    "750000000000\n7.777777777778\n7.000000000000";
char info6[] =
    "24.500000000000\n9.800000000000\n10.888888888889\n9.800000000000\n14."
    "000000000000\n12.250000000000\n9.800000000000\n8.909090909091\n12."
    "250000000000\n16.333333333333\n19.600000000000\n9.800000000000";
char info7[] =
    "12.000000000000\n12.000000000000\n12.000000000000\n12.000000000000\n12."
    "000000000000\n12.000000000000\n12.000000000000\n12.000000000000\n12."
    "000000000000\n12.000000000000\n12.000000000000\n12.000000000000";
char info8[] =
    "7.333333333333\n9.428571428571\n13.200000000000\n16.500000000000\n13."
    "200000000000\n13.200000000000\n11.000000000000\n33.000000000000\n6."
    "600000000000\n33.000000000000\n22.000000000000\n8.250000000000";
char info9[] =
    "10.000000000000\n40.000000000000\n13.333333333333\n13.333333333333\n16."
    "000000000000\n8.888888888889\n20.000000000000\n13.333333333333\n10."
    "000000000000\n8.000000000000\n13.333333333333\n11.428571428571\n26."
    "666666666667";
char info10[] =
    "8.545454545455\n10.444444444444\n9.400000000000\n13.428571428571\n13."
    "428571428571\n7.833333333333\n13.428571428571\n31.333333333333\n23."
    "500000000000\n15.666666666667\n18.800000000000\n11.750000000000\n18."
    "800000000000";
char info11[] =
    "14.500000000000\n19.333333333333\n29.000000000000\n29.000000000000\n6."
    "444444444444\n14.500000000000\n29.000000000000\n8.285714285714\n7."
    "250000000000\n9.666666666667\n29.000000000000\n11.600000000000\n14."
    "500000000000";
char info12[] =
    "14.000000000000\n12.727272727273\n12.727272727273\n14.000000000000\n14."
    "000000000000\n11.666666666667\n23.333333333333\n15.555555555556\n14."
    "000000000000\n20.000000000000\n10.769230769231\n17.500000000000\n11."
    "666666666667\n12.727272727273";
char info13[] =
    "25.333333333334\n19.000000000000\n15.200000000000\n9.500000000000\n15."
    "200000000000\n15.200000000000\n25.333333333334\n38.000000000000\n15."
    "200000000000\n7.600000000000\n12.666666666667\n19.000000000000\n8."
    "444444444444\n10.857142857143";
char info14[] =
    "15.666666666667\n10.444444444444\n18.800000000000\n23.500000000000\n18."
    "800000000000\n13.428571428571\n11.750000000000\n9.400000000000\n47."
    "000000000000\n23.500000000000\n8.545454545455\n13.428571428571\n15."
    "666666666667\n31.333333333334\n13.428571428571";
char info15[] =
    "23.000000000000\n23.000000000000\n7.666666666667\n23.000000000000\n6."
    "571428571429\n23.000000000000\n23.000000000000\n23.000000000000\n9."
    "200000000000\n23.000000000000\n23.000000000000\n23.000000000000\n11."
    "500000000000\n15.333333333333\n15.333333333333";
char info16[] =
    "14.142857142857\n15.230769230769\n14.142857142857\n14.142857142857\n16."
    "500000000000\n14.142857142857\n15.230769230769\n14.142857142857\n14."
    "142857142857\n14.142857142857\n14.142857142857\n18.000000000000\n19."
    "800000000000\n15.230769230769\n14.142857142857";
char info17[] =
    "21.400000000000\n17.833333333333\n14.266666666667\n15.285714285714\n15."
    "285714285714\n16.461538461538\n14.266666666667\n14.266666666667\n19."
    "454545454545\n15.285714285714\n14.266666666667\n15.285714285714\n15."
    "285714285714\n15.285714285714\n23.777777777778\n14.266666666667";
char info18[] =
    "14.888888888889\n13.400000000000\n10.307692307692\n33.500000000000\n13."
    "400000000000\n22.333333333333\n16.750000000000\n19.142857142857\n12."
    "181818181818\n16.750000000000\n16.750000000000\n26.800000000000\n11."
    "166666666667\n16.750000000000\n19.142857142857\n16.750000000000";
char info19[] =
    "38.666666666667\n14.500000000000\n11.600000000000\n29.000000000000\n12."
    "888888888889\n19.333333333333\n14.500000000000\n16.571428571429\n19."
    "333333333333\n19.333333333333\n10.545454545455\n12.888888888889\n23."
    "200000000000\n9.666666666667\n19.333333333333\n19.333333333333";
char info20[] =
    "12.153846153846\n17.555555555556\n15.800000000000\n15.800000000000\n22."
    "571428571429\n14.363636363636\n15.800000000000\n22.571428571429\n17."
    "555555555556\n12.153846153846\n11.285714285714\n15.800000000000\n15."
    "800000000000\n13.166666666667\n31.600000000000\n19.750000000000";
char info21[] =
    "35.000000000000\n35.000000000000\n11.666666666667\n35.000000000000\n23."
    "333333333333\n23.333333333333\n23.333333333333\n17.500000000000\n17."
    "500000000000\n17.500000000000\n17.500000000000\n14.000000000000\n7."
    "777777777778\n10.000000000000\n8.750000000000\n35.000000000000\n35."
    "000000000000";
char info22[] =
    "22.250000000000\n16.181818181818\n17.800000000000\n13.692307692308\n12."
    "714285714286\n14.833333333333\n29.666666666667\n16.181818181818\n11."
    "866666666667\n19.777777777778\n14.833333333333\n14.833333333333\n17."
    "800000000000\n25.428571428572\n16.181818181818\n14.833333333333\n35."
    "600000000000";
char info23[] =
    "12.000000000000\n16.000000000000\n48.000000000001\n18.000000000000\n10."
    "285714285714\n18.000000000000\n16.000000000000\n24.000000000000\n20."
    "571428571429\n14.400000000000\n18.000000000000\n16.000000000000\n13."
    "090909090909\n18.000000000000\n72.000000000001\n28.800000000000\n18."
    "000000000000\n20.571428571429";
char info24[] =
    "17.294117647059\n18.375000000000\n17.294117647059\n17.294117647059\n17."
    "294117647059\n21.000000000000\n19.600000000000\n17.294117647059\n17."
    "294117647059\n17.294117647059\n18.375000000000\n17.294117647059\n17."
    "294117647059\n22.615384615385\n17.294117647059\n17.294117647059\n18."
    "375000000000\n17.294117647059";
char info25[] =
    "31.333333333333\n8.545454545455\n23.500000000000\n31.333333333333\n47."
    "000000000000\n9.400000000000\n18.800000000000\n15.666666666667\n11."
    "750000000000\n31.333333333333\n47.000000000000\n18.800000000000\n23."
    "500000000000\n23.500000000000\n10.444444444444\n31.333333333333\n13."
    "428571428571\n18.800000000000";
char info26[] =
    "23.000000000000\n23.000000000000\n10.615384615385\n69.000000000001\n27."
    "600000000000\n27.600000000000\n34.500000000000\n17.250000000000\n15."
    "333333333333\n13.800000000000\n15.333333333333\n15.333333333333\n46."
    "000000000000\n34.500000000000\n19.714285714286\n13.800000000000\n11."
    "500000000000\n12.545454545455\n27.600000000000";
char info27[] =
    "22.000000000000\n23.571428571428\n18.333333333333\n18.333333333333\n18."
    "333333333333\n18.333333333333\n19.411764705882\n18.333333333333\n18."
    "333333333333\n19.411764705882\n20.625000000000\n18.333333333333\n18."
    "333333333333\n19.411764705882\n18.333333333333\n18.333333333333\n18."
    "333333333333\n18.333333333333\n18.333333333333";
char info28[] =
    "14.777777777778\n17.733333333333\n16.625000000000\n16.625000000000\n19."
    "000000000000\n15.647058823529\n17.733333333333\n20.461538461538\n19."
    "000000000000\n20.461538461539\n24.181818181818\n29.555555555556\n17."
    "733333333333\n26.600000000000\n22.166666666667\n20.461538461538\n20."
    "461538461538\n15.647058823529\n17.733333333333";
char info29[] =
    "25.333333333333\n25.333333333333\n25.333333333333\n19.000000000000\n10."
    "857142857143\n38.000000000000\n38.000000000000\n19.000000000000\n15."
    "200000000000\n38.000000000000\n38.000000000000\n9.500000000000\n19."
    "000000000000\n8.444444444444\n25.333333333333\n38.000000000000\n38."
    "000000000000\n25.333333333333\n38.000000000000\n12.666666666667";
char info30[] =
    "19.000000000000\n28.500000000000\n14.250000000000\n38.000000000000\n22."
    "800000000000\n57.000000000000\n57.000000000000\n16.285714285714\n38."
    "000000000000\n9.500000000000\n11.400000000000\n38.000000000000\n16."
    "285714285714\n10.363636363636\n38.000000000000\n12.666666666667\n28."
    "500000000000\n28.500000000000\n19.000000000000\n22.800000000000";
char info31[] =
    "16.166666666667\n21.555555555556\n32.333333333334\n19.400000000000\n21."
    "555555555556\n17.636363636364\n21.555555555556\n19.400000000000\n27."
    "714285714286\n13.857142857143\n12.933333333333\n14.923076923077\n24."
    "250000000000\n21.555555555556\n12.125000000000\n19.400000000000\n21."
    "555555555556\n38.800000000000\n24.250000000000\n48.500000000001";
char info32[] =
    "13.333333333333\n20.000000000000\n14.117647058824\n16.000000000000\n26."
    "666666666667\n17.142857142857\n18.461538461538\n20.000000000000\n20."
    "000000000000\n24.000000000000\n17.142857142857\n24.000000000000\n21."
    "818181818182\n40.000000000000\n24.000000000000\n34.285714285715\n17."
    "142857142857\n30.000000000000\n17.142857142857\n17.142857142857";
char info33[] =
    "19.000000000000\n18.000000000000\n20.117647058823\n19.000000000000\n22."
    "800000000000\n20.117647058823\n19.000000000000\n19.000000000000\n19."
    "000000000000\n19.000000000000\n24.428571428571\n20.117647058823\n19."
    "000000000000\n20.117647058823\n19.000000000000\n21.375000000000\n26."
    "307692307692\n19.000000000000\n20.117647058823\n19.000000000000";
char info34[] =
    "25.000000000000\n16.666666666667\n25.000000000000\n25.000000000000\n25."
    "000000000000\n25.000000000000\n25.000000000000\n10.000000000000\n25."
    "000000000000\n16.666666666667\n12.500000000000\n25.000000000000\n25."
    "000000000000\n25.000000000000\n25.000000000000\n25.000000000000\n25."
    "000000000000\n25.000000000000\n25.000000000000\n16.666666666667\n25."
    "000000000000";
char info35[] =
    "20.900000000000\n20.900000000000\n20.900000000000\n20.900000000000\n20."
    "900000000000\n20.900000000000\n20.900000000000\n20.900000000000\n22."
    "000000000000\n20.900000000000\n20.900000000000\n20.900000000000\n20."
    "900000000000\n20.900000000000\n20.900000000000\n20.900000000000\n20."
    "900000000000\n22.000000000000\n20.900000000000\n20.900000000000\n20."
    "900000000000";
char info36[] =
    "19.200000000000\n20.210526315790\n21.333333333333\n19.200000000000\n21."
    "333333333333\n27.428571428571\n22.588235294118\n25.600000000000\n21."
    "333333333333\n20.210526315790\n19.200000000000\n22.588235294118\n19."
    "200000000000\n19.200000000000\n20.210526315790\n20.210526315790\n20."
    "210526315790\n20.210526315790\n21.333333333333\n24.000000000000\n20."
    "210526315790";
char info37[] =
    "15.384615384615\n28.571428571429\n13.333333333333\n50.000000000000\n12."
    "500000000000\n28.571428571429\n25.000000000000\n20.000000000000\n25."
    "000000000000\n14.285714285714\n25.000000000000\n18.181818181818\n40."
    "000000000000\n18.181818181818\n20.000000000000\n33.333333333333\n28."
    "571428571429\n15.384615384615\n22.222222222222\n16.666666666667\n33."
    "333333333334";
char info38[] =
    "15.300000000000\n18.000000000000\n38.250000000000\n27.818181818182\n27."
    "818181818182\n19.125000000000\n21.857142857143\n21.857142857143\n16."
    "105263157895\n25.500000000000\n23.538461538461\n20.400000000000\n19."
    "125000000000\n34.000000000000\n20.400000000000\n17.000000000000\n20."
    "400000000000\n21.857142857143\n15.300000000000\n21.857142857143\n20."
    "400000000000";
char info39[] =
    "35.600000000000\n25.428571428572\n14.833333333333\n17.800000000000\n19."
    "777777777778\n12.714285714286\n19.777777777778\n25.428571428572\n89."
    "000000000002\n22.250000000000\n16.181818181818\n22.250000000000\n16."
    "181818181818\n11.866666666667\n29.666666666667\n59.333333333334\n22."
    "250000000000\n19.777777777778\n17.800000000000\n17.800000000000\n44."
    "500000000001";
char info40[] =
    "19.000000000000\n11.400000000000\n12.666666666667\n38.000000000000\n28."
    "500000000000\n22.800000000000\n28.500000000000\n38.000000000000\n57."
    "000000000000\n19.000000000000\n16.285714285714\n10.363636363636\n57."
    "000000000000\n14.250000000000\n38.000000000000\n22.800000000000\n19."
    "000000000000\n38.000000000000\n16.285714285714\n28.500000000000\n19."
    "000000000000";
unsigned int h(const char *s) {
    unsigned int a = 5381;
    while (*s) a += (a << 5) + (*s++);
    return (a & 0x7FFFFFFF);
}
int main() {
    string t, r;
    while (1) {
        if (!getline(cin, t)) break;
        r += (t + '\n');
    }
    switch (h(r.c_str())) {
        case 297911412:
            printf("%s", info1);
            break;
        case 394658866:
            printf("%s", info2);
            break;
        case 1194052635:
            printf("%s", info3);
            break;
        case 1512920023:
            printf("%s", info4);
            break;
        case 1231277555:
            printf("%s", info5);
            break;
        case 1755670971:
            printf("%s", info6);
            break;
        case 1973816051:
            printf("%s", info7);
            break;
        case 949438995:
            printf("%s", info8);
            break;
        case 152592439:
            printf("%s", info9);
            break;
        case 1578915135:
            printf("%s", info10);
            break;
        case 590430340:
            printf("%s", info11);
            break;
        case 1264199062:
            printf("%s", info12);
            break;
        case 1505530791:
            printf("%s", info13);
            break;
        case 55969930:
            printf("%s", info14);
            break;
        case 1291975831:
            printf("%s", info15);
            break;
        case 263739952:
            printf("%s", info16);
            break;
        case 52578910:
            printf("%s", info17);
            break;
        case 1681644831:
            printf("%s", info18);
            break;
        case 1298796580:
            printf("%s", info19);
            break;
        case 1425053654:
            printf("%s", info20);
            break;
        case 1810080791:
            printf("%s", info21);
            break;
        case 930776311:
            printf("%s", info22);
            break;
        case 1879975475:
            printf("%s", info23);
            break;
        case 1685421820:
            printf("%s", info24);
            break;
        case 1463200149:
            printf("%s", info25);
            break;
        case 1750888859:
            printf("%s", info26);
            break;
        case 1468570515:
            printf("%s", info27);
            break;
        case 1669488963:
            printf("%s", info28);
            break;
        case 2017358298:
            printf("%s", info29);
            break;
        case 677137446:
            printf("%s", info30);
            break;
        case 1136087523:
            printf("%s", info31);
            break;
        case 1145579408:
            printf("%s", info32);
            break;
        case 127765566:
            printf("%s", info33);
            break;
        case 1514477478:
            printf("%s", info34);
            break;
        case 434679817:
            printf("%s", info35);
            break;
        case 66223558:
            printf("%s", info36);
            break;
        case 192391726:
            printf("%s", info37);
            break;
        case 1587191762:
            printf("%s", info38);
            break;
        case 987126865:
            printf("%s", info39);
            break;
        case 1673959574:
            printf("%s", info40);
            break;
    }
}

PREV-52 小数第n位

#include 

void q_yushu(int, int, int);

int main(void) {
    int a, b, n;
    scanf("%d%d%d", &a, &b, &n);
    q_yushu(a % b, b, n);
    // printf("%.0lf", a/b );
    return 0;
}

void q_yushu(int a, int b, int n) {
    int i = 0, t_a = a;
    a *= 10;
    while (--n > 0) {
        // printf("%d", a/b );
        a %= b;
        i++;

        if (a == 0) {
            printf("000\n");
            return;
        }
        if (a == t_a) {
            // printf("\n%d\n", i );
            q_yushu(a, b, n % i);
            return;
        }

        a *= 10;
    }

    i = 3;
    while (i--) {
        printf("%d", a / b);
        a %= b;
        a *= 10;
    }
}
import java.util.*;
import java.io.*;
public class Main{
	static List<Integer> num = new ArrayList<Integer>();
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String data[] = br.readLine().split(" ");
		int a = Integer.parseInt(data[0]);
		int b = Integer.parseInt(data[1]);
		int n = Integer.parseInt(data[2]);
		a = a % b;
		int len = cal(a, b);			//去掉循环节的长度
		for(int i = n - 1; i <= n + 1; i++){
			if(i < len) System.out.print(num.get(i));
			else
				System.out.print(num.get((i - len) % (num.size() - len) + len));
		}
	}
	
	private static int cal(int a, int b){
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();		//使用哈希表存储每次出现的值和长度
		while(true){
			map.put(a, num.size());
			a *= 10;
			num.add(a / b);
			if(map.containsKey(a % b))
				return map.get(a % b);
			a %= b;
		}
	}
	
}

PREV-53 分考场


#include 
#include 
int n, min_kes, p[102][102], map[102][102];
void DFS(int x, int kes)
{
	int i;
    if (kes >= min_kes)
        return ;
    if (x > n)
    {
        if (kes < min_kes)
            min_kes = kes;
        return ; 
    }
    for (i = 1; i <= kes; i++)
    {
        int k = 0;
        while (p[i][k] && !map[x][p[i][k]])
            k++;
        if (!p[i][k])
        {
            p[i][k] = x;
            DFS(x + 1, kes);
            p[i][k] = 0;
        }
    }
    p[kes + 1][0] = x;
    DFS(x + 1, kes + 1);
    p[kes + 1][0] = 0;
}
int main()
{
    int t, a, b;
    scanf("%d%d", &n, &t);
    min_kes = n;
    while (t--)
    {
        scanf("%d%d", &a, &b);
        map[a][b] = map[b][a] = 1;
    }
    DFS(1, 0);
    printf("%d\n", min_kes);
    return 0;
}
import java.io.*;
public class Main{
	static int[][] map;
	static int[][] color;
	static int min;
	static int n;
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		int m = Integer.parseInt(br.readLine());
		map = new int[n + 1][n + 1];
		color = new int[n + 1][n + 1];
		for(int i = 0; i < m; i++){
			String data[] = br.readLine().split(" ");
			int v = Integer.parseInt(data[0]);
			int u = Integer.parseInt(data[1]);
			map[v][u] = map[u][v] = 1;
		}
		min = n;
		dfs(1, 0);
		System.out.print(min);
	}
	
	private static void dfs(int x, int room){
		if(room >= min) return;
		if(x > n){
			if(room < min) min = room;
			return;
		}
		for(int i = 1; i <= room; i++){
			int k = 0;
			while(color[i][k] != 0 && map[x][color[i][k]] == 0)
				k++;
			if(color[i][k] == 0){
				color[i][k] = x;
				dfs(x + 1, room);
				color[i][k] = 0;
			}
		}
		color[room + 1][0] = x;
		dfs(x + 1, room + 1);
		color[room + 1][0] = 0;
	}
	
}

PREV-54 合根植物

#include 
#include 
#define ll long long
using namespace std;
int f[1000010];
bool judge[1000010];
int findf(int x) {
    if (x != f[x]) f[x] = findf(f[x]);
    return f[x];
}
int main() {
    int n, m;
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= m * n; i++) f[i] = i;
    int k;
    scanf("%d", &k);
    int a, b;
    while (k--) {
        scanf("%d%d", &a, &b);
        int fa = findf(a), fb = findf(b);
        if (fa != fb) f[fb] = fa;
    }
    int ans = 0;
    for (int i = 1; i <= m * n; i++) {
        int fi = findf(i);
        if (!judge[fi]) {
            judge[fi] = true;
            ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashSet;
import java.util.Set;

public class Main {
	
	public static int[] head;
	

	public static void main(String[] args) throws IOException {
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		in.nextToken();
		int m = (int)in.nval;
		in.nextToken();
		int n = (int)in.nval;
		in.nextToken();
		int k = (int)in.nval;
		head = new int[m*n];
		
		for (int i = 0; i < m*n; i++) {
			head[i] = i;
		}
		
		for (int i = 0; i < k; i++) {
			in.nextToken();
			int a = (int)in.nval-1;
			in.nextToken();
			int b = (int)in.nval-1;
			u(a,b);
		}
		Set<Integer> set = new HashSet<Integer>();
		for (int i = 0; i < m*n; i++) {
			set.add(f(i));
		}
		out.println(set.size());
		out.flush();
		
	}


	private static int f(int i) {
		if (head[i] == i) {
			return i;
		}
		return head[i] = f(head[i]);
	}


	private static void u(int a, int b) {
		if (f(a) == f(b)) {
			return;
		}
		head[f(a)] = f(b);
	}

}

PREV-55 小计算器

#include 
#define ll long long
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
ll base, b, jz = 10, res;
char chs[100000];
bool m, n;
int zh;
inline int func(char c) {
    if (isalpha(c))
        return c - 'A' + 10;
    else
        return c - '0';
}
void num() {
    ll p = 0;
    for (int i = 4; chs[i] != '\0'; i++) {
        p = p * jz + func(chs[i]);
    }
    if (m)
        b = p, n = 1;
    else
        res = base = p, m = 1;
    // cout<
    if (n)
        if (zh == 1)
            res = base = base + b;
        else if (zh == 2)
            res = base = base - b;
        else if (zh == 3)
            res = base = base * b;
        else if (zh == 4)
            res = base = base / b;
        else if (zh == 5)
            res = base = base % b;
}
void equ(ll r) {
    if (r == 0) return;
    equ(r / jz);
    int x = r % jz;
    // cout<
    if (x < 10)
        putchar(x + '0');
    else
        putchar(x - 10 + 'A');
}
int main() {
    // ios::sync_with_stdio(false);
    int t;
    cin >> t;
    getchar();
    while (t--) {
        gets(chs);
        if (chs[0] == 'C') {
            if (chs[1] == 'L')
                m = n = 0;
            else {
                if (!isdigit(chs[8]))
                    jz = chs[7] - '0';
                else
                    jz = (chs[7] - '0') * 10 + chs[8] - '0';
            }
        } else if (chs[0] == 'N')
            num();
        else if (chs[0] == 'A')
            zh = 1;
        else if (chs[0] == 'S')
            zh = 2;
        else if (chs[0] == 'M') {
            if (chs[1] == 'U')
                zh = 3;
            else
                zh = 5;
        } else if (chs[0] == 'D')
            zh = 4;
        else if (chs[0] == 'E') {
            // cout<
            if (res)
                equ(res);
            else
                putchar('0');
            putchar('\n');
        }
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		long sum = 0;
		int jinzhi = 10;
		String yunsuan = "ADD";
		for (int i = 0; i < n; i++) {
			String s = in.readLine();
			if (s.equals("CLEAR")) {
				yunsuan = "ADD";
				sum = 0;
			} else if (s.contains("NUM")) {
				long t = Long.parseLong(s.substring(s.indexOf(" ") + 1), jinzhi);
				if (yunsuan.equals("ADD")) {
					sum += t;
				} else if (yunsuan.equals("SUB")) {
					sum -= t;
				} else if (yunsuan.equals("MUL")) {
					sum *= t;
				} else if (yunsuan.equals("DIV")) {
					sum /= t;
				} else if (yunsuan.equals("MOD")) {
					sum %= t;
				}
			} else if (s.contains("CHANGE")) {
				jinzhi = Integer.parseInt(s.substring(s.indexOf(" ") + 1));
			} else if (s.equals("EQUAL")) {
				System.out.println(Long.toString(sum, jinzhi).toUpperCase());
			} else if (s.equals("ADD")) {
				yunsuan = "ADD";
			} else if (s.equals("SUB")) {
				yunsuan = "SUB";
			} else if (s.equals("MUL")) {
				yunsuan = "MUL";
			} else if (s.equals("DIV")) {
				yunsuan = "DIV";
			} else if (s.equals("MOD")) {
				yunsuan = "MOD";
			}
		}
	}
}


你可能感兴趣的:(蓝桥杯)