pku 1001

pku 1001
本来都打定主意叫它几十次,这道题考虑了很多细节,想了很多边界条件,一次交过了还是没想到的
我也不知道这道题到底要不要考虑那些边界条件,不过看交过的比例,要注意的肯定很多,也许还有应该要想到的
这是第一次作实数的高精度,写的很乱,很多都是发现问题后不上去的
  1 #include < iostream >
  2 using namespace std;
  3 #define Max  200
  4 #define Max_b  7
  5 typedef struct bigint{
  6      int  data[Max]; // 从0开始存储
  7      int   len ;
  8      int  i; // 表示小数位
  9     bigint()
 10     {
 11         memset(data, 0 ,sizeof(data));
 12          len = 1 ;
 13         i = 0 ;
 14     }
 15     friend bigint operator + (bigint,bigint);
 16     friend bigint operator * (bigint,bigint);
 17     void print(); // 小数点在第i位上,后面有i个小数位
 18     void operator = ( const  bigint & y){
 19          len = y.len;
 20          for ( int  j = 0 ;j < y.len;j ++ )
 21             data[j] = y.data[j];
 22         i = y.i;
 23     }
 24 }BIGINT;
 25 BIGINT operator + (BIGINT x,BIGINT y)
 26 {
 27     BIGINT r;
 28      int  rlen = x.len > y.len?x.len:y.len;
 29      int  tmp,i,jinwei = 0 ;
 30      for (i = 0 ;i < rlen;i ++ ){
 31         tmp = x.data[i] + y.data[i] + jinwei;
 32         r.data[i] = tmp% 10 ;
 33         jinwei = tmp / 10 ;}
 34      if (jinwei)r.data[rlen ++ ] = jinwei;
 35     r.len = rlen;
 36     return r;
 37 }
 38 BIGINT operator * (BIGINT x,BIGINT y)
 39 {
 40    BIGINT  r;
 41     int  i,j;
 42    memset(r.data, 0 ,sizeof(r.data));
 43    r.len = x.len + y.len;
 44     for (i = 0 ;i < x.len;i ++ )
 45         for (j = 0 ;j < y.len;j ++ )
 46            r.data[i + j] += x.data[i] * y.data[j];
 47     for (i = 0 ;i < r.len;i ++ ){
 48        r.data[i + 1 ] += r.data[i] / 10 ;
 49        r.data[i]% = 10 ;}
 50     while (r.data[i]){
 51        r.data[i + 1 ] += r.data[i];
 52        r.data[i]% = 10 ;
 53        i ++ ;}
 54     while (i >= 0 && !r.data[i])i -- ; // 这个已经消除了开头的零
 55     // 末尾不存在零,不用考虑
 56     if (i! =- 1 )r.len = i + 1 ;
 57     else  r.len = 1 ; // r为0的情况
 58    r.i = x.i + y.i;
 59    return r;
 60 }
 61 void BIGINT::print()
 62 {
 63      int  j,k;
 64      for (j = this -> len - 1 ;j >= this -> i;j -- ) // 输出了len - i个或是一个也还没输出
 65         printf( " %d " ,this -> data[j]);
 66      if (j ==- 1 ){
 67         putchar( ' \n');
 68         return;}
 69     putchar( ' .');
 70      if (j < this -> i) // 小数点后要补零
 71          for (k = this -> i - 1 ;k > j;k -- )
 72             putchar( ' 0');
 73      for (;j >= 0 ;j -- )
 74         printf( " %d " ,this -> data[j]);
 75     putchar( ' \n');
 76 }
 77 BIGINT cToBigint(char c[])
 78 {
 79      int  clen = ( int )strlen(c),i = 0 ,j = clen - 1 ,k;
 80     BIGINT result;
 81     memset(result.data, 0 ,sizeof(result.data));
 82      while (c[i] == ' 0'&&i<clen-1)i++;
 83      while (c[j] == ' 0'&&j>=0)j--;
 84      if (j > i){
 85         result.len = j - i;
 86          for (j = result.len - 1 ;c[i]! = ' .';j--,i++)
 87             result.data[j] = c[i] - ' 0';
 88         result.i = j + 1 ;
 89          for (i ++ ;j >= 0 ;j -- ,i ++ )
 90             result.data[j] = c[i] - ' 0';}
 91      else   if (j < i){ //
 92         result.len = 1 ;
 93         result.i = 0 ;}
 94      else   if (i == j && c[i]! = ' .'){//完全就是整数,没有小数点
 95          for (j = clen - 1 ,k = 0 ;j >= i;j -- ,k ++ )
 96             result.data[k] = c[j] - ' 0';
 97         result.len = k;
 98         result.i = 0 ;
 99     }
100     return result;
101 }
102 int  main()
103 {
104     BIGINT R,result;
105      int  n,i;
106     char c[Max_b];
107      while (scanf( " %s %d " ,c, & n)! = EOF){
108         memset(result.data, 0 ,sizeof(result.data));
109         result.i = 0 ;
110         result.len = 1 ;
111         R = cToBigint(c);
112         result.data[ 0 ] = 1 ;
113          for (i = 1 ;i <= n;i ++ )
114             result = result * R;
115         result.print();
116     }
117     return  0 ;
118 }

你可能感兴趣的:(pku 1001)