java string做除法_如果用java來實現傳統方式的除法,用String來保存結果,想精確多少位都行,那改怎么做?...

我會加分的,提個思路都行,目前做了個乘法和加法,但是現在對除法沒有什么思路。以下是我編寫的功能:publicclassCalculator{publicstaticStringmulti(Strings1,Strings2){if(s1==nu...

我會加分的,提個思路都行,目前做了個乘法和加法,但是現在對除法沒有什么思路。以下是我編寫的功能:

public class Calculator {

public static String multi(String s1,String s2){

if(s1 == null || s2 == null){

return "0";

}

char c1[] = s1.toCharArray();

char c2[] = s2.toCharArray();

for(int i=0;i

if(i==0){

if(c1[i] !='-' && c1[i] != '+' && !(c1[i]>='0' && c1[i]<='9')){

throw new RuntimeException("你要計算的數字不合法!");

}

}else{

if(!(c1[i]>='0' && c1[i]<='9')){

throw new RuntimeException("你要計算的數字不合法!");

}

}

}

for(int i=0;i

if(i==0){

if(c2[i] !='-' && c2[i] != '+' && !(c2[i]>='0' && c2[i]<='9')){

throw new RuntimeException("你要計算的數字不合法!");

}

}else{

if(!(c2[i]>='0' && c2[i]<='9')){

throw new RuntimeException("你要計算的數字不合法!");

}

}

}

String sign1 = "";

String sign2 = "";

if(s1.charAt(0)>='0' && s1.charAt(0)<='9'){

sign1="+";

}else{

sign1 = s1.substring(0,1);

s1 = s1.substring(1);

}

if(s2.charAt(0)>='0' && s2.charAt(0)<='9'){

sign2 = "+";

}else{

sign2 = s2.substring(0,1);

s2 = s2.substring(1);

}

StringBuffer buffer = new StringBuffer();

Long[][] number = new Long[s2.length()][s1.length()+s2.length()];

for(int i=0;i

int t = Integer.parseInt(s2.substring(s2.length()-(i+1),s2.length()-i));

int temp = 0;

int count = 0;

for(int j=number[i].length-1;j>=0;j--){

if(count

int cols = Integer.parseInt(s1.substring(s1.length()-(count+1),s1.length()-count));

number[i][j-i] = new Long( ((cols*t)%10)+temp);

temp = cols*t/10;

}else if(temp !=0){

number[i][j-i] = new Long(temp);

temp = 0;

}

count++;

}

}

int temp = 0;

for(int i=(s1.length()+s2.length())-1;i>=0;i--){

int t = 0;

for(int j=0;j

if(number[j][i] != null){

t+=number[j][i];

}

}

buffer.append(""+(t%10+temp));

temp = t/10;

}

buffer = buffer.reverse();

String result = buffer.toString();

while(result.charAt(0) == '0' && result.length() !=1){

result = result.substring(1);

}

if(!sign1.equals(sign2)){

result = "-"+result;

}

return result;

}

public static String add(String s1,String s2){

//因為字數超過限制,所以沒有上傳

return "還沒有實現!";

}

public static void main(String[] args){

System.out.println(Calculator.add("-755334311424342","-9222012"));

BigInteger big1 = new BigInteger("-755334311424342");

BigInteger big2 = new BigInteger("-9222012");

System.out.println(big1.add(big2)); System.out.println(Calculator.multi("-23423403504534534563","23222222222222222222222222222222222"));

}

}

-----------result-------------

-755334320646354

-755334320646354

-543943470131721017861316196131010101010101010101010101019518951801921211121215658986

展開

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542676.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542676.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542676.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c語言數組的聲明和初始化_C聲明和初始化能力問題和解答

c語言數組的聲明和初始化This section contains aptitude questions and answers on C language Declarations and Initialization. 本節包含有關C語言聲明和初始化的適切性問題和解答。 1) What will be the output of following program ? int main(){int m10;int xprintf(…

python2和python3的默認編碼_python2和python3哪個版本新

Python2 還是 Python3 &#xff1f; py2.7是2.x系列的最后一個版本&#xff0c;已經停止開發&#xff0c;不再增加新功能。2020年終止支持。 所有的最新的標準庫的更新改進&#xff0c;只會在3.x的版本里出現。Python3.0在2008年就發布出來&#xff0c;而2.7作為2.X的最終版本并…

html-css樣式表

一、CSS&#xff1a;Cascading Style Sheet—層疊樣式表&#xff0c;其作用是美化HTML網頁。 樣式表分類&#xff1a;內聯樣式表、內嵌樣式表、外部樣式表 1、內聯樣式表 和HTML聯合顯示&#xff0c;控制精確&#xff0c;但是可重用性差&#xff0c;冗余多。 例如&#xff1a;&…

java 棧 先進后出_棧先進后出,堆先進先出

1.棧(stack)與堆(heap)都是Java用來在Ram中存放數據的地方。與C不同&#xff0c;Java自動管理棧和堆&#xff0c;程序員不能直接地設置棧或堆。2.棧的優勢是&#xff0c;存取速度比堆要快&#xff0c;僅次于直接位于CPU中的寄存器。但缺點是&#xff0c;存在棧中的數據大小與生…

c#給定二維數組按升序排序_在數組中按升序對數字進行排序| 8086微處理器

c#給定二維數組按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 問題&#xf…

使用python套用excel模板_Python自動化辦公Excel-從表中批量復制粘貼數據到新表

1、模塊安裝 1&#xff09;cmd模式下&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2&#xff09;如果有安裝Pycharm&#xff0c;則在程序中操作如下&#xff1a; 菜單欄&…

在HubSpot是如何應對Fat JAR困境的

在七月底&#xff0c;Spring Boot和Dropwizard分別發布了1.4和1.0版本&#xff0c;它們都是基于Fat JAR的。隨著人們更多地采用這些框架和微服務架構&#xff0c;Fat JAR成為了通用的部署機制。\\Fat JAR技術會將Java應用的所有依賴打包到一個bundle之中&#xff0c;便于執行&a…

給定數字的b+樹創建_在C ++中找到給定數字中的兩個的下一個和上一個冪

給定數字的b樹創建Problem statement: 問題陳述&#xff1a; Find Next and previous power of two of a given number 查找給定數字中兩個的下一個和上一個冪 Next power of two 下一個二的冪 Example(1):input: 22output: 32 ( as 32 is 2^5)Example(2):input: 54output…

java 字節數組作用_這段java代碼中字節數組b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自動生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩潰log_過年回家,還怕搶不到票?程序員教你如何搶票

2019年接近尾聲&#xff0c;距離春節回家的日子越來越近&#xff0c;26日起&#xff0c;2020年除夕火車票正式開售&#xff0c;搶票大戰也進入白熱化階段。是否為某搶票 App 加速而煩惱&#xff0c;是否為車票“秒光而煩惱”。別慌&#xff0c;作為連“對象”都是 new 出來的程…

獲取列表中包含的元素數 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 給定一個列表&#xff0c;我們必須使用List.Count屬性計算其元素總數 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 虧數(Deficient number)

數論中&#xff0c;若一個正整數除了本身之外所有因子之和比此數自身小&#xff0c;則稱此數為虧數。虧數&#xff08;Deficient number&#xff09;也稱為缺數&#xff0c;參見百度百科_虧數&#xff0c;或參見維基百科的Deficient number。虧數在OEIS中的數列號為A005100。 問…

hashmap轉紅黑樹的閾值為8_面試必考的 HashMap,這篇總結到位了

點擊藍色“JavaKeeper”關注我喲加個“星標”&#xff0c;一起成長&#xff0c;做牛逼閃閃的技術人1 概述HashMap是基于哈希表實現的,每一個元素是一個key-value對,其內部通過單鏈表解決沖突問題,容量不足(超過了閥值)時,同樣會自動增長.HashMap是非線程安全的,只適用于單線程環…

linux用戶組管理命令_Linux用戶和組命令能力問題和解答

linux用戶組管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本節包含有關Linux用戶和組命令的 Aptitude問答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019獨角獸企業重金招聘Python工程師標準>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二個索引_MySQL高級第二章——索引優化分析

一、SQL性能下降原因1.等待時間長&#xff1f;執行時間長&#xff1f;可能原因&#xff1a;查詢語句寫的不行索引失效(單值索引、復合索引)CREATE INDEX index_user_name ON user(name);(底層做了一個排序)CREATE INDEX index_user_nameEmail ON user(name,email);查詢關聯join…

遞歸反轉鏈表改變原鏈表嗎_在不使用遞歸的情況下找到鏈表的長度

遞歸反轉鏈表改變原鏈表嗎Solution: 解&#xff1a; Algorithm to find length 查找長度的算法 Input: 輸入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head. 一個單鏈表 &#xff0c;其第一個節點的地址存儲在指針(例…

西瓜仿站高手v1.08官方正式版

2019獨角獸企業重金招聘Python工程師標準>>> 西瓜仿站高手是一款綠色好用的由追風網絡出品的網站模板批量下載軟件&#xff0c;西瓜仿站高手是一款仿站工具&#xff0c;仿站神器。軟件功能強大&#xff0c;能夠幫你輕松幫你下載任意網站、任意模板&#xff0c;并且速…

用hundred造句子_八個有趣的開學破冰游戲,線上線下都能用

知道大家最近都很忙&#xff0c;所以省略開篇&#xff0c;直接上正題——開學“破冰游戲”走起&#xff01;一、你比劃我來猜把詞語展示在PPT上&#xff0c;猜詞的同學背對PPT&#xff0c;其他同學可以看到詞語并且用身體動作把詞語表現出來&#xff0c;直到猜詞的同學可以把詞…

java 執行順序_Java代碼執行順序

程序中代碼執行的順序非常重要&#xff0c;稍有不慎便會是程序運行出錯&#xff0c;那么我將結合實例來分析代碼中的執行。名詞解釋首先了解幾個名詞&#xff1a;非靜態代碼塊直接由 { } 包起來的代碼&#xff0c;稱為非靜態代碼塊靜態代碼塊直接由 static { } 包起來的代碼&am…