1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (<=230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
题目描述
#include<iostream>
#include<string>
#include<sstream>
#include<math.h>
using namespace std;
int CountOne(std::string num)
{
int len = num.length();
if(len == )
return ;
int fir = num[] - ''; if(len == && fir == )
return ;
if (len == && fir >= )
return ; int num1 = ,num2,num3;
if (fir == )
{
string re = num ,tem;
num.erase(num.begin());
tem = num;
num = re;
re = tem;
stringstream ss;
ss << re;
ss >> num1;
++ num1;
}
else if(fir > )
{
num1 = pow((double),len - );
}
num2 = (len -) * fir * pow((double),len-);
string tnum = num;
tnum.erase(tnum.begin());
num3 = CountOne(tnum);
return num1 + num2 + num3;
}
int main()
{
string num;
cin >> num;
printf("%d\n",CountOne(num));
return ;
}
1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)的更多相关文章
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT 1049 Counting Ones[dp][难]
1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...
- PAT 1049 Counting Ones [难]
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to coun ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组
题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明:初始化 nums1 和 nums2 的元素数量分别为 m ...
- 整数中1出现的次数(从1到n整数中1出现的次数)
整数中1出现的次数(从1到n整数中1出现的次数) 题目描述 求出1 ~ 13的整数中1出现的次数,并算出100 ~ 1300的整数中1出现的次数?为此他特别数了一下1 ~ 13中包含1的数字有1.10 ...
- 剑指Offer-31.整数中1出现的次数(从1到n整数中1出现的次数)(C++/Java)
题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- 对于大于等于3的整数n,在区间【n,3/2 * n】中一定存在一个素数
对于大于3的整数n,在区间[n,3/2 * n]中一定存在一个素数
随机推荐
- QQ互联登录回调路径错误redirect uri is illegal(100010)
QQ互联登录设置的路径设置
- Sharepoint 2013 安装部署系列篇 第三篇 -- 安装和配置网络负载均衡在前端web服务器
第一部分 系统集群安装 第二部分 SQL集群安装 第四部分 安装和配置sharepoint 场(三层拓扑部署) 接下来一步一步开始配置NLB吧, 以下开始讲解如何配置NLB集群作为sharepoint ...
- IOS版应用商店应用源码
app商店 swift版 用swift编写的 应用商店 支持iPad iPhone利用了ios8过渡动画 支持横竖屏操作 源码下载: http://code.662p.com/view/11384.h ...
- js正则表达式的验证示例
//验证邮箱的JS正则 <script type="text/javascript"> $(function() { $("#inputemail" ...
- python基础day2作业:购物车
#功能:1.可注册账号2.登录买家账号3.可查询编辑购物车里商品4.可以余额充值5.可提示余额不足6.购物车结算 #使用:1.第一次使用先注册账号填写账号金额2.账号金额信息保存在buyer_acco ...
- 关于document.write
document.write的用处 document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中 ...
- 'hibernate.dialect' must be set when no Connection avalable
'hibernate.dialect' must be set when no Connection avalable 当连接不可用时,必须要设置Hibernate方言 搜索大多数的解决办法是:首先查 ...
- Debug Intro
The ABAP Debugger is used tool to execute and analyze programs line by line. Using it we can check t ...
- Window Phone 8开发问题反思
项目开发有段时间了,进入了阶段测试.然而在测试过程中bug连连不断,在抱怨产品需求的坑爹.不合理之外,我也一直在反思为什么会出现这么多Bug. 首先,由于项目开发的两个人都是新手,在刚刚认识MVVM架 ...
- arm汇编指令总结(不断更新)
/** ****************************************************************************** * @author Maox ...