整数求和

来源:互联网 发布:淘宝商城女童鞋 编辑:程序博客网 时间:2024/06/05 10:13

Description
输入两个整数a和n,求 a + aa + aaa +…+ aa…a (注:n个a) 之和。例如输入2和3,输出246(因为2 + 22 + 222 = 246)。

本题所给测试数据的计算结果不超出unsigned long的表示范围。

Input
输入有多行,每一行是两个整数。第一个是上述的a,第二个是上述的n。

a不是负数,n > 0 。

Output
对应输入的每行,单独输出一行求和的结果。

Sample Input
2 3
9 4

Sample Output
246
11106

#include <stdio.h>#include <math.h>int main(void){    double a,n,j,sum=0,temp=1,temp1=0;    while (scanf("%lf %lf",&a,&n)!=EOF)    {        if(a>=0&&n>0)        {            for(j=0;j<n;j++)            {                temp=pow(10,j);                temp1=temp+temp1;                sum=sum+temp1;            }            printf("%.0f\n",sum*a);            sum=0;            temp1=0;        }    }    return 0;}