蓝桥杯冲刺31天day8

1、

分数

根据题意的规律即可得。

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int z = 1;
        int m = 1;
        for(int i=1;i<20;i++){
          m*=2;//分母每次都是2倍变化
          z+=m;//从1/1+1/2可以发现其等于3/2,而分子是根据前2项和得出
        }
        System.out.printf("%d/%d",z,m);
    }
}

 2、回文日期

思路尽在注释中,一定要判断合法性,开始忘记了

import java.util.Scanner;

public class 回文日期{
    static int h1= 0;
    static int h2 = 0;
static boolean isYear(int n) {
   int a[]= {31,28,31,30,31,30,31,31,30,31,30,31};
   int y=n/10000; //年
   int m=n/100%100;//月
   int d=n%100; //日
       if ((y%100!=0&&y%4==0)||y%400==0) {
    	   a[1]=29; //闰年2月为29天
    	}
        for (int i=0;i<12;i++) { //在合理范围内返回true
    	   if ((i+1)==m&&d>=1&&d<=a[i]) return true;
    	}
    	  return false;
}
 static boolean isHw1(String s) {  //判断是否为回文数
  StringBuilder ss=new StringBuilder(s);
  if (s.equals(ss.reverse().toString())) {
   return true;
  }
  return false;
 }
 static boolean isHw2(String str) {//判断ABABBABA型回文数
	 char[] ch = str.toCharArray();
     char a = ch[0], b = ch[1], c = ch[2], d = ch[3],e = ch[4],f = ch[5],g = ch[6],h = ch[7];
     if(a==c&&c==f&&f==h&&b==d&&d==e&&e==g) {
        return  true;
         
     }
     return false;
}
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  int d1=in.nextInt();
  for (int i=d1+1;;i++) {//从样例日期接下来的第一个日期进行判断
	  if (isYear(i)) {
    if (isHw1(Integer.toString(i))) {
    	h1 = i;
    	break;
    }
	  }
  }
    for (int j=d1+1;;j++) {//从样例日期接下来的第一个日期进行判断
    	if(isYear(j)) {
        if (isHw2(Integer.toString(j))) {
        	h2 = j;
        	break;
        }
    	}
    
   }
  System.out.println(h1);
  System.out.println(h2);
 }

}

 4、斐波那契

题目描述

斐波那契数列大家都非常熟悉。它的定义是:

f(x)=1⋯(x=1,2)

f(x)=f(x−1)+f(x−2)⋯(x>2)

对于给定的整数 n 和 m,我们希望求出:

f(1)+f(2)+⋯+f(n) 的值。但这个值可能非常大,所以我们把它对 f(m) 取模。

但这个数字依然很大,所以需要再对 p 求模。

输入描述

输入描述

输入为一行用空格分开的整数 n,m,p (0<n,m,p<10^18)。

输出描述

输出为 1 个整数。

输入输出样例

示例

输入

2 3 5

输出

0

 思路:数据庞大,一直在思考怎么用BigInteger解决

开始因为可以直接通过BigInteger进行传递,但是不行,传递时不能用BigInteger类型之间运算得到的数据进行传递(个人理解,因为一直报错都是这部分)所以改为[ ]里用int类型表示即可,让BigInteger的数组进行递归传回。

第一次报错代码:

import java.util.Scanner;
import java.math.*;
public class fbnq {
 static BigInteger zero = new BigInteger("0");
 static BigInteger one = new BigInteger("1");
 static BigInteger two = new BigInteger("2");
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
       // String a = scan.next(),b=scan.next(),c=scan.next();
        String a = scan.next();
        BigInteger n = new BigInteger(a);
        BigInteger m = scan.nextBigInteger();
        BigInteger p = scan.nextBigInteger();
        BigInteger[]q = new BigInteger[Integer.valueOf(a)+1];
        for(int i = 1;i<=Integer.valueOf(a);i++) {
         q[i] = new BigInteger(String.valueOf(i));
        }
        BigInteger res = new BigInteger("0");
        for(BigInteger i:q) {
         res.add(fb(i));
        }
        
        res = res.mod(fb(m)).mod(p);
        
        System.out.println(res);
    }
    static BigInteger fb(BigInteger w) {
     if(w==zero) {
      return zero;
     }
     if(w==one) {
      return one;
     }
     return fb(w.subtract(one)).add(fb(w.subtract(two)));
    }
}

修改后代码(不过只能过4/7):个人思路理解写在注释了。

import java.math.*;
import java.util.Scanner;
public class 斐波那契 {
	static BigInteger zero = new BigInteger("0");//由于不能直接进行赋值,所以必须定义BigInteger的0与1
	static BigInteger one = new BigInteger("1");
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(),m=scan.nextInt(),p=scan.nextInt();//先把nmp存到int类型的变量里,方便后边用
         BigInteger fbi = new BigInteger("0");//定义需要保存到BigInteger类型里的数据
         BigInteger fbsum = new BigInteger("0");//
         BigInteger fbm = new BigInteger(String.valueOf(m));
         BigInteger fbp = new BigInteger(String.valueOf(p));
        for (int i = 1; i <= n; i++) {
            fbi = fb(i);//每一个数的斐波那契
            fbsum  = fbsum.add(fbi);//斐波那契和
        }
         fbm = fb(m);
        fbsum = fbsum.mod(fbm).mod(fbp);//题目中需要对m进行斐波那契,p不需要

        System.out.println(fbsum);

    }

    public static BigInteger fb(int n) {
    	BigInteger []fbn = new BigInteger[n+1];//定义BigInteger类型的数组存数据
    	fbn[0]=zero;//定义前两项
    	fbn[1]=one;
        for (int i = 2; i < n + 1; i++) {
            fbn[i] = fbn[i - 1].add(fbn[i - 2]);//递归操作,注意BigInteger的运算方法
        }
        return fbn[n];//传回
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值