标题:我服了,到底错哪了?高手帮忙!
取消只看楼主
shinobifc
Rank: 2
等 级:论坛游民
帖 子:12
专家分:20
注 册:2008-7-31
结帖率:100%
 问题点数:0 回复次数:1 
我服了,到底错哪了?高手帮忙!
程序的目的是实现:a+aa+aaa+... ...+aaa..aaa(n个a)的和,如n=3,a=2,则为:2+22+222。我的代码如下:
#include <math.h>
main ()
{
    long int a, temp=0, add=0, sum=0;
    int n, i;
    printf("Please input positive integers 'a' and 'n':\n");
    scanf("%ld %d", &a, &n);
    while (a<=0 || n<=0)
    {
        printf("Sorry! 'a' or 'n' doesn't fit. Please input 'a' & 'n' again:\n");
        scanf("%ld %d", &a, &n);
    }
    while (a>=10)
    {
        printf("Sorry! 'a' can not be larger than 9. Please input 'a' & 'n' again:\n");
        scanf("%ld %d", &a, &n);
    }
    for (i=n; i>0; i--)
    {
        temp=temp+a*pow(10,i-1);
    }
    add=(int)(temp);
    sum=add;
    while (add>9)
    {
        add=(int)(add/10);
        sum=sum+add;
    }
    printf("%d+%d%d+...+%d=%d.\n",a,a,a,(int)(temp),sum);
}
编译通过,测试:a=2, n=6,结果完全不对,a甚至还等于0了。我郁闷了,到底错哪了?怎么也调不出来!谢谢了!
搜索更多相关主题的帖子: long positive include 
2010-02-08 17:53
shinobifc
Rank: 2
等 级:论坛游民
帖 子:12
专家分:20
注 册:2008-7-31
得分:0 
回复 2楼 heartnheart
那该如何解决呢?
2010-02-08 20:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-297442-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.491633 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved