大数阶乘(某公司笔试题)

 

在int型范围内,打印
(1)求N!
(2)求N!-M!
 (3) 求N!+M!

package demo05;

import   java.util.ArrayList;
import   java.util.Scanner;

public   class   JieCheng {

   
       public   static   void   main(String[] args) {
          
             while ( true ){
             boolean   bl =   false   //判断change方法中是否执行打印操作
            System. out .println( "请选择你要的操作:" );
            System. out .println( "1、计算N的阶乘" );
            System. out .println( "2、计算N!+M!的和" );
            System. out .println( "3、计算N!-M!的差(M>N)" );
            System. out .println( "4、退出" );
            Scanner input =   new   Scanner(System. in );
             int   c = input.nextInt();
             switch (c){
             case   1 :
                  System. out .println( "请输入N的值:" );
                  Scanner num =   new   Scanner(System. in );
                   int   number = num.nextInt();
                  bl= true ;
                   change (number,bl);
                   break ;
             case   2 :
                  System. out .println( "请输入N和M的值:(N<M)" );
                  Scanner num1 =   new   Scanner(System. in );
                   int   n = num1.nextInt();
                   int   m = num1.nextInt();
                   sum ( change (n,bl), change (m,bl));
                   break ;
             case   3 :
                  System. out .println( "请输入N和M的值:(N<M)" );
                  Scanner x =   new   Scanner(System. in );
                   int   y = x.nextInt();
                   int   z = x.nextInt();
                   minus ( change (y,bl), change (z,bl));
                   break ;
             case   4 :
                  System. out .println();
                  System. out .println();
                  System. out .println();
                  System. out .println( "good-bye!" );
                   return ;
            }
            }
      }
          /**
         * 计算输入数字的阶乘,并转存在数组中
         */
          public   static   ArrayList<Integer>   change ( int   num, boolean   bl){
        ArrayList<Integer> list =   new   ArrayList<Integer>();
               list.add(1);
                 for ( int   i=2;i<=num;i++){
                       int   c = 0;
                       for ( int   j = 0;j<list.size();j++){
                             int   p = list.get(j)*i+c;
                           list.set(j, p%10);
                           c=p/10;
                     }
                       while (c>0){
                           list.add(c%10);
                           c=c/10;
                     }
               }
                 if (bl== true ){
                       for ( int   j = list.size();j>0;j--){
                     System. out .print(list.get(j-1));
                     }
                     System. out .println();
                     bl= false ;
               }
             
             return   list;
        }
          public   static   void   sum(ArrayList<Integer> n ,ArrayList<Integer> m){
            ArrayList<Integer> s =   new   ArrayList<Integer>();
             int   c = 0;    //存在进位值
             int   i= 0;     //存储的是所用元素的角标
                 for (i = 0;i<n.size();i++){
                    int   temp = n.get(i)+m.get(i)+c;
                  s.add(i, temp%10);
                  c=temp/10;
               }
                 while (i<=m.size()-1)
               {
                       int   temp=m.get(i)+c;
                     s.add(i,temp%10);
                     c/=10;
                     i++;
               }
               for ( int   j = s.size();j>0;j--){
               System. out .print(s.get(j-1));
               }
             System. out .println();
        }
          /**
         * 拿大数减小数,结果加负号
         */
          public   static   void   minus(ArrayList<Integer> n ,ArrayList<Integer> m){
            ArrayList<Integer> min =   new   ArrayList<Integer>();
             int   c = 0 ;     //借位值
             int   i = 0;
                 for (i = 0;i<n.size();i++){
                 int   temp = m.get(i)-n.get(i)+c;
                 if (temp>=0){
                     min.add(i, temp);
                     c=temp/10;
               } else   if (temp<0){
                     min.add(i,temp+10);
                     c=-1;
               }
               }
                 while   (i<m.size()){
                       int   temp = m.get(i)+c;
                       if (temp>0){
                           min.add(i, temp);
                           i++;
                           c=0;
                     } else   if (temp==0){
                           min.add(i, temp);
                           i++;
                           c=0;
                     } else   if   (temp<0){
                           min.add(i,9);
                           c=-1;
                           i++;
                     }
               }
               System. out .print( "-" );
               for ( int   j = min.size();j>0;j--){
               System. out .print(min.get(j-1));
               }
             System. out .println();
        }
}

                                                   欢迎大家拍砖,指正!

你可能感兴趣的:(职场,休闲,大数阶乘代码)