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;
    }


  • 相关阅读:
    关于写页面时容易发生的问题
    用wamp实现前端和php的交互效果
    原生封装的ajax
    用angular引入复杂的json文件2
    用angular引入复杂的json文件
    vue属性
    vue的事件
    require'模块化jquery和angular问题
    css3在页面中插入内容
    css3+javascript实现翻页幻灯片
  • 原文地址:https://www.cnblogs.com/riasky/p/3430992.html
Copyright © 2011-2022 走看看