首页 > 其他分享 >PAT_A1049 Counting Ones【困难】

PAT_A1049 Counting Ones【困难】

时间:2023-10-27 11:44:54浏览次数:29  
标签:10 Counting PAT int Ones ans 12 now

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<bits/stdc++.h>
using namespace std;
int n, ans;
int main(){
	scanf("%d", &n);
	int r = 0, l = 0, a = 1, now;
	while(n/a != 0){
		l = n/(a*10);
		now = n/a%10;
		r = n%a;
		if(now == 0) ans += (l * a);
		else if(now == 1) ans += l * a + r + 1;
		else ans += (l+1) * a;
		a *= 10;
	}
	printf("%d\n", ans);
	return 0;
}

总结

数学问题/简单数学 需要严格推理,具体见算法笔记上机指南p199.

每次迭代,记录当前位出现1的个数;对当前位的数,分<1, ==1, >1三种情况讨论。

标签:10,Counting,PAT,int,Ones,ans,12,now
From: https://www.cnblogs.com/Afinis/p/17791920.html

相关文章

  • PAT_A1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10segments:(0.1)(0.1,0.2)(0.1,0.2,0.3)(0.1,0.2,0.3,0.4)(0.2)(0.2,0.3)(0.2,0.3,0.4)......
  • PAT 甲级【1012 The Best Rank】
    本题用java极容易超时,提交了好几次才成功另外9088777750,名次应该是12335,不是12334 importjava.io.*;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizer......
  • PAT_B1003 我要通过!
    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送——只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。得到“答案正确”的条件是:字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;任意形如 xPA......
  • gerrit 将他人改动直接 打patch到自己代码上
    原文:https://blog.csdn.net/qq_21438461/article/details/131362485 在Linux中,patch命令用于将补丁文件应用到源代码文件中,从而实现对源代码的修改。patch命令的详细描述如下:patch命令用于将补丁文件应用到源代码文件中,以实现对源代码的修改。补丁文件通常是由开发者或者社区......
  • ExcelPatternTool 开箱即用的Excel工具包现已发布!
    目录ExcelPatternTool功能特点:快速开始使用说明常规类型高级类型Importable注解Exportable注解IImportOption导入选项IExportOption导出选项单元格样式StyleMapping样式映射使用数据库作为数据源示例Sample1:不同类型字段导出Sample2:高级类型导入和导出Sample3:员工健康体检工具已知......
  • PAT_A1101 Quick Sort
    Thereisaclassicalprocessnamed partition inthefamousquicksortalgorithm.Inthisprocesswetypicallychooseoneelementasthepivot.Thentheelementslessthanthepivotaremovedtoitsleftandthoselargerthanthepivottoitsright.Given ......
  • Qt CustomDashLine会对范围外Path自动裁剪问题
    在使用QPainter进行绘制时发现问题。当直接使用QPen进行绘制自定义虚线时会出现一个问题:当绘制的Path遇到界面进行裁剪时,此时虚线线型将会省略裁剪的那一部分,导致自定义虚线在移动以及放大时会自动修改位置。解决办法:直接使用QPainterPathSkroke。问题描述......
  • PAT 甲级【1011 World Cup Betting】
    importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizerin=ne......
  • PAT 甲级【1010 Radix】
    本题范围long型(35)^10枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举代码如下importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassMain{@SuppressWarnings("unchecked")pu......
  • Spring MVC入口Servlet详解(HttpServletBean,FrameworkServlet,DispatcherServlet )
    SpringMVC中DispatcherServlet前端控制器是web服务器的入口,那么它是怎么样进行初始化的,是怎么样进行工作?继承关系1.HttpServletBean主要做一些初始化的工作,将web.xml中配置的参数设置到Servlet中。比如servlet标签的子标签init-param标签中配置的参数。2.FrameworkServlet将Serv......