a=111111111111111111111111111111(30位) = (10^30-1)/9*1
b=999999999999999999999999999999(30位) = (10^30-1)/9*9
c=555555555555555555555555555555(30位) = (10^30-1)/9*5
(a*10^30 + b)/c
= [ (10^30-1)/9*1*10^30 + (10^30-1)/9*9 ] / [(10^30-1)/9*5]
分子分母同时除以 (10^30-1)/9
= (10^30 + 9) / 5
= 2*(10^30 + 9) / 10
= ( 2000…共28个零…00018) / 10
= 2000…共28个零…0001 余8
也就是
200000000000000000000000000001
假如用程序计算,
竖式除法是小学二三年级的知识,不应该不会呀
程序代码:
#include <stdio.h>
#include <string.h>
unsigned __divide_n( char* restrict rem, const char* b, size_t n )
{
unsigned quo = 0;
for( ; strncmp(rem,b,n)>=0; ++quo )
{
unsigned carry = 0;
for( size_t i=n; i!=0; --i )
{
carry = 10-carry + (rem[i-1]-'0') - (b[i-1]-'0');
rem[i-1] = carry%10 + '0';
carry = 1 - carry/10;
}
}
return quo;
}
void divide( const char* a, const char* b )
{
b += strspn( b, "0\n" );
if( !*b ) { puts("INF\n"); return; };
a += strspn( a, "0\n" );
if( !*a ) { puts("0\n"); return; };
const size_t alen = strlen( a );
const size_t blen = strlen( b );
if( alen<blen || (alen==blen && strcmp(a,b)<0) ) { puts("0\n"); return; };
char b_[100];
b_[0] = '0';
memcpy( b_+1, b, blen );
char rem[100];
rem[0] = '0';
strncpy( rem+1, a, blen );
unsigned quo = __divide_n( rem, b_, blen+1 );
if( quo != 0 )
printf( "%u", quo );
for( const char* p=a+blen; *p; ++p )
{
memmove( rem, rem+1, blen );
rem[blen] = *p;
unsigned quo = __divide_n( rem, b_, blen+1 );
printf( "%u", quo );
}
putchar( '\n' );
}
int main( void )
{
const char* araw = "111111111111111111111111111111";
const char* braw = "999999999999999999999999999999";
const char* craw = "555555555555555555555555555555";
char a[100];
strcat( strcpy(a,araw), braw );
divide( a, craw );
}