POJ1001

感觉写的太乱了,用了多层函数,基本是下面这个结构


int main()
{
	int get_num(char r[], short int rn[], int &rn_len)
	int R_e_n(int rn_pb, int n, int rn_len, short int rn[], short int t[]){
		void mul(short int rn[], short int t[], int &t_len, int rn_len)
	}
}



Source Code

Problem: 1001   User: washiwxm
Memory: 252K   Time: 0MS
Language: C++   Result: Accepted
  • Source Code
    #include
    
    using namespace std;
    
    #define N 10
    #define M 150
    
    void mul(short int rn[], short int t[], int &t_len, int rn_len)
    {
           int  res = 0, i, ca = 0, temp_len;
           short int temp[M];
           memset(temp,0,sizeof(short int)*M);
     
           for(i = 0; i < t_len; i++){
                   for(int j = 0; j < rn_len; j++){
                             res = t[i]*rn[j] + ca + temp[i + j];
                             ca = res / 10;
                             temp[i + j] = res % 10;
                             if((j == (rn_len - 1))&&(i != (t_len - 1))){
                                   temp[i + j + 1] = ca;
                                   ca = 0;
                             }else if((j == (rn_len - 1))&&(i == (t_len - 1))){
                                   temp[i + j + 1] = ca;
                             }          
                   }
                        
           }
           if(ca > 0){
                 //temp[t_len + rn_len -1] = ca;
                 temp_len = t_len + rn_len;
           }else{
                 temp_len =  t_len + rn_len -1;
           }
           for(i = 0; i < temp_len; i++){
                 t[i] = temp[i];
                 //cout<
           }
           //cout<
           t_len = temp_len;
    } 
    
    int R_e_n(int rn_pb, int n, int rn_len, short int rn[], short int t[])
    {
          //所有小数位数 
          
          int t_len = 1;
          t[0] = 1;
          while(n--){
                //每次用R乘以t,将结果存入t
                mul(rn, t, t_len, rn_len);     
          }
    
          return t_len;  
    
           
    } 
    
    
    int get_num(char r[], short int rn[], int &rn_len)
    {
          int i, j, ni;
          //rn_len来传递rn[N]的长度,以免后面重复计算 
          for(rn_len = -1, i = 0; r[i] != '\0'; i++) rn_len++;
          
          //************************
     
          //cout<<"r:"<
          
          i = rn_len;
          while(r[i] == '0'){
                     rn_len--;
                     i--;
          }
          
          
          //将r[N]倒置放入rn[N] 
          for(i = 0, j = 0, ni = 0; i <= rn_len; i++){
                
                if(r[i] != '.') {
                        rn[rn_len - 1 - ni] = r[i] - '0';
                        ni++;                
                }else{
                        j = i;      
                }     
          }
          //rn_len来传递rn[N]的长度,以免后面重复计算 
          //*****************************
          //cout<<"小数位数为"<
          
          //此时i为r[N]的长度,i - j - 1即为小数点后面的位数 
          return (i - j - 1);    
    }    
    
    int main()
    {
        int n;
        //r[N]记录R 
        char r[N];
        short int  rn[N];
        //t[M]存储每次结果 
        short int t[M];
        //t_len存储t的长度  n_pb为小数位数 
        int t_len, rn_pb, i, rn_len, t_pb;
        
        
        memset(r,0,sizeof(short int)*N);
        memset(rn,0,sizeof(short int)*N);
        memset(t,0,sizeof(short int)*M);
        
        //loop
        while(cin>>r>>n){
              //将r[N]转换为rn[N]
              
              rn_pb = get_num(r, rn, rn_len);
              //cout<<"小数位数为:"<
              t_len = R_e_n(rn_pb, n, rn_len, rn, t);
              t_pb = n * rn_pb;
              for(i = 0; (i < t_len - t_pb) && (t[t_len - i - 1] == 0); i++){
              }
              for(; i < t_len - t_pb; i++){      
                    cout<<t[t_len - i - 1];
              }
              if (t_pb != 0) cout<<".";
              
              for(i = t_len - t_pb; i < t_len; i++){
                    cout<<t[t_len - i - 1];
              }
              cout<<endl;
                       
              memset(rn,0,sizeof(short int)*N);
              memset(t,0,sizeof(short int)*M);
        }
        
        return 0;
    }

你可能感兴趣的:(POJ)