先上Java Web图

为了简化叙述,只写Java代码,然后控制台输出

使用【Random类】取得随机数

import java.util.Random;

public class Fir {
public static void main(String[] args) {
//输出
int [] a=creatnumber_11x5();
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
//11选5 也可以实现36选7
public static int[] creatnumber_11x5() {
Random rd=new Random();
//11选5
int MEM=5,TOT=12;
int [] number=new int[MEM];
for(int i=0;i<MEM;i++){
boolean flag=true;
while(flag){
int a=rd.nextInt(TOT);
if(isRe(number,a)){
number[i]=a;
flag=false;
}
}
}
//冒泡排序
int temp=0;
for(int i=1;i<number.length;i++){
for(int j=0;j<number.length-i;j++){
if(number[j]>number[j+1]){
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
return number;
}
/**
* 判断是否重复
* @param arr
* @param x
* @return
*/
public static boolean isRe(int[] arr,int x){
for(int i=0;i<arr.length;i++){
if(x==arr[i]){
return false;
}
}
return true;
}
}

BigInteger大数据了  

构造函数public BigInteger(String str)

也就是接受字符串类型

示例:

		BigInteger b1=new BigInteger("4953493636435253464646");
BigInteger b2=new BigInteger("5685639769376446");
System.out.println(b1.add(b2));
System.out.println(b1.subtract(b2));
System.out.println(b1.multiply(b2));
System.out.println(b1.divide(b2));

  

除法小数位被被截断
divideAndRemainder()此函数处理小数
返回一个数组,索引0存储整数,索引1存储小数

public class Te {
public static void main(String[] args) {
BigInteger b1=new BigInteger("4953493636435253464646");
BigInteger b2=new BigInteger("5685639769376446");
System.out.println(b1.add(b2));
System.out.println(b1.subtract(b2));
System.out.println(b1.multiply(b2));
System.out.println(b1.divide(b2)); //除法小数位被被截断
BigInteger[] b=b1.divideAndRemainder(b2);//此函数处理小数
//返回一个数组,索引0存储整数,索引1存储小数
System.out.println(b[0]);
System.out.println(b[1]);
}
}

  来看华为的一道面试题:求1000的阶乘。

此题设计的非常能区分人

如果只是考虑一个for循环连续相乘,出错还找不到原因,那基本水平可以测试出来。

1、此题起码需要了解底层一些知识:

存储的结果用int保存?long保存?long64位最多保存的数也有范围,超出了怎么办?

2、有数学的观察力:1000看似小,但是经过连乘这个数大概是多少位?

3、如何保存计算结果。

此题的一种解法是,用int数组来保存每一位整数,然后每10进位

用Java中的BigInteger类工具可以轻松解决此问题

import java.math.BigInteger;

public class TT {
public static void main(String[] args) { //求1000的阶乘
BigInteger bigIn=new BigInteger("1");
for(int i=1;i<1000;i++){
BigInteger currentBigInte=new BigInteger(""+i);
bigIn=bigIn.multiply(currentBigInte);
}
System.out.println(bigIn);
}
}

为了更好观察,切刀cmd下运行  

运行如图:

Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)的更多相关文章

  1. 日期类&&包装类&&System类&&Math类&&Arrays数组类&&大数据类

    day 07 日期类 Date 构造函数 Date():返还当前日期. Date(long date):返还指定日期 date:时间戳--->距离1970年1月1日 零时的毫秒数 常用方法 日期 ...

  2. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  3. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  4. 代码的坏味道(16)——纯稚的数据类(Data Class)

    坏味道--纯稚的数据类(Data Class) 特征 纯稚的数据类(Data Class) 指的是只包含字段和访问它们的getter和setter函数的类.这些仅仅是供其他类使用的数据容器.这些类不包 ...

  5. Python基础-random模块及随机生成11位手机号

    import random # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围# print(random.randint(1, 20)) # 随机整数,顾头 ...

  6. java常用类详细介绍及总结:字符串相关类、日期时间API、比较器接口、System、Math、BigInteger与BigDecimal

    一.字符串相关的类 1.String及常用方法 1.1 String的特性 String:字符串,使用一对""引起来表示. String声明为final的,不可被继承 String ...

  7. python的内置模块random随机模块方法详解以及使用案例(五位数随机验证码的实现)

    1.random(self): Get the next random number in the range [0.0, 1.0) 取0到1直接的随机浮点数 import random print( ...

  8. python模块知识二 random -- 随机模块、序列化 、os模块、sys -- 系统模块

    4.random -- 随机模块 a-z:97 ~ 122 A-Z :65 ~ 90 import random #浮点数 print(random.random())#0~1,不可指定 print( ...

  9. 模块 random 随机

    random 随机数 0 导入 >>> import random 1 random 随机小数 random.random() # 大于0且小于1之间的小数 0.7664338663 ...

随机推荐

  1. html用户注册界面

    html用户注册界面   先上一张简约的界面的效果图 这里是style里面的内容 <style> input[type]{ border: 1px solid darkorange; ba ...

  2. 在虚拟机下安装hadoop集成环境(centos7+hadoop-2.6.4+jdk-7u79)

    [1]64为win7系统,用virtualbox建立linux虚拟机时,为什么没有64位的选项? 百度 [2]在virtualbox上安装centos7 [3]VirtualBox虚拟机网络环境解析和 ...

  3. win7无法保存打印机设置(错误0x000006d9)解决方法

    解决win7打印机共享出现‘无法保存打印机设置’操作无法完成(错误0x000006d9),接下来与大家分享下解决方法, 找到windows firewall服务,启用即可 ============== ...

  4. python DBUtils.PooledDB 中 maxcached 和 maxconnections

    PooledDB 有这么几个参数 mincached : the initial number of idle connections in the pool (the default of 0 me ...

  5. C语言指针类型

    1:只要是指针类型,不管是几级指针[带几个*],其宽度都是4字节 2:任何数据类型[包括自己定义的结构体]前面都能加*号,表示该数据类型的一个指针 3:由于是386处理器,其数据处理的宽度都是四个字节 ...

  6. 【转】Caffe初试(四)数据层及参数

    要运行caffe,需要先创建一个模型(model),如比较常用的Lenet,Alex等,而一个模型由多个层(layer)构成,每一层又由许多参数组成.所有的参数都定义在caffe.proto这个文件中 ...

  7. char 型二维数组

    char FutureFunc[][16] = {"XMA","ZIG","PEAK","PEAKBARS"," ...

  8. 第二章 编写与设置Servlet

    2.1 第一个Servlet package cc.openhome; import javax.servlet.ServletException; import javax.servlet.http ...

  9. LeetCode之404. Sum of Left Leaves

    ------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...

  10. C# 如何用多个字符串来切分字符串并去除空格

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...