先上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. C和指针 第十三章 习题

    1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...

  2. liunx学习(一):linux下目录操作大全

    Linux C函数之文件及目录函数(全):http://blog.sina.com.cn/s/blog_695e489c01013ldd.html linux目录操作发:http://www.cnbl ...

  3. 如何在github下载开源项目到本地(Coding iOS 客户端为例)

    一.前言 以 Coding iOS 客户端 为例讲解如何在github下载开源项目到本地 github地址:https://github.com/Coding/Coding-iOS 二.分析 根据项目 ...

  4. Ubuntu下三个实用的录屏软件

    Ubuntu下三个实用的录屏软件 Kazam 优点: 易安装 可选择区域录制,也可全屏录制 有录屏和截图功能 安装: sudo apt-get install kazam 展示: Simple Scr ...

  5. 如何让页眉随章节的不同而变化(Word 2010)

    在一般情况下,我们将页眉设置完成后,所有章节的页眉都是一样的. 但在某些时候,我们需要让不同的章节拥有各自不同的页眉.那么该如何设置呢? 1. 我们以下图的文件为例,该文件一共包括4个实验,暂未设置页 ...

  6. Swift - 界面的跳转模式

    iOS开发中界面跳转有两种方式,上下跳转和左右跳转. 上下跳转_TO: let secondViewController = SecondViewController() self.presentVi ...

  7. 《BuildingMachineLearningSystemsWithPython》学习笔记

    BuildingMachineLearningSystemsWithPython Python机器学习入门 数据分析五个步骤 读取和清洗数据 探索和理解输入数据[我的理解是业务理解] 分析如何将算法应 ...

  8. 标准模板库(STL)的一个 bug

    今天敲代码的时候遇到 STL 的一个 bug,与 C++ 的类中的 const 成员变量有关.什么,明明提供了默认的构造函数和复制构造函数,竟然还要类提供赋值运算符重载.怎么会这样? 测试代码 Tes ...

  9. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  10. [Unity3D]NGUI用Sprite动画和屏幕自适应做游戏开始场景

    我们在玩任何一款手游产品时,都是先上来个logo界面,游戏欢迎界面等,这就意味着我们要做一款游戏需要多个场景,场景之间来回切换实现游戏逻辑,unity也不例外,所以从本篇开始将会介绍如何搭建多个场景, ...