当前位置: 首页 > news >正文

蓝桥杯(3.2)

1209. 带分数

import java.io.*;

public class Main
{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    static final int N = 10;

    static int n, cnt;
    static int[] a = new int[N];
    static boolean[] st = new boolean[N];

    public static int calc(int l, int r)
    {
        int res = 0;
        for (int i = l; i <= r; i ++ )
            res = res * 10 + a[i];

        return res;
    }

    public static void dfs(int u)
    {
        if (u > 9)
        {
            for (int i = 1; i <= 7; i ++ )
                for (int j = i + 1; j <= 8; j ++ )
                {
                    int a = calc(1, i), b = calc(i + 1, j), c = calc(j + 1, 9);
                    if (a * c + b == c * n) cnt ++ ;
                }
            return;
        }

        for (int i = 1; i <= 9; i ++ )
            if (!st[i])
            {
                a[u] = i;
                st[i] = true;
                dfs(u + 1);
                a[u] = 0;
                st[i] = false;
            }
    }

    public static void main(String[] args) throws IOException
    {
        n = Integer.parseInt(br.readLine());
		dfs(1);
		pw.print(cnt);
		pw.flush();
		pw.close();
    }
}

717. 简单斐波那契

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int f1 = 0;
		int f2 = 1;
		for(int i=1;i<=n;i++) {
			int f3 = f1 + f2;
			System.out.print(f1+" ");
			f1 = f2;
			f2 = f3;
		}
	}
}

P1255 数楼梯

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    static BigInteger f1 = new BigInteger("1");
    static BigInteger f2 = new BigInteger("2");
    static BigInteger f3 = new BigInteger("0");
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //1 2 3 5 ...
        if (n == 1) {
            System.out.println(f1);
        } else if (n == 2) {
            System.out.println(f2);
        } else {
            for (int i = 3; i <= n; i++) {
                f3 = f1.add(f2);
                f1 = f2;
                f2 = f3;
            }
            System.out.println(f3);
        }
    }
}

P1036 [NOIP2002 普及组] 选数

import java.util.Scanner;

public class Main {
	static int n;
	static int m;
	static final int N = 21;
	static int[] a = new int[N];
	static int[] b = new int[N];
	
	static int ans;
	public static boolean isPrime(int n) {
		for(int i=2;i<n;i++) {
			if(n%i == 0)
				return false;
		}
		return true;
	}
	public static void dfs(int u,int start) {
		if(u > m) {
			int sum = 0;
			for(int i=1;i<=m;i++) {
				sum+=b[i];
			}
			if(isPrime(sum)) {
				ans++;
			}
			return ;
		}
		for(int i=start;i<=n;i++) {
			b[u] = a[i];
			dfs(u+1,i+1);
			b[u] = 0;
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		for(int i=1;i<=n;i++) {
			a[i] = sc.nextInt();
		}
		dfs(1,1);
		System.out.println(ans);
	}
}

P1028 [NOIP2001 普及组] 数的计算

import java.util.Scanner;
public class Main{
	//6
    public static int dfs(int x) {
        if (x == 1) {
            return 1;
        }
        int tot = 1;//long
        for (int i = 1; i <= x / 2; i++) {
            tot += dfs(i);
        }
        return tot;
    }
    public static void main(String[] args){    	
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	System.out.println(dfs(n));
    }
}

在这里插入图片描述
P1464 Function

import java.util.Scanner;

public class Main {
	static int N = 21;
	static long[][][] s = new long[N][N][N];
	public static long w(long a,long b,long c) {
		if(a<=0||b<=0||c<=0)
			return 1;
		if(a>20||b>20||c>20)
			return s[20][20][20];
		if(a<b&&b<c)
			return s[(int)a][(int)b][(int)c-1]+s[(int)a][(int)b-1][(int)c-1]-s[(int)a][(int)b-1][(int)c];
		return s[(int)a-1][(int)b][(int)c]+s[(int)a-1][(int)b-1][(int)c]+s[(int)a-1][(int)b][(int)c-1]-s[(int)a-1][(int)b-1][(int)c-1];
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a,b,c;
		for(int i=0;i<=20;i++)
			for(int j=0;j<=20;j++)
				for(int k=0;k<=20;k++)
					s[i][j][k] = w(i,j,k);
		// 输入a、b、c,计算w(a, b, c)并输出结果
        while (true) {
            a = sc.nextLong();
            b = sc.nextLong();
            c = sc.nextLong();
            if (a == -1 && b == -1 && c == -1)
                return ;
            else
                System.out.println("w(" + a + ", " + b + ", " + c + ") = " + w(a, b, c));
        }
	}
}

相关文章:

  • 前端vue后端go实现大文件分片下载
  • python笔记_程序流程控制
  • python毕设选题 - 大数据商城人流数据分析与可视化 - python 大数据分析
  • kafka消费者重平衡是什么?怎么避免?
  • GPT与MBR:硬盘分区表格式的革新与区别
  • sql基本语法+实验实践
  • 李沐动手学习深度学习——3.2练习
  • leetcode 简单
  • type may not be empty [type-empty]
  • 飞天使-学以致用-devops知识点4-SpringBoot项目CICD实现(实验失败,了解大概流程)
  • 武器大师——操作符详解(下)
  • docker 转为docker-compose(composerize 命令)
  • OpenCV下载与变量配置
  • 苍穹外卖学习 Day10 Day11 Day12
  • Unity(第十四部)光照
  • 《TCP/IP详解 卷一》第7章 防火墙和NAT
  • iOS消息发送流程
  • Vue.js中的$nextTick
  • rsync远程同步
  • django-paramiko远程服务器和文件管理(五)
  • 白酒瓶“神似”北京第一高楼被判侵权,法院一审判赔45万并停售
  • 中法共创《海底两万里》,演员保剑锋重回戏剧舞台演船长
  • 金隅集团:今年拿地将选择核心热门地块,稳健审慎投资
  • 巴基斯坦最近“比较烦”:遣返阿富汗人或致地区局势更加动荡
  • 海上生明月,九天揽星河,2025年“中国航天日”主场活动在上海启动
  • 陕西省烹饪餐饮行业领军人物黄建军逝世,终年53岁