Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)
先上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的阶乘)的更多相关文章
- 日期类&&包装类&&System类&&Math类&&Arrays数组类&&大数据类
day 07 日期类 Date 构造函数 Date():返还当前日期. Date(long date):返还指定日期 date:时间戳--->距离1970年1月1日 零时的毫秒数 常用方法 日期 ...
- C++ BigInteger 大整数类模板(转)
#include <deque> #include <vector> #include <iostream> #include <string> #in ...
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
- 代码的坏味道(16)——纯稚的数据类(Data Class)
坏味道--纯稚的数据类(Data Class) 特征 纯稚的数据类(Data Class) 指的是只包含字段和访问它们的getter和setter函数的类.这些仅仅是供其他类使用的数据容器.这些类不包 ...
- Python基础-random模块及随机生成11位手机号
import random # print(random.random()) # 随机浮点数,默认取0-1,不能指定范围# print(random.randint(1, 20)) # 随机整数,顾头 ...
- java常用类详细介绍及总结:字符串相关类、日期时间API、比较器接口、System、Math、BigInteger与BigDecimal
一.字符串相关的类 1.String及常用方法 1.1 String的特性 String:字符串,使用一对""引起来表示. String声明为final的,不可被继承 String ...
- python的内置模块random随机模块方法详解以及使用案例(五位数随机验证码的实现)
1.random(self): Get the next random number in the range [0.0, 1.0) 取0到1直接的随机浮点数 import random print( ...
- python模块知识二 random -- 随机模块、序列化 、os模块、sys -- 系统模块
4.random -- 随机模块 a-z:97 ~ 122 A-Z :65 ~ 90 import random #浮点数 print(random.random())#0~1,不可指定 print( ...
- 模块 random 随机
random 随机数 0 导入 >>> import random 1 random 随机小数 random.random() # 大于0且小于1之间的小数 0.7664338663 ...
随机推荐
- C和指针 第三章 变量的储存类型 auto、static、register以及static关键词
变量的储存类型决定标量何时创建,何时销毁以及他的值保持多久.有三个地方可以储存变量: 普通内存static 运行时堆栈auto 硬件寄存器register 变量的缺省储存类型取决于它的声明位置: 静态 ...
- Linux学习之一--VI编辑器的基本使用
vi编辑器是Linux系统下标准的编辑器.而且不逊色于其他任何最新的编辑器.可是会用的有多少呢.下面介绍一下vi编辑器的简单用法和部分命令.让你在Linux系统中畅行无阻. 基本上vi可以分为三种状态 ...
- Windows10更新提示语言不同不能保留程序和设置
打开注册表编辑器(Win+R,输入regedit)定位到: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Nls\Language 在右边窗口中拉到最 ...
- MySQL分库分表总结
单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...
- java 深入技术二(Collection)
1. java集合 存储和管理多个java对象 包括很多java类和接口 Collection List Set ArrayList Lin ...
- linux 使用 nvidia 的 gpu
第一种方法: [Wizard@Wizard ~]$ nvidia-detect kmod-nvidiaOptimus hardware detected: An Intel display contr ...
- come on,逆战
腾讯游戏 琳琅天上 逆战 ...
- Logging vs NoLogging
You Asked My Prod environments is like this. Three Node RAC, Active Data guard enabled. There is a p ...
- Android客户端性能优化(魅族资深工程师毫无保留奉献)
本文由魅族科技有限公司资深Android开发工程师degao(嵌入式企鹅圈原创团队成员)撰写,是degao在嵌入式企鹅圈发表的第一篇原创文章,毫无保留地总结分享其在领导魅族多个项目开发中的Androi ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...