Welcome

首页 / 软件开发 / 数据结构与算法 / UVa 10994 Simple Addition :组合数学

UVa 10994 Simple Addition :组合数学2014-07-08 synapse7

10994 - Simple Addition

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1935计算sum{i从右往左数的第一个非0数字,p<=i<=q}。

见代码。。

01./*0.016s*/02.03.#include<cstdio>04.typedef long long ll;05.06.ll sum(ll n)07.{08.ll ans = 0, x;09.while (n)10.{11.x = n % 10;12.n /= 10;13.ans += ((1 + x) * x) / 2 + n * 45;///当个位在循环的时候,高位的朋友你在干嘛?14.}15.return ans;16.}17.18.int main()19.{20.ll a, b;21.while (scanf("%lld%lld", &a, &b), a >= 0)22.printf("%lld
", sum(b) - sum(a - 1));23.}