题解 | 数字统计

数字统计

https://www.nowcoder.com/practice/179d9754eeaf48a1b9a49dc1d438525a

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


//解题思路1
//拆分数字
//思路2 
//利用to_string函数把每个数字转成字符串
//然后查找子串"2"
int main() {
    int L,R,cnt=0;
    cin>>L>>R;

    for(int i=L;i<=R;i++)
    {
        string temp=to_string(i);
        size_t newpos=0,oldpos=0;
        while(newpos!=string::npos)//查找子串"2"
        {
            newpos=temp.find("2",oldpos);//从头开始查找
            if(newpos!=string::npos)cnt++;//找到cnt++
            oldpos=newpos+1;//更新下次查找的位置
        }
    }
    std::cout<<cnt<<std::endl;

}
// 64 位输出请用 printf("%lld")

两种思路

//解题思路1

//拆分数字

//思路2

//利用to_string函数把每个数字转成字符串

//然后查找子串"2"

#你觉得现在的大学教的大学有用吗##26届的你们有几段实习?#
全部评论

相关推荐

码农索隆:邮件那么小的内存,把邮箱都干满了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务