Java算法题挑战 Second day

算法题打卡第二天:6 - 10题

第六题

  1. 问题描述:
    对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

    00000

    00001

    00010

    00011

    00100

    请按从小到大的顺序输出这32种01串。

  2. 代码块:

public class Main {
    public static void main(String[] args)
    {
        for(int i = 0;i<32;i++) {
            int n = i|256;
            String sum = Integer.toBinaryString(n);
            String num = sum.substring(sum.length()-5);
            System.out.println(num);
        }
    }
}
  1. 个人思路方法:
    这题由于没有唯一的解,通过toBinaryString() 方法将n值转换为二进制表示的字符串,然后通过subString()方法去进行格式输出。

第七题

  1. 问题描述:
    求两个大的正整数相减的差。

  2. 代码块:

import java.util.*;
import java.math.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        BigInteger m = in.nextBigInteger();
        BigInteger n = in.nextBigInteger();
        BigInteger sum = m.subtract(n);
        System.out.print(sum);
    }
}
  1. 个人思路方法:
    这题主要考察的是对类型的使用,而在Java中的java.math包中有求大整数的类包,直接拿过来调用即可,我在我的Java专栏中对这个也有具体讲解。

第八题

  1. 问题描述:
    题目描述
  2. 代码块:
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        double sum = 0.0;
        double p=1 , q=2;
        double temp=0;
        for(int i=1;i<=n;i++)
        {     
            sum += q*1.0/p*1.0;
            //通过中间量temp进行变量赋值计算,而不能直接赋值,因为直接赋值会使其中的一个变量值发生改变
            temp = q;
            q=p+q;
            p=temp;
        }
        System.out.printf("%.4f",sum);
    }
}
  1. 个人思路方法:
    这题可以直接通过题目中所给的公式去遍历求解,但是这题对精度有一定的要求,这也是这题要考察的重点。

第九题

  1. 问题描述:
    请统计某个给定范围 [L, R][L,R] 的所有整数中,数字 22 出现的次数。
    比如给定范围 [2, 22][2,22],数字 22 在数 22 中出现了 11 次,在数 1212 中出现 11 次,在数 2020 中出现 11 次,在数 2121 中出现 11 次,在数 2222 中出现 22 次,所以数字 22 在该范围内一共出现了 66 次。

输入格式
输入共 1行,为两个正整数 L 和 R,之间用一个空格隔开。
1\le L \le R \le 10001≤L≤R≤1000。

输出格式
输出共 1 行,表示数字 2 出现的次数。

  1. 代码块:
import java.util.*;
public class Main{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int i , j , count=0;
        for(i=m; i<=n; i++)
        {
            for(j=i;j>0;) 
            {
                if(j%10==2)	//若个位满足,则累加
                    count +=1;
                j/=10; //分离每个数的个数位
            }
        }
        System.out.print(count);
    }
}
  1. 个人思路方法:
    从这题题目中很容易看出,给定一个范围[L,R],求出在此范围中出现的2个数,先做个遍历范围,然后逐步去求每个数的个位和十位,并与2去比对。

第十题

  1. 问题描述:
    利用递归方法求n!

  2. 代码块:

import java.util.Scanner;
public class digui {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while (in.hasNext())
        {
            int n = in.nextInt();
            long num = (long)func(n);
            System.out.println(num);
        }
    }
    public static int func(int n)
    {
        if(n==0 || n==1)
        {
            return n;
        }else
        {
            return n*func(n-1);
        }
    }
}
  1. 个人思路方法:
    这题我的思路就是根据递归公式去求:num = n*func(n-1)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值