2013年普及组第一题(普通做法+数学做法)

20 篇文章 0 订阅
12 篇文章 0 订阅

题干在这里:https://www.luogu.org/problemnew/show/P1980
不要问我为什么是洛谷。。。

好吧,其实这道题没有什么好讲的,普通的模拟就直接可以过了
代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
int n,x;

int main()
{
    //freopen("count.in","r",stdin);
    //freopen("count.out","w",stdout);
    //这是比赛的时候要加的。。。
    int i,j,k,n,x,ans;
    cin>>n>>x;
    for(int i=1;i<=n&&(k=i);i++){
        int k=i;
        while(k>0){
            if(k%10==x)
                ans++;
            k/=10;
        }
    }
    cout<<ans<<endl ;
    return 0;
} 

其实,这道题可以加上一些数学的思维
首先考虑出现在个位的情况:每十个就出现一个了
其次考虑之后的情况:他的位数之后的数随意取就好了,前面仍是十个出现一个,两边得出的树相乘就可以了。
可能我讲的有点玄幻,直接上代码吧:

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    //freopen("count.in","r",stdin);
    //freopen("count.out","w",stdout); 
    int n,x,m=1,ans=0;
    cin>>n>>x;
    while(m<=n){
        int a=n/(m*10),b=n/m%10,c=n%m;
        if(x!=0){
            if(b>x)
                ans+=(a+1)*m;
            if(b==x)
                ans+=a*m+c+1;
            if(b<x)
                ans+=a*m;
        }
        else{
            if(b!=0)
                ans+=a*m;
            else
                ans+=(a-1)*m+c+1;
        }
        m*=10;
    }
    cout<<ans<<endl;
    return 0;
}

说实话,如果这道题还不会的话,兄弟,还是换一条路走吧。信息不适合你

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值