先上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. Vundle的安装

    1.Vundle.vim 安装 https://github.com/VundleVim/Vundle.vim 2.插件安装https://github.com/yangyangwithgnu/use ...

  2. Android多种进度条使用详解

    在这里,总结一下loading进度条的使用简单总结一下. 一.说起进度条,必须说说条形进度条,经常都会使用到嘛,特别是下载文件进度等等,还有像腾讯QQ安装进度条一样,有个进度总给人良好的用户体验. 先 ...

  3. resize

    resize 属性规定是否可由用户调整元素尺寸. resize: none|both|horizontal|vertical; none:用户无法调整元素的尺寸.      比较常用 both:用户可 ...

  4. NOIp2014 解题报告

    有史以来第一届面向社会征题的NOIp结束了.最开始以为面向社会征题会很难,但是这是我参加的最水的一次NOIp了. 由于停了两月的课,所以现在正在补文化科目就没时间打代码了.所以所有的题目就均不给出代码 ...

  5. Sublime Text永久设置使用4个空格缩进

    Sublime Text是一款轻量高效的代码编辑器,官网地址是:http://www.sublimetext.com/,默认情况下sublime是使用tab进行缩进,如果手动敲空格是比较麻烦的,并且很 ...

  6. Word 2010 发布博文测试

    新建"博客文章" 点击"文件-> 新建 -> 博客文章 -> 创建": 新窗口的编辑区和不同模式的word有所不同,如果你还没有设置博客账号( ...

  7. Java 自动装箱、拆箱机制及部分源码分析

    Integer i = 10; //装箱,反编译后发现调用Integer.valueOf(int i) int t = i; //拆箱,反编译后发现调用i.intValue() public clas ...

  8. iOS开发UI篇—核心动画(关键帧动画)

    转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...

  9. c语言中遇到“警告: the `gets' function is dangerous and should not be used.”的解决办法

    写于2016年12月1日. 在用c的库函数gets(str)时,编译出现该提示.原因在于linux下gcc不支持gets命令,要换成fgets(arr,size,stdin).

  10. Build2016上值得一看的大数据相关Session

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:Build2016开完很久了,现在才来回顾下,就说说那些和大数据相关的Session, ...