每日一题----2022.12.28

目录

题目

样例

题解 

注意事项


题目


给定两个正整数 n 和 k,求从 1 到 n 这 n 个正整数的十进制表示中 k 出现的次数。

输入格式

共一行,包含两个整数 n 和 k。

输出格式

输出一个整数,表示答案。

数据范围

1≤n≤10^6
1≤k≤9


样例



题解 


#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;

    int res = 0;
    for (int i = 1; i <= n; i ++ )
        for (auto c: to_string(i))
            if (c - '0' == k)
                res ++ ;

    cout << res << endl;
    return 0;
}

 

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
typedef long long ll;
using namespace std;
int main()
{
	ll a, b,ans=0; cin >> a >> b;
	
	for (int i = 1; i <= a; i++)
		for (auto c : to_string(i))
			if (c == b + '0')ans++;
	cout << ans;
}
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;

    int res = 0;
    for (int i = 1; i <= n; i ++ )
        for (int j = i; j; j /= 10)
            if (j % 10 == k)
                res ++ ;

    cout << res << endl;
    return 0;
}


注意事项


1.字符可以通过-‘0’转成数字,数字可通过+‘0’转成字符。

2.to_string用法:

 3.for(auto a:b)循环:

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值