先上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. struts2表单批量提交

    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <% ...

  2. 1.2Web API 2中的Action返回值

    本主题描述 ASP.NET Web API 将返回值转换从一个控制器动作到 HTTP 响应消息. 一个 Web API 控制器动作可以返回下列任一操作 ︰ 1.void 2.IHttpActionRe ...

  3. 4. K线基础知识

    1. K线基础知识 K线又叫阴阳线.蜡烛图.最早由日本米市商人发明,后来推广应用到金融行情价格的分析. K线图的构造主要包含四个价格因素:开盘价.收盘价.最高价.最低价 2. K线图例 收盘价高于开盘 ...

  4. jquery 用attr修改src 淡入淡出

    $("#wanwan").animate({ opacity: 'toggle' }, "slow", null, function () { $(" ...

  5. (转)Intent flag 与启动模式的对应关系

    原文地址:http://www.cnblogs.com/ttylinux/p/4069513.html Activity有四种启动模式: 1.standard(标准)    2.singleTop   ...

  6. python 内置&&递归

    lambda 优点: 1:可以简单使用一个脚本来替代我们的函数 2:不用考虑命名的问题 3:简化代码的可读性,不用跳转到def了,省去这样的步骤 内置函数:bif filter:过滤器 map:映射 ...

  7. Python全栈开发【基础一】

    Python全栈开发[第一篇] 本节内容: Python 的种类 Python 的环境 Python 入门(解释器.编码.变量.input输入.if流程控制与缩进.while循环) if流程控制与wh ...

  8. springMVC学习之接受JSON参数

    今天在springmvc使用rest模式异步提交,后台接受json字符.发现好多问题,感觉和spring3.0使用习惯上多少有点区别.因此把4.0的异步提交和方式记录下来. 前台页面代码如下: < ...

  9. Effective Python2 读书笔记1

    Item 2: Follow the PEP 8 Style Guide Naming Naming functions, variables, attributes lowercase_unders ...

  10. .NET LINQ查询语法与方法语法

    LINQ 查询语法与方法语法      通过使用 C# 3.0 中引入的声明性查询语法,介绍性 LINQ 文档中的多数查询都被编写为查询表达式. 但是,.NET 公共语言运行时 (CLR) 本身并不具 ...