<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    where the amazing happens

    算法3:計算超大數字整數乘法

    還不能處理負數和小數點

    package?s1;

    import?java.util.Stack;
    import?java.util.Vector;


    /**
    ?*?A?multiply?simulation
    ?*?for?example?:?
    ?*?56?X?67?=?
    ?*
    ?*?????56
    ?*???x?67
    ?*?----------
    ?*????392
    ?*???336
    ?*------------
    ?*???3752
    ?*
    ?*?So?,in?this?way,We?can?calculation?very?very?big?number,for?example:?299^133?etc.?
    ?*?
    ?*?
    @author?Urashima?
    ?*
    ?
    */

    public?class?BigNumberMultiply?extends?BaseNumberOperation{
    ????
    ????
    //each?array?indicates?a?param
    ????private?Integer[]?paramFirst,paramSecond;
    ????
    ????
    //main?method????
    ????public?String?calculate(String?param1,String?param2){
    ????????paramFirst??
    =?convert(param1);
    ????????paramSecond?
    =?convert(param2);
    ????????
    //multiply?each?bit,from?low?to?high
    ????????int?indexFirst?=?paramFirst.length?-?1;
    ????????
    int?indexSecond?=?paramSecond.length?-?1;
    ????????
    //multiply?results?for?each?bit
    ????????Vector<Integer[]>?branchResults?=?new?Vector<Integer[]>();
    ????????
    for(;indexSecond?>?-1;indexSecond--){
    ????????????branchResults.add(branchMultiply(paramFirst,paramSecond[indexSecond]));
    ????????}

    ????????String?finalResult?
    =?branchAddTogether(branchResults);
    ????????
    return?finalResult;
    ????}

    ????
    ????
    private?Integer[]?branchMultiply(Integer[]?upper,Integer?down){
    ????????Stack
    <Integer>?results?=?new?Stack<Integer>();
    ????????
    //todo?:?all?core?gose?here
    ????????int?carryFlag?=?0;
    ????????
    for(int?index?=?upper.length-1?;index?>?-1;index--){
    ????????????
    int?r1?=?down;
    ????????????
    int?r2?=?upper[index];
    ????????????
    int?r0?=?r1?*?r2?+?carryFlag;
    ????????????carryFlag?
    =?(int)(r0/10);
    ????????????
    int?r3?=?r0?-?(?10?*?carryFlag?);
    ????????????
    if(index!=0)
    ????????????????results.push(r3);
    ????????????
    else
    ??????????????????results.push(r0);????
    ????????}

    ????????
    //sorts?out?and?return
    ????????Integer[]?branchResult?=?new?Integer[results.size()];
    ????????results.toArray(branchResult);
    ????????
    //System.out.println?(branchResult.toString());
    ????????return?branchResult;
    ????}
    ????
    ????????
    ????
    private?String?branchAddTogether(Vector<Integer[]>?v){
    ????????Vector
    <String>?params?=?new?Vector<String>();
    ????????
    //TODO:?fix?bug#001
    ????????int?bi?=?0;
    ????????
    for(Integer[]?pps?:?v){
    ????????????
    //revers?pps
    ????????????Integer[]?rpps?=?new?Integer[pps.length];
    ????????????System.arraycopy(pps,
    0,rpps,0,pps.length);
    ????????????
    for(int?k?=?pps.length-1?;?k?>?-1?;?k--?){
    ????????????????rpps[pps.length
    -1-k]?=?pps[k];
    ????????????}

    ????????????v.set(bi
    ++,rpps);
    ????????}

    ????????
    //sort?out?data,add?increamental?0?to?each?bit
    ????????for(Integer[]?ii?:?v){
    ????????????String?pa?
    =?"";
    ????????????
    for(Integer?i?:?ii){
    ????????????????pa?
    +=?i;
    ????????????}

    ????????????params.add(pa);
    ????????}
    ?????
    ????????
    int?incr?=?0;
    ????????
    for(int?idx?=?0?;?idx?<?params.size();?idx?++){
    ????????????String?s?
    =?params.get(idx);
    ????????????
    for(int?i?=?0?;?i?<?incr?;?i?++){
    ????????????????s?
    +=?"0";
    ????????????}

    ????????????params.set(idx,s);
    ????????????
    //System.out.println?(s);
    ????????????incr++;
    ????????}

    ????????
    //convert?back?to?int[]
    ????????for(int?i?=?0?;?i?<?params.size();i++){
    ????????????String?ctt?
    =?params.get(i);
    ????????????
    //System.out.println?(ctt);
    ????????????v.set(i,convert(ctt));
    ????????}

    ????????
    //add?them?together????
    ????????Stack<Integer>?result;
    ????????
    //trim?right?and?add
    ????????result?=?trimRightAdd(v);
    ????????StringBuffer?sb?
    =?new?StringBuffer();
    ????????
    try{
    ?????????
    while(true)
    ?????????????sb.append(result.pop());
    ????????}
    catch(Exception?e){
    ????????????
    //pass,ignore.
    ????????}

    ????????
    return?sb.toString();????
    ????}
    ????
    ????????
    ????
    private?Stack<Integer>?trimRightAdd(Vector<Integer[]>?params){????
    ????????Stack
    <Integer>?result?=?new?Stack<Integer>();
    ????????
    int?carry?=?0;
    ????????
    int?maxBit?=?0;
    ????????
    ????????
    //todo:maxbit
    ????????for(Integer[]?i?:?params){
    ????????????
    int?il?=?i.length;
    ????????????maxBit?
    =?il?>?maxBit?il:maxBit;
    ????????}

    ????????
    //bit?moving?,?from?low?to?higth
    ????????int?indexDecreaseCount?=?1;
    ????????
    int?columnValue?=?0;
    ????????
    int?bitValue?=?0;????
    ????????
    for(int?k?=?0?;?k?<?maxBit;?k++){
    ?????????
    if(k?>?0){
    ?????????????result.push(bitValue);
    ?????????????columnValue?
    =?0;
    ?????????????bitValue?
    =?0;
    ?????????}

    ?????????
    //value?of?each?column,including?carry????
    ?????????int?num?=?0;
    ?????????
    for(Integer[]?param?:?params){
    ???????????????
    int?index?=?param.length?-?indexDecreaseCount;
    ?????????????
    try{
    ?????????????????num?
    =?param[index];
    ?????????????}
    catch(Exception?e){
    ?????????????????num?
    =?0;
    ?????????????}

    ?????????????
    //TODO:?may?be?simulation?calculate?is?better?here
    ?????????????columnValue?+=?num;
    ?????????}

    ?????????
    //first?bit
    ?????????if(k?!=?maxBit-1?){
    ?????????????columnValue?
    +=?carry;
    ?????????????carry?
    =?(int)(columnValue/10);
    ?????????????bitValue?
    =?columnValue?-?(?10?*?carry?);
    ?????????????indexDecreaseCount?
    ++;
    ?????????}
    else{
    ?????????????columnValue?
    +=?carry;
    ?????????????result.push(columnValue);
    ?????????}

    ???????}

    ???????
    return?result;
    ????}
    ????
    ????
    }
    測試計算結果
    package?s1;

    public?class?Demo{
    ????
    ????
    private?TwoNumberOperation?operatorMultiply?=?new?BigNumberMultiply();
    ????
    ????
    public?void?multiplyDemo(String?param1,String?param2){
    ????????String?result?
    =?operatorMultiply.calculate(param1,param2);
    ????????System.out.println?(param1
    +"?x?"+param2+"?=?"+result);
    ????}

    ????
    ????
    public?static?void?main?(String[]?args)?{
    ????????Demo?demo?
    =?new?Demo();
    ????????demo.multiplyDemo(
    "12","12");
    ????????demo.multiplyDemo(
    "56","67");
    ????????demo.multiplyDemo(
    "3459398","11667278");
    ????????demo.multiplyDemo(
    "9283736253829832323432342342543654576343534564353734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553745645545",
    ??????????????????????????
    "23470926947345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537863053758734076407083760987207345345345345345345537693709479");
    ????}

    ????
    }


    --------------------Configuration: <Default>--------------------
    12 x 12 = 144
    56 x 67 = 3752
    3459398 x 11667278 = 40361758178644
    9283736253829832323432342342543654576343534564353734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553745645545 x 23470926947345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537734534534534534534553773453453453453453455377345345345345345345537863053758734076407083760987207345345345345345345537693709479 = 217897895412061538535213749538424917178427642790309526149197050342787946552594940694811082625513148107368458788645637103672066743646902872289253339664141494118086813947265391829794894470256056436951017797373234350763998817946444452982381515332165838866556971112151531240855136817304521889160157930869290050844235614446072972714978233006835557537630008993034139267140464023218920328137852716504553853724234899138660678581541565601300563516326950021051186577024411324340905167953542819936071934540621055

    Process completed.

    posted on 2006-09-25 20:40 where the amazing happens 閱讀(1473) 評論(1)  編輯  收藏 所屬分類: 算法&數據結構

    評論

    # re: 算法3:計算超大數字整數乘法 2006-12-26 09:33 zy

    這個程序你沒有給全嗎?能給我個完整的程序嗎,太感謝拉!   回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     

    公告

    點擊這里給我發消息

    導航

    <2006年12月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(3)

    隨筆分類(18)

    隨筆檔案(17)

    文章分類

    相冊

    其他我的blog

    技術Blog

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲成aⅴ人片在线观| www.亚洲色图.com| **aaaaa毛片免费同男同女| 99国产精品视频免费观看| 99久热只有精品视频免费看| 国产羞羞的视频在线观看免费| 黄视频在线观看免费| 天黑黑影院在线观看视频高清免费| 全黄大全大色全免费大片| 97人妻精品全国免费视频 | 中国亚洲女人69内射少妇| 亚洲精品无码永久在线观看| 亚洲欧洲日本在线| 久久亚洲国产精品一区二区| 久久噜噜噜久久亚洲va久| 日韩精品一区二区亚洲AV观看| 亚洲黄色在线视频| 在线观看亚洲AV日韩A∨| 日韩国产欧美亚洲v片| 色多多免费视频观看区一区| 国产高清视频免费在线观看| 成全视频高清免费观看电视剧| 99久久久国产精品免费牛牛四川| 免费观看国产网址你懂的| 成人人观看的免费毛片| 亚洲成网777777国产精品| 日本亚洲欧洲免费天堂午夜看片女人员 | 亚洲国产综合精品一区在线播放| 国产亚洲精久久久久久无码AV| 亚洲国产精品VA在线看黑人| 亚洲综合免费视频| 亚洲精品av无码喷奶水糖心| 人妻18毛片a级毛片免费看| 暖暖免费日本在线中文| 一二三四在线观看免费高清中文在线观看| 巨胸喷奶水视频www网免费| 免费国产成人高清在线观看麻豆| 亚洲午夜久久久影院| 亚洲午夜在线一区| 美女被免费网站在线视频免费| 97超高清在线观看免费视频|