51nod 1009 数字1的数量
来源:互联网 发布:linux 内核下载 编辑:程序博客网 时间:2024/05/17 03:12
数论问题
引用一段别人分析的东西吧
1位数的情况:
大于等于1的时候,有1个,小于1就没有。
2位数的情况:
N=13,个位数出现的1的次数为2,分别为1和11,十位数出现1的次数为4,分别为10,11,12,13,所以f(N) = 2+4。
N=23,个位数出现的1的次数为3,分别为1,11,21,十位数出现1的次数为10,分别为10~19,f(N)=3+10。
由此我们发现,个位数出现1的次数不仅和个位数有关,和十位数也有关,如果个位数大于等于1,则个位数出现1的
次数为十位数的数字加1;如果个位数为0,个位数出现1的次数等于十位数数字。而十位数上出现1的次数也不仅和
十位数相关,也和个位数相关:如果十位数字等于1,则十位数上出现1的次数为个位数的数字加1,假如十位数
大于1,则十位数上出现1的次数为10。
3位数的情况:
N=123
个位出现1的个数为13:1,11,21,…,91,101,111,121
十位出现1的个数为20:10~19,110~119
百位出现1的个数为24:100~123
我们可以继续分析4位数,5位数,推导出下面一般情况:
假设N,我们要计算百位上出现1的次数,将由三部分决定:百位上的数字,百位以上的数字,百位一下的数字。
如果百位上的数字为0,则百位上出现1的次数仅由更高位决定,
比如12013,百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,共1200个。
等于更高位数字乘以当前位数,即12 * 100。
如果百位上的数字大于1,则百位上出现1的次数仅由更高位决定,比如12213,
百位出现1的情况为100~199,1100~1199,2100~2199,…,11100~11199,12100~12199共1300个。
等于更高位数字加1乘以当前位数,即(12 + 1)*100。
如果百位上的数字为1,则百位上出现1的次数不仅受更高位影响,还受低位影响。例如12113,
受高位影响出现1的情况:100~199,1100~1199,2100~2199,…,11100~11199,共1200个,但它还受低位影响,
出现1的情况是12100~12113,共114个,等于低位数字113+1。
//@auther zhou//@Number 201408070203//@start time://@finish time:/*@此处注意:*//* 测试数据*/#include<iostream>#include<cstring>#include<vector>#include<cmath>#include<algorithm>using namespace std;int calcu1(int n){//暴力解 int ans=0;int temp=0,temp1=0;for(int i=1;i<=n;i++){temp=i;while(temp>=1){temp1=temp%10;if(!(temp1-1)){//cout<<"temp的值"<<temp<<"temp1的值"<<temp1<<endl;ans++;}//cout<<temp<<"temp除以10的结果是"<<temp/10<<endl;temp=temp/10;}}return ans;}int calcu2(int n){//算法解 int count = 0; int i = 1; int current = 0, after = 0, before = 0; while ((n / i) != 0) { current = (n / i) % 10; before = n / (i * 10); after = n - (n / i) * i; if (current > 1) count = count + (before + 1) * i; else if (current == 0) count = count + before * i; else if (current == 1) count = count + before * i + after + 1; i = i * 10; } return count;}int main(){int n;cin>>n;//cout<<calcu1(n)<<endl;cout<<calcu2(n)<<endl;//while(cin>>n) cout<<calcu1(n)<<endl;return 0;}
- 51nod 1009:数字1的数量
- 51NOD 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51 nod 1009 数字1的数量
- 51nod-【1009 数字1的数量】
- 51nod 1009 数字1的数量
- 51nod-1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51NOD 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51Nod 1009 数字1的数量
- 51nod 1009 数字1的数量
- 51Nod 1009 数字1的数量
- 51Nod 1009-数字 1 的数量
- SpringXML方式配置bean的懒加载lazy-init
- SpringXML方式配置bean的生命周期lifecycle
- Activity的四种启动模式
- SpringXML方式配置bean的自动装配autowire
- SpringXML方式配置bean的集合注入:list,map,properties
- 51nod 1009 数字1的数量
- SpringXML方式配置bean的生存范围Scope
- SpringXML方式给bean初始化属性值
- SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
- SpringAnnotation注解之@Resource
- hduoj 2030统计汉字
- SpringAnnotation注解之@Autowired
- SpringAnnotation之配置AnnotationXML文件