• 10994


    Problem E
    Simple Addition
    Input: 
    Standard Input

    Output: Standard Output

     

    Let’s define a simple recursive function F (n), where

    Let’s define another function S (p, q),

    In this problem you have to Calculate S (p, q) on given value of   and q.

     

    Input

    The input file contains several lines of inputs. Each line contains two non negative integers and q (p <= q) separated by a single space. p and q will fit in 32 bit signed integer. In put is terminated by a line which contains two negative integers. This line should not be processed.

    For each set of input print a single line of the value of S(p, q).

           Sample Input                               Output for Sample Input

    1 10

    10 20

    30 40

    -1 -1

     

    46

    48

    52

    题意:求题目中那个公式从p到q F【i] 的和

    思路:每个数肯定是1-9.这样的规律,个位1-9,十位1-9,百位1-9.如此一来,只要把各位加完,加十位,加百位,一个个加完,最后就是答案了。加的时候可以直接用等差数列和公式

    代码:

    #include <stdio.h>
    
    int p, q;
    
    long long Sum(long long s, long long e) {
        if (s > e)
    	return 0;
        return (s + e) * (e - s + 1) / 2;
    }
    long long cal(long long n) {
        if (n <= 0) return 0;
        long long ans = 0;
        while (n) {
    	ans += n / 10 * 45;
    	ans += Sum(1, n % 10);
    	n /= 10;
        }
        return ans;
    }
    
    int main() {
        while (~scanf("%d%d", &p, &q) && p != -1 && q != -1) {
    	printf("%lld
    ", cal(q) - cal(p - 1));
        }
        return 0;
    }


  • 相关阅读:
    Linux系统下公式编辑器KLatexFormula
    C++11 std::chrono库详解
    Direct Visual-Inertial Odometry with Stereo Cameras
    改变机器人的初始位姿,让机器人的初始位置不在地图的原点处
    ubuntu为文件添加可执行权限
    oracle的启动和停用
    orcale创建用户、授权
    手工配置oracle数据库
    MySQL存储过程详解 mysql 存储过程
    mysql的navicat执行存储过程
  • 原文地址:https://www.cnblogs.com/riasky/p/3430992.html
Copyright © 2020-2023  润新知