UVA748 Exponentiation 解题报告

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=689

题目大意:实数阶乘

题目考点:大数模拟

解题思路:把实数转化为整数,输出时再加小数点;

题目代码:

View Code
  1 // File Name: uva748.c
  2 // Author: darkdream
  3 // Created Time: 2013年01月27日 星期日 11时15分03秒
  4 
  5 #include<stdio.h>
  6 #include<string.h>
  7 #include<stdlib.h>
  8 #include<time.h>
  9 #include<math.h>
 10 #define clean(b)  memset(b,0,sizeof(b))
 11 char a[100];
 12 
 13 int  change(char a[],int  b[])
 14 {
 15     int i , t = -1, j = 0 ;
 16     for (i = strlen(a)-1; i >= 0; i--)
 17         if(a[i] == '.')
 18         {
 19             t = i ;
 20 
 21         }
 22         else 
 23         {
 24             b[j++] = a[i] -'0';
 25         }
 26   if (t != -1)
 27      return strlen(a) - t -1 ;
 28   else return -1 ;
 29 }
 30 int strle(int c[])
 31 {
 32  int i ;
 33  for (i = 550 ;i >=0 ;i --)
 34     if (c[i] != 0)
 35         break;
 36  return  i + 1 ;
 37 }
 38 
 39 void mu(int c[],int d[], int e[])
 40 { 
 41     void print();
 42     int i ,j, k ,t ; 
 43     for (i =0 ;i < strle(c) ;i ++)
 44     {
 45         int s = 0 ;
 46         for (j = 0 ; j < strle(d) ;j++)
 47         {
 48             t = c[i]*d[j] +s +e[i+j] ;
 49             s = t / 10 ;
 50             e[i+j] = t % 10 ;
 51         }
 52         for (j = i+j  ; j< 550 ; j++)
 53         {
 54             t = e[j] + s;
 55             s = t /10 ;
 56             e[j] = t %10 ;
 57         }
 58 
 59     }
 60 
 61 }
 62 
 63 void print(int t , int c[])
 64 {
 65 
 66   int i , j ,k ; 
 67   for (i = 550 ;i >= 0; i --)
 68       if(c[i] != 0)
 69           break;
 70   if (i < t -1 )
 71   {
 72     printf(".");
 73     for (k =i ; k < t-1 ; k++)
 74         printf("0");
 75   }
 76   for (j = 0 ; j < t ; j++)
 77       if (c[j] != 0)
 78           break;
 79   
 80   for (; i >= j ; i--)
 81   {  if (i  ==  t-1)
 82         printf(".");             
 83       printf("%d",c[i]);
 84   }
 85   printf("\n");
 86 
 87 }
 88 
 89 
 90 int main(){
 91 
 92     while(scanf("%s",a) != EOF)
 93     {
 94         int n , d[600], e[600] , f[600];
 95         clean(d);
 96         clean(e);
 97         clean(f);
 98         scanf("%d",&n);
 99         int i , j , k , l  , t;
100         t = change(a , d);
101         t = t * n ;
102         memcpy(e , d ,sizeof(d) );
103        if (n >1)
104         mu(d,e,f);
105        else 
106          memcpy(f,e,sizeof(e));
107         for (i = 3 ; i <= n; i++)
108         {
109           memcpy(d,f,sizeof(f));
110           clean(f);
111           mu(d,e,f);
112         }
113       print(t,f);
114 
115     }
116 
117     return 0 ;
118 }
原文地址:https://www.cnblogs.com/zyue/p/2878644.html