若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1198: How many 0

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,141

题目描述

A Benedict monk No. 16 writes down the decimal representations of all natural numbers between and including
m and n , m≤ n
. How many 0's will he write down?
Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, m ≤ n. The last line of input has the value of m negative and this line should not be processed.
For each line of input print one line of output with one integer number giving the number of 0's written down by the monk.

输入

暂无

输出

暂无

样例输入

10 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

样例输出

1
22
92
987654304
3825876150

参考代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
unsigned countl(char *s) 
{
    unsigned r;
    if (strlen(s) == 1) return 1;
    if (s[0] == '0') 
    {
        sscanf(s+1,"%lu",&r);
        return 1 + r + countl(s+1);
    }
    r = pow(10,strlen(s+1));
    r += (s[0]-'0') * strlen(s+1) * pow(10,strlen(s+1)-1);
    r += countl(s+1);
    return r;
}
unsigned count(char *s) 
{
    unsigned r = 1;
    char tmp[20];
    if (strlen(s) == 1) return 1;
    for (r=0;r<strlen(s)-1;r++) tmp[r] = '9';
    tmp[r] = 0;
    r = count(tmp);
    r += (s[0]-'1') * strlen(s+1) * pow(10,strlen(s+1)-1);
    r += countl(s+1);
    return r;
}
char x[100], y[100];
unsigned u;
int main() 
{
    while (2 == scanf("%s%s",x,y) && x[0] != '-') 
    {
        if (x[0] == '0') printf("%lun",count(y));
      else {
         sscanf(x,"%lu",&u);
         sprintf(x,"%lu",u-1);
         printf("%lun",count(y)-count(x));
      }
   }
     return 0;
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Dreams孤独患者 Skip
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist