博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 12517 Digit Sum(数学题)
阅读量:6365 次
发布时间:2019-06-23

本文共 2014 字,大约阅读时间需要 6 分钟。

 Digit Sum

Description

The security system for Associated Computer Informatics Systems (ACIS) has an interesting test to check the identity for authorized personal. These persons have got a piece of software that allowed them to calculate, given two integer positive numbers M and N, what is the sum of the decimal digits in the sequence MM + 1, ..., N. Then, when somebody is trying to access ACIS system, he/she is asked to answer the question of the sum for some M and N that are provided at that moment, by means of the given software.

ACIS programmers have developed a rather naïve algorithm only to verify that the method calculates the right answer. Now they are interested in developing a faster algorithm, in order to stop unauthorized users (who may be detected because they do not answer the sum question fast enough). And then you have been hired to help ACIS programmers to find such a method.

Input

The problem input consists of several cases, each one defined by a line with two integer numbers, M and N, without leading blanks and separated by a blank. You may assume that 1   M   N   109. The end of the input is signaled by a line with two zero values.

Output

For each case, output a line with the sum of the decimal digits for the sequence MM + 1, ..., N.

Sample Input

3 8

5 18

1 50

0 0

Sample Output

33

80

330

 

刚开始写博客还有很多不熟悉,见谅

题目的意思是求出从mn的所有的数的每一位的和

我的思路是分别求出从1n的和、从1m的和,相减再加上m数字的各位和

关键是求出每一位数字和,如果用循环的话,直接超时,所以找规律

我是定义一个a数组,用来存储从199999999999999… 的各位和,这样就可以对应相应位置上的数

 

#include
#include
long long int a[11]; void init(){ long long int i,j; for(i=0;i<=10;i++) a[i]=0; for(i=2,j=1;i<=10;i++,j*=10){ //因为从1到9的和为45,而位数增加以为,对应上一位就需要增加十倍 a[i]=45*j+10*a[i-1]; }}long long int f(long long int x){ //求从1到x的各位和 long long int i,temp=x,flag=1,num=0,ans=0,fflag=1; while(temp) { long long int k=temp%10; if(flag==1){ for(i=0;i<=k;i++) ans+=i; } else{ long long int sum=0; for(i=1;i

 

 

转载地址:http://ojama.baihongyu.com/

你可能感兴趣的文章
xen MacOS
查看>>
如何学好C和C++
查看>>
Gitlab通过custom_hooks自动更新服务器代码
查看>>
我的友情链接
查看>>
python 如何判断调用系统命令是否执行成功
查看>>
Lesson10 vSphere 管理特性
查看>>
memcache 扩展和 memcached扩展安装
查看>>
好程序员的查克拉---自信
查看>>
线程池的设计(二):领导者追随者线程池的设计
查看>>
获取设备列表
查看>>
Django使用网上模板做个能展示的博客
查看>>
基于同IP不同端口,同端口不同Ip的虚拟主机 基于FQDN的虚拟主机
查看>>
项目软件集成三方模块,编译中int32和uint32定义冲突解决方法
查看>>
StretchDIBits速度测试(HALFTONE)
查看>>
在.NET Workflo“.NET研究”w 3.5中使用多线程提高工作流性能
查看>>
验证Oracle处理速度
查看>>
自己写一个jquery
查看>>
BGP聚合attribute-map
查看>>
艾伟:C#中抽象类和接口的区别
查看>>
Flink - NetworkEnvironment
查看>>