题目

Let’s call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbers.

For a given number n, find the number of ordinary numbers among the numbers from 1 to n.

输入格式

The first line contains one integer t (1≤t≤104). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤109).

输出格式

For each test case output the number of ordinary numbers among numbers from 1 to n.

数据范围

10的九次方

样例输入

6
1
2
3
4
5
100

样例输出

1
2
3
4
5
18

题意

多组输入输出
找到1-n的范围内有几个类似111这样的,每一位都相同的数

思路

  • 1.1-10里有9个
  • 2.11-100里有9个
  • 3.101-1000里有九个
  • 4.以此类推…
  • 5.先找这个数的范围,用while分解每一位,每分解一位就+9,cnt初始化为-9,因为比如100,其实只到第二个范围,如果不减9就到第三个范围了
  • 6.然后给的数万一不是一个正好的整数,比如23456,我们根据上一步操作只找到了1-10000里的符合要求的数的数量,要找10001-23456里有几个符合要求的数,就用23456/11111,再加上之前的结果就是要求的数

坑点

  • 1.1
  • 2.3
  • 3.3

代码

原版

#include<bits/stdc++.h>
using namespace std;
int main()
{long long int n;cin>>n;while(n--){long long int a;cin>>a;if(a<10){cout<<a<<endl;}else{long long int cnt=-9;long long int x=a;long long int sum=1;//1000000000 long long int sum1=0;//111111111while(x>0){f=x%10;x/=10;sum=sum*10;sum1=sum1*10+1;cnt+=9;}//cout<<f<<endl;/*sum=sum/10*f;a-=sum;sum1/=10;cnt=cnt+a/sum1;*/cnt+=a/sum1;cout<<cnt<<endl;}}return 0;
}   

简化版1

#include<iostream>
int main()
{long long int n;scanf("%lld",&n);while(n--){long long int a;scanf("%lld",&a);if(a<10){printf("%lld\n",a);}else{long long int cnt=-9,x=a,sum1=0;while(x>0){x/=10;sum1=sum1*10+1;cnt+=9;}cnt+=a/sum1;printf("%lld\n",cnt);}}return 0;
}

最终简化版(length:311 , time:31ms)

#include<iostream>
int main()
{long long int n;scanf("%lld",&n);while(n--){long long int a;scanf("%lld",&a);if(a<10) printf("%lld\n",a);else{long long int cnt=-9,x=a,sum1=0;while(x>0){x/=10;sum1=sum1*10+1;cnt+=9;}cnt+=a/sum1;printf("%lld\n",cnt);}}return 0;
}

总结

思考得出

B. Ordinary Numbers相关推荐

  1. Codeforces791:Ordinary Numbers题解

    Codeforces Round#791(Div.3) B.Ordinary Numbers Ordinary Numbers题解 Codeforces Round#791(Div.3) B.Ordi ...

  2. sort command

    http://hi.baidu.com/zealot886/item/a32241d0b7b2ee5fd73aae33 http://www.cnblogs.com/51linux/archive/2 ...

  3. Codeforces Round #719 (Div. 3)/ Codeforces Round #720 (Div. 2)

    A. Do Not Be Distracted! 题意: 一件事情一但开始,只能做完才能做别的事,当出现一件事不连续出现时,教师会怀疑 题目: Polycarp has 26 tasks. Each ...

  4. SICP:Building Abstractions with Data

    为什么80%的码农都做不了架构师?>>>    2.1 Introduction to Data Abstraction our programs should use data i ...

  5. 如何创建SQL Server报告服务(SSRS)报告

    SQL Server Reporting Services (SSRS) is part of Microsoft SQL Server services since version 2000. If ...

  6. 本世纪最好的NSA!

    NONSTANDARD ANALYSIS By DR. J. PONSTEIN With love, love, love to those five women, who caressed me. J ...

  7. 无穷小进课堂,历史在召唤

    无穷小进课堂,历史在召唤 当今,在国内微积分教科书中,对无穷小理论的误解与偏见,比比皆是,不胜枚举. 为正视听,我们特别推荐本文附件文字(无穷小理论简介),供给读者参阅袁萌  陈启清 7月9日 附件: ...

  8. 为了数学的明天,,穿越时空,重返南大(III)-

    为了数学的明天,,穿越时空,重返南大(III)     进入二十一世纪,非阿基米德数学(比如:含有无穷小的连续统)逐渐兴起,我们用该如何面对?     这是一个基本问题,必须彻底搞清楚,事实求是.   ...

  9. Law of continuity

    The law of continuity is a heuristic principle introduced by Gottfried Leibniz based on earlier work ...

最新文章

  1. mysql 字段扩容_解决DB2事物日志满、扩充表字段长度和表空间的命令
  2. CSS综合复习笔记 01
  3. dos中的for命令简单使用
  4. Linux运行Django项目远程访问时报错:Invalid HTTP_HOST header: '***.***.*.*:8000'.
  5. ecshop 手机版的php代码在哪里,PHP 在ecshop上集成 手机网页支付_php
  6. Python问题记录
  7. 使用mysql数据库与go进行交互
  8. Android SDK Setup如何使用?
  9. Android OpenGL ES(十二):三维坐标系及坐标变换初步 .
  10. 蓝桥杯c语言用什么编译软件,蓝桥杯c语言软件大赛
  11. aria2c 官方手册中文翻译版
  12. linux程序设计第四版中文pdf下载地址
  13. python 打卡记录代码_利用Python实现对考勤打卡数据处理的总结
  14. 【图像处理算法常用数据集】整理第二弹
  15. 服务器管理口安装系统,管理口安装服务器操作系统
  16. 使用jstack查看线程
  17. 使用阿里云dns+oss+cdn+ssl发布静态网站
  18. tensorflow 学习率的下降策略
  19. 新年新气象 每天一个好心情
  20. 搜索引擎优化是什么,搜索引擎优化应该怎么学?

热门文章

  1. 这段代码,c 1秒,java 9秒,c# 14秒,而python。。。
  2. 大数据24小时:腾讯杀入无人驾驶市场,百度安全联合成立“OASES智能终端安全生态联盟”
  3. top与free命令详解
  4. vue根据不同权限显示图片_vuex根据不同的用户权限展示不同的路由列表
  5. CDA_Level 1_学习笔记2
  6. 国内网页无法加载reCAPTCHA解决方案
  7. Shell 加法运算的几种写法
  8. 杰罗姆鲍威尔 是犹太人吗_采访:杰罗姆·卢维尔(JéromeLouvel)关于Restlet
  9. Java 周日期计算工具,获取当前第几周,按周数获取周一至周日,按日期获取周一至周日
  10. 【翻译】听觉刺激增强深度睡眠,实现“熟睡自由”