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]中一定存在一个素数
随机推荐
- Java作业
1.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff.具体要求如下: (1)Person类中的属性有:姓名name(String类型),地址 ...
- JQuery.Gantt(甘特图)开发
一.简介 JQuery.Gantt是一个开源的基于JQuery库的用于实现甘特图效果的可扩展功能的JS组件库. 二.前端页面 2.1 资源引用 首先需要将下载到的源码中的CSS.IMG.JS等资源放入 ...
- 常用的Linux系统调用命令
常用的Linux系统调用命令 下面一些函数已经过时,被新的更好的函数所代替了(gcc在链接这些函数时会发出警告),但因为兼容的原因还保留着,这些函数将在前面标上“*”号以示区别. 一.进程控制 ...
- 实现类似微信的延迟加载的Fragment——LazyFragment
参考微信,使用ViewPager来显示不同的tab,每个tab是一个Fragment, 假设有3个tab,对应的fragment是FragmentA.FragmentB.FragmentC 需要实现的 ...
- Part 59 to 60 Difference between Convert ToString and ToString,String and StringBuilder
Part 59 Difference between Convert ToString and ToString Part 60 Difference between String and Strin ...
- @@ROWCOUNT (Transact-SQL)
@@ROWCOUNT (Transact-SQL) 返回受上一语句影响的行数.如果行数大于 20 亿,请使用 ROWCOUNT_BIG. Transact-SQL 语法约定 语法 @@ROWCOUNT ...
- iOS 数据模型 的 一般设计
- List.Select按字符串选择属性
不知道大家有没有遇到这样的情况:List使用Lambda表达式的时候,想要选择项的某个属性列. 例如,选择编号ID: var idList=list.Select(o=>o.ID).ToList ...
- OC5_Block
// // main.m // OC5_Block // // Created by zhangxueming on 15/6/26. // Copyright (c) 2015年 zhangxuem ...
- (转)IIS7 优化-网站请发并发数
1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...