栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java算法题挑战 Second day

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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,之间用一个空格隔开。
1le 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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号