Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2529347
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-07-26 16:14:37

    题目:求s=a aa aaa aaaa aa...a的值,其中a是一个数字。例如2 22 222 2222 22222(此时共有5个数相加),几个数相加有键盘控制。
    看到这个题目后,我们可以看到每次都是在上次迭代的数乘以10加上输入的那个输入作为这一个数字进行求和的运算。因此我们的程序,也主要就是构造迭代后的数字。代码如下:

#include <stdio.h>

int main(int argc,int *argv[])
{
    int a,n,count = 1;
    int sum = 0,temp;
    printf("please input a,n:");
    scanf("%d,%d",&a,&n);
    temp = a;
    while (count <= n)
    {
          sum += temp;
          temp = temp * 10 + a;
          count ++;
    }
    printf("the result is :%d",sum);
    system("pause");
    return 0;
}


阅读(1507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~