求:S = a + aa + aaa + aaaa + aaaaa + ... + aaaaaaaaaa; 最后一个为n个a,其中a 是一个数字。例如:
2+22+222+2222+22222 (n = 5)
1 #include <stdio.h> 2 3 int * sum( int buffer[], int x, unsigned n)/*buffer用来存放n个x,所以buffer的最小size为n*/ 4 { 5 int i; 6 //初始化 7 for(i=0;i<n;i++) 8 { 9 buffer[i] = x; 10 } 11 //不进位,求和 12 for(i=0;i<n;i++) 13 { 14 buffer[i] = buffer[i]* (i+1); 15 } 16 //进位 17 for(i=n-1;i>0;--i) 18 { 19 int temp; 20 temp = buffer[i]/10; 21 buffer[i] = buffer[i] % 10; 22 buffer[i-1] += temp; 23 } 24 buffer[i] = buffer[i]%10; 25 return buffer; 26 } 27 28 int main() 29 { 30 int s[18]; 31 sum(s, 3,17); 32 int i; 33 for(i=0;i<17;i++) 34 printf("%d",s[i]); 35 //free(s); 36 return 0; 37 }
原文:http://www.cnblogs.com/yongjiuzhizhen/p/4335752.html