Follows 275.c (Total 71 lines):

/* @JUDGE_ID:4461XX 275 C "math" */
/* A */
#include<stdio.h>
#include<string.h>
#define MAX 1000

int used[MAX] , ans[MAX+1] , last[MAX+1] ;
int n , k ;

void Print( int digit )
{
	int i , j ;

	putchar( '.' ) ;
	for( i=1 ; i<=49&&i<digit ; ++i ) printf( "%d" , ans[i] ) ;
	putchar( '\n' ) ;

	if( digit>50 ){
		for( i=50,j=1 ; i<digit ; ++i,++j ){
			printf( "%d" , ans[i] ) ;
			if( !( j%50 )&&i+1<digit ){
				putchar( '\n' ) ;
				j = 0 ;
			}
		}
		putchar( '\n' ) ;
	}
}
void count( void )
{
	int digit , i ;

	memset( ans , 0 , sizeof( int )*MAX ) ;
	memset( last , 0 , sizeof( int )*MAX ) ;

	for( i=0 ; i<MAX ; ++i ) used[i] = -1 ;
	used[n] = 0 ;

	for( digit=1 ; ; ++digit ){
		n *= 10 ;

		ans[digit] = n/k ;
		last[digit] = n%k ;

		if( !( n%k ) ){
			Print( digit+1 ) ;
			puts( "This expansion terminates.\n" ) ;
			break ;
		}
		
		if( used[n%k]!=-1 ){
			Print( digit+1 ) ;
			printf( "The last %d digits repeat forever.\n\n" , digit-used[n%k] ) ;
			break ;
			
		}

		used[n%k] = digit ;
		n %= k ;
	}
}
int main( void )
{
	while( scanf( "%d %d" , &n , &k )==2 ){
		if( !n&&!k ) break ;

		count() ;
	}

	return 0 ;
}

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com