zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    HDOJ 1846 Brave Game
    并查集模板
    HDU 2102 A计划
    POJ 1426 Find The Multiple
    POJ 3278 Catch That Cow
    POJ 1321 棋盘问题
    CF 999 C.Alphabetic Removals
    CF 999 B. Reversing Encryption
    string的基础用法
    51nod 1267 4个数和为0
  • 原文地址:https://www.cnblogs.com/riasky/p/3430992.html
Copyright © 2011-2022 走看看