pat(A) 2-06. 数列求和(模拟摆竖式相加)

1.链接:http://www.patest.cn/contests/ds/2-06

2.思路:模拟摆竖式相加,因为同样位置上的数字同样,那么同一位上的加法就能够用乘法来表示

3.代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

char s[1000000];

int main()
{
    int a,n;
    while(scanf("%d%d",&a,&n)==2)
    {
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        memset(s,'0',sizeof(s));
        int t=0;
        int j=0;
        for(int i=n; i>=1; i--)
        {
            int x=a*i+t;
            s[j++]+=x%10;
            t=x/10;
        }
        while(t>0)
        {
            s[j++]+=t%10;
            t=t/10;
        }
        for(int i=j-1; i>=0; i--)
        {
            if(i==0)
                printf("%c\n",s[i]);
            else
                printf("%c",s[i]);
        }
    }
    return 0;
}


posted on 2017-06-07 20:23  yjbjingcha  阅读(160)  评论(0编辑  收藏  举报

导航