洛谷题单3-P1980 [NOIP 2013 普及组] 计数问题-python-流程图重构

题目描述

试计算在区间 1 1 1 到 n n n 的所有整数中,数字 x x x( 0 ≤ x ≤ 9 0\le x\le9 0≤x≤9)共出现了多少次?例如,在 1 1 1 到 11 11 11 中,即在 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 1,2,3,4,5,6,7,8,9,10,11 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 1 1 出现了 4 4 4 次。

输入格式

2 2 2 个整数 n , x n,x n,x,之间用一个空格隔开。

输出格式

1 1 1 个整数,表示 x x x 出现的次数。

输入输出样例

输入

复制代码
11 1

输出

复制代码
4

说明/提示

对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 1 0 6 1\le n\le 10^6 1≤n≤106, 0 ≤ x ≤ 9 0\le x \le 9 0≤x≤9。

方式

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        num_1, num_2 = map(int, input().split())
        return num_1, num_2

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 11, 1

    @staticmethod
    def solution(num_1, num_2):
        count = 0

        for i in range(1, num_1 + 1):
            i = str(i)
            count += i.count(str(num_2))

        print(count)


oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    num1, num2 = oi_test()
    # num1, num2 = oi_input()
    solution(num1, num2)

流程图

开始 读取输入num1, num2 初始化计数器count=0 循环i从1到num1 将i转换为字符串str_i 统计str_i中num2字符出现次数 累加到count 循环结束 输出count值 结束

相关推荐
小徐不徐说2 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ZhengEnCi2 小时前
模型训练完整流程:从数据准备到模型上线的 10 个关键步骤(全文可跑)
后端·算法
ragnwang2 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go2 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
Wang's Blog2 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3272 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
weixin_307779133 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
Dxy12393102163 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷3 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang4 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain