zoukankan      html  css  js  c++  java
  • 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
    ");
                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
    ",s[i]);
                else
                    printf("%c",s[i]);
            }
        }
        return 0;
    }
    


  • 相关阅读:
    2020年12月2日
    2020年12月1日
    2020年11月30日
    2020年11月29日
    2020年11月28日
    2020年11月27日
    2020年11月26日
    2020年11月25日
    浅谈扩展欧几里得算法
    Hello 2020
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6958916.html
Copyright © 2011-2022 走看看