PAT 1049 Counting Ones

PAT-A-1049 Counting Ones (30 分) 数位DP C++题解 1049 Counting Ones (30 分) 题目传送门:1049 Counting Ones (30 分) 一看到这道题就知道考的是数位DP,倔强的我不想找模板~~,自己写才有成就感。昨晚临睡觉前开始写的,今天早起继续写,居然一交就AC了,呵呵,开心 阅读详情

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.

本题的题意也是十分明确的,就是给定一个小于等于2^30的正整数,要求计算出1~n中,1出现的次数。

小编一开始也摸不着头脑,之后呢,通过猜想假设,1出现的次数存在一定特性,先遍历出些小规模的数据,

最后得到了一个结论:

设n由abcdefg组成,1的个数由sum来计数,若a=1,sum+=bcdefg+1+(1~bcdefg中1出现的次数);

否则sum+=n-bcdefg+a*(n-1位数中1出现的总次数)+(1~bcdefg中1出现的次数)。

参考代码如下:

#include<stdio.h>
#include<math.h>
int sum(int n,int a[])
{
	int num=0,count=0,x,y=n;;
    while(y){count++;y/=10;}
    if(count==1)return 1;//当n为个位数时返回次数1 
    x=(int)pow(10.0,count-1);
	num+=(n/x)*a[count-1];
	if(n/x==1)
		num+=n%x+1;
	else
		num+=x;
	num+=sum(n%x,a);//进行递归计数 
	return num;
}
int main()
{
	int n,number,a[20];
	int i,x;
	for(i=1,x=1;i<10;i++)
	{
		a[i]=i*x;
		x*=10;
	}//i位数中总的1出现次数为a[i]
	while(scanf("%d",&n))
	{ 
		x=n;
		number=sum(n,a);//计算1出现次数总和
		printf("%d\n",number);
	}
	return 0;
}

总结:这种题目多数运用的是数论相关知识,小编就是缺乏对数论的了解,在做本题时用时过长,数论知识的扩充是必不可少的。有空得多看看相关书籍了=.=...

大语言模型应用指南:幻觉和偏见问题 大语言模型应用指南:幻觉和偏见问题 1. 背景介绍 1.1 大语言模型的发展历程 1.1.1 早期的语言模型 1.1.2 Tr 阅读详情

相关推荐

win10+conda+pytorch-gpu安装

pytorch 官网上只有linux和Mac的程序包,没有windows系统的,但是windows系统还是可以用pytorch的。  github: https://github.com/peterjc123/pytorch-scripts 首先要安装anaconda: 推荐清华镜像 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ ...

一摩尔自由的博客 1万+

PAT甲1049 Counting Ones (30)(30 分)

#include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; using namespace std; int a; int main() { scanf("%d",&amp;a); int left=a,right=0,exp=1; ...

一点点 238

考试遇到 Assistant API 考点时,该掌握哪些要点?

【摘要】聚焦阿里云大模型 ACP 认证中智能体 Assistant API 考点,我结合自学认证成功经验,分享复习攻略。先解析 assistants、threads 等五个核心组件作用与关系,点明各组件支持及不支持的方法,最后给出计算 Python 代码调用大模型次数的思路,助力考生攻克考点、顺利拿证 。

davidkorhenn的博客 494

PAT 1049. Counting Ones (30)(数位dp(记忆化搜索))

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

sakura_is_the_best的博客 372

FDTD脚本教程——结构的创建

文章目录结构的搭建创建长方体创建柱体多边形柱体(椭)圆柱体圆环柱自定义形状创建surface波导球棱锥 在lumerical中,可以使用脚本进行操作,可在右侧脚本编辑器运行,或者直接在下方shell输入。 结构的搭建 创建长方体 um = 1e-6; nm = 1e-7; addrect; set("name","myRect"); set("material","SiO2 (Glass) - Palik"); set("render type","wireframe"); # 默认是细节渲染,也就是det

战斗果冻的训练中心 2万+

谷歌Gemini模型免费API调用全攻略:从注册到实战(附代码示例)

本文详细介绍了如何免费调用谷歌Gemini模型的API,从注册获取API密钥到实战应用的全过程。内容涵盖环境准备、基础API调用、多模态处理、高级功能及生产环境实践,帮助开发者高效利用Gemini模型进行代码生成和复杂任务处理。附有代码示例,适合AI开发者和技术爱好者。

weixin_29212429的博客 1599

PAT 1049. Counting Ones (30)

1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task is simple: given any positive intege

FrozenShore 543

IgH EtherCAT 从入门到精通:第 4 章 网络设备驱动选型与配置

摘要:本章详细介绍了EtherCAT主站网络设备驱动的选型与配置。IgH EtherCAT Master提供Generic通用驱动和Native原生驱动两种方案:Generic驱动通过Linux Socket接口实现,兼容性强但实时性较差;Native驱动通过修改网卡驱动绕过内核协议栈,具有更低延迟和更小抖动。文章具体分析了e1000/e1000e/igb/igc等Intel网卡驱动以及r8169/8139too等Realtek驱动的配置方法,并提供了驱动选型建议。对于实时性要求高的场景,推荐使用Nativ

从事嵌入式linux android驱动开发,拥有全栈能力 304

PAT 1049 Counting Ones 数位dp

#include #include #include #include #include #include using namespace std; int bit[15],dp[15][15][2]; int nm,n; int solve(int nn,int up,int bd) { if(dp[nn][up][bd]!=-1)return dp[nn][up][bd]; if(nn =

coding my life 589

CryoEM - 冷冻电镜 基于深度学习的 从头重构(Ab-initio Reconstruction) 开源项目 教程

冷冻电镜(CryoEM) 是一种成像方式,为蛋白质和其他生命基本组成部分的动态,提供独特的观察方式。从数百万噪声和随机取向的二维投影中,以计算效率高的方式,联合估计生物大分子的姿态、三维结构和构象异质性。

AGI 1554

ubuntu下安装多版本cuda及版本切换教程

首先,从官网下载所需版本的cuda安装包,下载链接。选择对应版本后,会跳转到该版本的配置信息页面。比如我是ubuntu20.04系统,已经安装好了cuda10.0,但是现在还需要用到cuda9.0。那么,在选定版本后,配置信息如下图所示(由于Version选项中没有20.04,因此我们随便选一个即可,这里选的16.04,这一选项不会影响后面的安装),Installer Type选择为runfile(因为使用deb文件进行安装的话,可能会将已经安装的较新的显卡驱动给替换掉),选择好之后点击Base Insta

zeeq的博客 3万+

pat 1049

做完发现好像做麻烦了,,直接组合一位一位求。。。 我用数位dp弄的。。。。。。。 /************* *Author:chanjun *email:15755396353@163.com *************/ #include #include #include #include #include #include #include #include

醉江楼的专栏 790

手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程

手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 4443

PAT A1049. Counting Ones (30)

原文链接: PAT A1049. Counting Ones (30) ...

阿豪 122

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【PAT甲级 - C++题解】1049 Counting Ones

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 798

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

PAT.1049. Counting Ones (30)

原题地址:https://www.patest.cn/contests/pat-a-practise/1049 题目原文: Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task is simple: given any positive integer

Mloza的博客 401

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

pat 1049 Counting ones

http://pat.zju.edu.cn/contests/pat-a-practise/1049 题目很有意思,感觉机试的时候出这种题目还是很好的,直接暴力就可以拿到20多分,哈哈 看《编程之美》上的讲解,拿出代码跟大家分享一下(具体的分析过程大家可以去看看编程之美的2.4数字之美) 贴一下可以AC的代码: 1 2 3 4 5 6 7 8 9 10

u014098060的专栏 685

[PAT A1049]Counting Ones

[PAT A1049]Counting Ones 题目描述 1049 Counting Ones (30 分)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...

欢迎来到我的编程小屋 786

[PAT-A 1049]Counting Ones

题目大意: 给出一个数字n(n<=2^30),求1-n里面出现1的个数。 样例中在1-12内出现的1的数字又1,10,11,12,总计5个1。 思路: 如果暴力一个一个枚举,必然超时,应当从右向左拆分数字,统计每个位出现1的个数,最后累加 记当前位数字now,now所处位数为a,ans为结果 1)如果now=0,则ans+=left*a. 2)如果now=1,则ans+=left*a+1...

RegulusF的博客 218

PAT甲级1049——Counting Ones

#include<iostream> #include<string> #include<math.h> using namespace std; int N; int ones[10]={0}; //ones[i]表示10的i次方以内(不包括自身),有多少个1; int ans=0; void get_one_num(int idx){ int tmp=p...

qq_36510764的博客 232

【PAT 1049】 Counting Ones 数学规律

1049. Counting Ones (30) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task is simple: given any positive intege

天堂人间 5225

PAT 甲级 1049  Counting Ones

1049 Counting Ones (30 point(s)) 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, give...

漫浸天空的雨色的备忘录~ 621

1049 Counting Ones (PAT甲级)

我和柳婼的基本思路是一致的,但我用了string导致代码复杂了很多……柳婼的解法简洁许多。等我有空再按她的思路写一遍吧。

linh2006的博客 283

PAT A1049-Counting Ones

PAT A1049-Counting Ones 题目 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 bei...

weixin_41515197的博客 405

1049 Counting Ones——PAT甲级(纯解释)

其他now>=2时,就是在now==0的基础上,即左侧0~left-1可以多考虑letf,即0~left,例如121,如果是101的话只能考虑010,011,012...019十个,但是121可以多考虑110,111,112...119这十个,得益于十位数超过1,而131,141,151...191,在十位数上1的个数上,与121相同。比如数字201,对于十位数0,那么此时十位数上数字为2*10,即010,011,012,013...019,110,111,112...,119,共2*10个。

flyidj的博客 474
上一篇: linux基础学习笔记(持续更新ING)
下一篇: PAT 1010. Radix (25)
chamsu
博客等级 码龄14年 78粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值