PAT甲级1049 Counting Ones (30分) ***感觉不简单,使用递归

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 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 (≤2
​30
​​ ).

Output Specification:
For each test case, print the number of 1’s in one line.

Sample Input:
12
Sample Output:
5

最简单直接循环,超时肯定的

最简单的思路,从1到N,转成string ,每一位判断,但测试点肯定会超时

   long N;
    cin>>N;
    long sum=0;
    for(int i=1; i<=N; i++)
    {
        string str=to_string(i);
        for(int q=0; q<str.size(); q++)
        {
            if(str[q]=='1')
                sum++;
        }
    }
    cout<<sum<<endl;

考虑N位数字的所有1总和,写一半没思路了

然后我又想,通过排列组合,计算出所有N位的数字中,有几个1,简化一下,结果没想到好办法处理 10000到数字N之间的1的数目如何求
下面是部分代码,看以后有好想法没

//这个是实现排列,比如C52 =10
long cpi(int posNum,int i)
{
    long cpi=1;
    for(int q=0; q<i; q++)
    {
        cpi*=posNum-q;
    }

    for(int q=1; q<=i; q++)
    {
        cpi/=q;
    }
    return cpi;
}

long get(int posNum,int firstZero)
{
    if(posNum==1)
        return 1;
    if(firstZero==1)
    {
//        第一个数字可以为0,那么就是共有posNum个位置,
//        那么1的数目从 1 到posnum个 ,现在假设有i个1
//        CposNum i  抽选出哪些位置放置1,其他位置posnum-i个 每个位置9个选择除了1
        long sum=0;
        for(int i=1; i<=posNum; i++)
        {
            //C posNum i
            int get=cpi(posNum,i);
            sum+=cpi(posNum,i)*pow(9,posNum-i)*i;//关键是后面的乘以i
        }
        return sum;
    }
    else
    {
        long sum=0;
        sum+=get(posNum-1,1)*8;// 第一位是23456789的情况
        long sum2=0;
        for(int i=1; i<=posNum-1; i++)
        {
            //C posNum i
            sum2+=cpi(posNum-1,i)*pow(9,posNum-i-1)*(i+1);//关键是后面的乘以i
        }
        sum2+=pow(9,posNum-1);
        return sum+sum2;
    }
}

对位数进行递归

最后AC了的代码是考虑递归,对数字n的每一位进行递归,知道最后一位就确定了一个数字,只要递归时候把之前位的1个数当做参数递归就好。最初想到这个思路,只是感觉可以用递归,只要我们适当的保存之前的数字1的个数,然后在数字n的最后一位递归的时候(其实也就确定了一个小于N的数字了),对含有1的个数,return 返回出去。
int df(int pos,int sum1) //pos是数字的那个位置,sum1是在pos之前的位置有几个1
但是实现起来有个问题,就是比如数字n=789654

对于数字的第一个位置的数字7,我们能从i=0到7 循环一下,i=0 2 3 4 5 6 其实是一样的,i=1是特殊的情况,递归时候需要sum1+1,i=7也是特殊情况,因为我们不能让数字超过N,这就限制了第二位的选择,即第一位确定是7了第二位最高就是8,但是第二位0 2 3 4 5 6 其实是一样的,第二位是1的情况进行第三位递归时候还是需要sum1+1,第二位是8了,又要限制第三位的选择了,以此类推。

但是只要我们再pos位置的数字小于N在pos位置的数字,在pos+1位置的递归就是无数字限制了,所以我就又写了一个递归函数df2处理这种无数字限制的情况。

代码放下面了,我感觉我下次也不一定能写出这个代码。。。。。

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
string str;
int df2(int pos,int sum1)
{

    if(pos==str.size()-1)
    {
        int posNumber=str[pos]-'0';

        long sum=0;
        for(int i=0; i<=9; i++)
        {
            if(i==1)
                sum+=sum1+1;
            else
                sum+=sum1;
        }
        return sum;
    }

    int posNumber=str[pos]-'0';
    long sum=0;
    sum+=df2(pos+1,sum1+1);
    sum+=df2(pos+1,sum1)*9;
    //因为已经确定现在的数字比N小了,数字的选择也就无限制了

    return sum;

}

int df(int pos,int sum1)
{

    if(pos==str.size()-1)
    {
        int posNumber=str[pos]-'0';

        long sum=0;
        for(int i=0; i<=posNumber; i++)
        {
            //这里其实就是判断某个数字有几个1 ,一直递归回去
            if(i==1)
                sum+=sum1+1;
            else
                sum+=sum1;
        }
        return sum;
    }

    int posNumber=str[pos]-'0';
    long sum=0;
    for(int i=0; i<=posNumber; i++)
    {
        
        //首先判断是否是n在该位置的值,
        //比如n=789,i=7的时候就不能调用df2这个无限制的计算方法
        if(i==posNumber)
        {
            if(i==1)
                sum+=df(pos+1,sum1+1);
            else
                sum+=df(pos+1,sum1);
        }

        else
        {   //只要不是n在该位置的值,就是无限制的了,按照0-9情况考虑
            if(i==1)
                sum+=df2(pos+1,sum1+1);
            else
                sum+=df2(pos+1,sum1);
        }

    }
    return sum;

}


int main(void)
{
    long N=99;
    cin>>N;
    str=to_string(N);

    cout<<df(0,0);

}


纯数学方法,参考柳神

https://blog.csdn.net/liuchuo/article/details/52264944

牛客方法也很多

https://www.nowcoder.com/questionTerminal/eb1e0fc83a25461e93b8bf2039e871b9?toCommentId=6269527

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值