[Day11]Bangla Numbers

上一篇介绍了Cubes,主要是考回圈的运用及判断,仔细思考之後就会发现不是那麽困难了。

今天讲解的题目是 Bangla Numbers
先附上程序码:

import java.util.;
import java.math.
;
import static java.lang.System.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger b=new BigInteger("10000000");
BigInteger c=new BigInteger("100000");
BigInteger d=new BigInteger("1000");
BigInteger e=new BigInteger("100");
BigInteger A=new BigInteger("0");
String[] t={"kuti","lakh","hajar","shata",""};
int res=1,B=1,C=1,Y=7,V=1;
while(sc.hasNextBigInteger()){
int arr[]=new int[5];
int ar[]=new int[5];
BigInteger a=sc.nextBigInteger();
if(a.compareTo(BigInteger.ZERO)==0){
System.out.println(" "+B+"."+" "+a);
B++;}
else {
res=a.compareTo(b);
if(res !=-1){
A=a.divide(b);
a=a.mod(b);
arr[0]=A.intValue();
if(arr[0]>10000000){
C=0;
ar[0]=arr[0]/10000000;
arr[0]=arr[0] % 10000000;
Y=0;}
if(arr[0]>100000){
C=0;
ar[1]=arr[0]/100000;
arr[0]=arr[0] % 100000;
Y=1;}
if(arr[0]>1000){
C=0;
ar[2]=arr[0]/1000;
arr[0]=arr[0] % 1000;
Y=2;;}
if(arr[0]>=100){
C=0;
ar[3]=arr[0]/100;
arr[0]=arr[0] % 100;;
Y=3;}
}
res=a.compareTo(c);
if(res !=-1){
A=a.divide(c);
a=a.mod(c);
arr[1]=A.intValue();
}
res=a.compareTo(d);
if(res !=-1){
A=a.divide(d);
a=a.mod(d);
arr[2]=A.intValue();
}
res=a.compareTo(e);
if(res !=-1){
A=a.divide(e);
arr[3]=A.intValue();
a=a.mod(e);
}
arr[4]=a.intValue();
if(C==1){
if(B>9) System.out.print(" "+B+".");
else System.out.print(" "+B+".");
for(int i=0;i<5;i++){
if(arr[i]!=0){
if (i!=4)
System.out.print(" "+arr[i]+" "+t[i]);
else System.out.print(" "+arr[i]);}}}
else{
if(B>9) System.out.print(" "+B+".");
else System.out.print(" "+B+".");
for(int i=0;i<4;i++){
if(ar[i]!=0)
if(arr[0]==0){
if(i!=Y) System.out.print(" "+ar[i]+" "+t[i]);
else System.out.print(" "+ar[i]+" "+t[i]+" "+t[0]);}
else System.out.print(" "+ar[i]+" "+t[i]);}
for(int i=0;i<5;i++){
if(arr[i]!=0){
if (i!=4)
System.out.print(" "+arr[i]+" "+t[i]);
else System.out.print(" "+arr[i]);}}
}
System.out.println();
B++;
C=1;}
}

}
};

今天这题因为题目需要的数字太大,因此有用到BigInteger,
BigInteger用法网址:https://www.itread01.com/content/1545195543.html
https://www.itread01.com/content/1546080151.html

首先要先在最上面新增import java.math.*;。
首先先设定题目要求的条件以及文字,接下来依照判断大小来输出数字以及文字,由於BigInteger的判断只有1、0、-1,因此在判断时要先把判断的程序码写出来,在使用if来判断,之後再使用阵列把数字记录下来之後再使用阵列输出。
这题要使用字串来写也可以,只需要把数字以字串记录下来,在判断有几个字元,接下来也是写判断式就可以了,但这种方法可以直接纪录数字,在某些时候会轻松许多,所以这一次打算使用BigInteger来写这一题,这样以後遇到大的数字时可以不用想怎麽转成字串了。
今天的讲解就到这里。
明天也是新的题目,会介绍Die Game,继续加油!


<<:  Day11 Lab 1 - 简单的Object storage系统

>>:  换了一个框架

菜鸡的机器学习入门

Day1 简介 前言 今天先简单介绍为什麽要写这个,其实训练一个模型不难,许多相关的教学都讲得相当清...

09. E2E Test x Browser Test x Cypress

cypress 安装步骤 step 1. 安装 npm install cypress --save...

Day17 PHP的常用函数-2:数组

数组 array(): 生成一个数组 range(): 创建并返回一个包含指定范围的元素的数组 co...

参加 VMware 1V0-21.20 认证考试以获得成功的职业生涯

您是 VMware 认证技术助理 - 数据中心虚拟化考试的有抱负的候选人之一吗?然後你就中了头奖!多...

DAY20-EXCEL统计分析:单因子变异数分析实例

今天有一家饮料店想比较不同县市的分店销售量,想确认不同县市每天的销售量是否有明显的差异,於是随机抽选...