文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序习题-计算数字连加的计算结果[6.3]

C程序习题-计算数字连加的计算结果[6.3]

时间:2010-07-27  来源:chengxiaopeng

    求Sn = a + aa + aaa + aaaa +...v之值,其中a是一个数字。例如: 2 + 22 + 222 + 2222 + 22222 (此时n=5),n由键盘输入。     看到这个题目后,我们可以看到看到每一项的数字都都有一个迭代的过程。即 f(n)= f(n-1) * 10 + a得到。推理出这个迭代过程。我们的程序就好书写了,代码如下:

#include <stdio.h>

int main(int argc,int *argv[])
{
    int a,n;
    int i,temp,result;
    printf("please input a,n:");
    scanf("%d,%d",&a,&n);
    
    temp = 0;
    result = 0;
    
    for(i = 0 ; i < n; i++)
    {
          temp = temp * 10 + a;
          result += temp;
          
          if (i == n - 1)
          {
             printf(" %d ",temp);
          }
          else
          {
             printf("%d + ",temp);
          }
    }
    
    printf(" = %d",result);
    system("pause");
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载