90分求助!(非打表)
  • 板块P1727 计算π
  • 楼主lzh009
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/8/30 20:16
  • 上次更新2023/11/3 00:17:41
查看原帖
90分求助!(非打表)
952814
lzh009楼主2023/8/30 20:16
#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
#define KS_MIN(a, b) ((a)<(b)?(a):(b))
#define KS_MAX(a, b) ((a)>(b)?(a):(b))
 
#define BITS 10000
 
 int nn;
typedef struct BigInt
{
    int L;
    int* d;
    BigInt()
    {
        L = 1;
        d = ( int* )malloc( ( BITS + 16 ) * sizeof( int ) );
        d[0] = 0;
    }
    ~BigInt()
    {
        free( d );
    }
 
} BigInt;
 
// out = in / x;
void division( BigInt& out, BigInt& in, int x )
{
    out.L = in.L;
    memset( out.d, 0, out.L * sizeof( int ) );
    int i, s = 0;
    for ( i = in.L - 1; i >= 0; i-- )
    {
        s = s * 10 + in.d[i];
        out.d[i] = s / x;
        s %= x;
    }
    for ( i = out.L - 1; i >= 0; i-- )
    {
        if ( out.d[i] ) break;
    }
    out.L = i + 1;
    if ( out.L == 0 ) out.L++;
}
 
// a += b;
void addequal( BigInt& a, BigInt& b )
{
    int len = KS_MAX( a.L, b.L ) + 1;
    memset( a.d + a.L, 0, ( len - a.L )*sizeof( int ) );
    a.L = len;
    int i;
    for ( i = 0; i < b.L; i++ )
        a.d[i] += b.d[i];
    for ( i = 0; i < a.L - 1; i++ )
    {
        if ( a.d[i] > 9 )
        {
            a.d[i + 1]++;
            a.d[i] -= 10;
        }
    }
    for ( i = a.L - 1; i >= 0; i-- )
    {
        if ( a.d[i] ) break;
    }
    a.L = i + 1;
    if ( a.L == 0 ) a.L++;
}
 
// a -= b;
void subequal( BigInt& a, BigInt& b )
{
    int i;
    for ( i = 0; i < b.L; i++ )
        a.d[i] -= b.d[i];
    for ( i = 0; i < a.L - 1; i++ )
    {
        if ( a.d[i] < 0 )
        {
            a.d[i + 1]--;
            a.d[i] += 10;
        }
    }
    for ( i = a.L - 1; i >= 0; i-- )
    {
        if ( a.d[i] ) break;
    }
    a.L = i + 1;
    if ( a.L == 0 ) a.L++;
}
 
// a *= k
void mulequal( BigInt& a, int k )
{
    int i;
    for ( i = 0; i < a.L; i++ )
        a.d[i] *= k;
    memset( a.d + a.L, 0, 12 * sizeof( int ) );
    a.L += 12;
    for ( i = 0; i < a.L - 1; i++ )
    {
        if ( a.d[i] > 9 )
        {
            a.d[i + 1] += a.d[i] / 10;
            a.d[i] %= 10;
        }
    }
    for ( i = a.L - 1; i >= 0; i-- )
    {
        if ( a.d[i] ) break;
    }
    a.L = i + 1;
    if ( a.L == 0 ) a.L++;
}
 
void BigIntCopy( BigInt& dst, BigInt& src )
{
    dst.L = src.L;
    memcpy( dst.d, src.d, dst.L * sizeof( int ) );
}
 
void argtanx( BigInt& out, int x, int digit )
{
    BigInt a, b;
    a.L = digit + 1;
    memset( a.d, 0, ( a.L - 1 )*sizeof( int ) );
    a.d[a.L - 1] = 1;
    division( b, a, x );
    BigIntCopy( a, b );
    BigIntCopy( out, b );
    int i = 3, xx = x * x, p = -1;
    while ( 1 )
    {
        division( b, a, xx );
        BigIntCopy( a, b );
        division( b, a, i );
        if ( b.L == 1 && b.d[0] == 0 )
            break;
        if ( p < 0 ) subequal( out, b );
        else addequal( out, b );
        p *= -1;
        i += 2;
    }
}
 
 /*
void print( BigInt& a )
{
    printf( "%d.", a.d[a.L - 1] );
    int i;
    int low = a.L > BITS ? a.L - BITS : 0; // 不打算全部打印出来
    for ( i = a.L - 2; i >= low; i-- ){
    	printf( "%d", a.d[i] );
	}
    printf( "\n" );
}
*/

void print(BigInt& a)
{
	printf( "%d.",a.d[a.L - 1] );
	printf( "\n");
	int i,j=0;
	for( i = a.L - 2; i >=( a.L - 1 - nn); i-- ){
		if( j % 10 == 0 && i != a.L - 2)
		{
			printf(" ");
		}
		if( j % 50 == 0 && i != a.L - 2) 
		{
			printf( "\n");
		}
		j++;
		printf( "%d", a.d[i] );
	}
}
 
int main( int argc, char* argv[] )
{
	scanf( "%d", &nn );
    BigInt a, b;
    argtanx( a, 5, BITS );
    argtanx( b, 239, BITS );
    mulequal( a, 16 );
    mulequal( b, 4 );
    subequal( a, b );
    print( a );
    return 0;
}
 
///* pi/4 = 4*arctan(1/5) - arctan(1/239) */
2023/8/30 20:16
加载中...