zoukankan      html  css  js  c++  java
  • 【9103】求n的累加和

    Time Limit: 10 second
    Memory Limit: 2 MB

    问题描述
    用高精度方法,求s=1+2+3+...+n的精确值(n以一般整数输入)

    Input

    文件输入仅一行,输入n

    Output

    s的值。

    Sample Input

    10
    

    Sample Output

    55
    
    

    【题解】

    就是高精度加单精度,加单精度的时候在第一位加上这个数字就好,然后从1到l进行一轮的进位即可。最后逆序输出。、

    【代码】

    #include <cstdio>
    
    int a[100],n,l = 1;
    
    void input_data()
    {
        scanf("%d",&n);
    }
    
    void get_ans()
    {
        for (int i = 1;i <= 99;i++) //先给每个位上的数置0
            a[i] = 0;
        for (int i = 1;i <= n;i++) //从1 累加到 n
            {
                a[1] += i; //直接在个位上递增i
                for (int i = 1;i <= l;i++) //然后来处理进位问题 直接 + / 然后%一下就好
                    {
                        a[i+1] += (a[i]/10);
                        a[i] = a[i] % 10;
                    }
                while (a[l+1] > 0) //延长位数。用一个while和l就能搞定
                    {
                        l++;
                        a[l+1] += (a[l] / 10);
                        a[l] = a[l] % 10;
                    }
            }
    }
    
    void output_ans() //最后要逆序输出答案。
    {
        for (int i = l;i >= 1;i--)
            printf("%d",a[i]);
    }
    
    int main()
    {
        input_data();
        get_ans();
        output_ans();
        return 0;
    }
    


     

  • 相关阅读:
    SRM 574 250 DIV2
    SRM 575 250 DIV2
    SRM 577 250 DIV2
    20181211-python1119作业郭恩赐
    20181207作业-郭恩赐
    python1119-20181206作业-郭恩赐提交
    python1119-20181205作业-郭恩赐提交
    python1119作业1-郭恩赐提交
    py1119_Linux学习_第二周总结
    小白都能看懂的block
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632435.html
Copyright © 2011-2022 走看看