n的范围是多少?你用哪个LB的int能存得下n个a吗?
程序代码:
#include <stdio.h> static void foo_( unsigned a, unsigned n, unsigned carry ) { if( n < 2 ) { putchar( '0' + n*a - carry ); return; } foo_( a, n-1, ((n-2)*a+carry + 9)/10 ); putchar( '0' + (10 - ((n-2)*a+carry)%10)%10 ); } void foo( unsigned a, unsigned n ) { foo_( a, n, 0 ); putchar( '\n' ); } int main( void ) { foo( 2, 3 ); // 198 foo( 2, 2 ); // 20 foo( 2, 1 ); // 2 foo( 2, 0 ); // 0 foo( 8, 99 ); // 790123456790123456790123456790123456790123456790123456790123456790123456790123456790123456790123544 }