首页 > 其他分享 >【PTA】1049 Counting Ones

【PTA】1049 Counting Ones

时间:2022-12-22 15:12:14浏览次数:39  
标签:10 right cur 1049 cdots Ones ans Counting left

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

Solution

数学

设\(N=d_nd_{n-1}d_{n-2}\cdots d_i \cdots d_2d_1\),对于每一个数位\(d_i\),计算当前数位可能出现1的次数并累加起来即可获得答案。

假设当前位为\(cur \rightarrow d_i\),记其左边的数\(left=d_nd_{n-1}d_{n-2}\cdots d_{i+1}\),右边的数为\(right=d_{i-1} \cdots d_3d_2d_1\)。使用一个变量\(a\)来记录当前位是个位,十位还是百位类推。根据数位的变化规律,有如下结论:

  • \(cur==0\),\(ans+=left*a\)。当高位的数值从\(0\)变换到\(left-1\)时,在\(cur\)位处会出现\(left\)次1,因为对于一个数\(d_nd_{n-1}d_{n-2}\cdots d_i XXXX\)来说,前缀一旦确定,那么\(d_i\)只会在当前前缀的数字下出现一次。但是由于\(cur==0\),所以在以\(left\)为前缀的数中,\(right\)从\(0\)到\(999\cdots\),因此对于一个确定的\(left\),\(d_i\)出现1的次数有\(a\)次,因此一共有\(left*a\)次1出现。
  • \(cur>1\),\(ans+=(left+1)*a\)。由于当前位的值大于1,因此高位的数值可以从\(0\)变换到\(left\),在\(cur\)位处会出现\(left+1\)次\(1\).
  • \(cur==1\),\(ans+=left*a+right+1\),\(left*a\)的来源与\(cur==0\)的情况一致。由于\(cur==1\),因此当左侧前缀\(left\)取到最大时,右侧的后缀的范围不是从0~999...,而是从\(0\)到\(right(d_{i-1} \cdots d_3d_2d_1)\),因此这里还有\(right+1\)个1.
#include<bits/stdc++.h>
long long ans=0;

int main(){
    int N;
    std::cin>>N;
    int left=0,right=0,cur=1,a=1;
    int ans=0;

    while(N/a){
        left=N/(a*10);
        right=N%a;
        cur=N/a%10;
        if(cur==0) ans+=left*a;
        else if(cur==1) ans+=left*a+right+1;
        else if(cur>1) ans+=(left+1)*a;
        a*=10;
    }

    std::cout<<ans;
    return 0;
}

标签:10,right,cur,1049,cdots,Ones,ans,Counting,left
From: https://www.cnblogs.com/yjx-7355608/p/16998762.html

相关文章

  • [LeetCode] 1753. Maximum Score From Removing Stones
    Youareplayingasolitairegamewith threepiles ofstonesofsizes a​​​​​​, b,​​​​​​and c​​​​​​respectively.Eachturnyouchoosetw......
  • counting the number of 1-bits
    Chapter5.CountingBits-Hacker’sDelight,SecondEdition[Book](oreilly.com)TheIBMStretchcomputer(about1960)hadameansofcountingthenumberof1......
  • matlab/simulink中如何使用ones/zeros(变量,变量)不报错
    1.脚本声明变量%使用脚本声明结构体变量m并创建simulink.busclcclearm.a1=[333];busInfo=Simulink.Bus.createObject(m);2.在simulink中使用ones报......
  • Counting Elements - MaxCounters
    YouaregivenNcounters,initiallysetto0,andyouhavetwopossibleoperationsonthem:increase(X) −counterXisincreasedby1,maxcounter −all......
  • Counting Elements - PermCheck
    QuestionAnon-emptyarrayAconsistingofNintegersisgiven.A permutation isasequencecontainingeachelementfrom1toNonce,andonlyonce.Forexam......
  • Counting Elements - FrogRiverOne
    QuestionAsmallfrogwantstogettotheothersideofariver.Thefrogisinitiallylocatedononebankoftheriver(position0)andwantstogettotheop......
  • CF 1722E Counting Rectangles(前缀和)
    题目分析(摘自这篇博客,原博主关于包含的定义有错误,在此处做了修改)给出一个长度为1e5的矩阵序列和1e5次询问,每次询问给出两个上下界矩阵,保证大矩阵包含小矩阵,请输出矩阵序......
  • 数数字(Digit Counting)
    DigitCountingTimelimit:3.000secondsTrungisboredwithhismathematicshomeworks.Hetakesapieceofchalkandstartswritingasequenceofconsecutiveint......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • [AGC005D] ~K Perm Counting
    [AGC005D]~KPermCounting智慧转化,但不是很难想到。首先考虑容斥,设\(f_i\)表示强制\(i\)个位置\(|P_i-i|=k\)的方案数,那么答案就为\(\sum_{i=0}^nf_i(n-i)!\),......