ACM题目_小学生统计数字

来源:互联网 发布:班旗班徽设计软件 编辑:程序博客网 时间:2024/04/30 13:34

这里写图片描述

解题思路:
循环分解数字+计数

C++解决方案

#include"stdio.h"#include"iostream"using namespace std;int split(int val){    int sum = 0;    while(val>0)    {        if(val%10==2)            sum++;        val/=10;    }    return sum;}int count(int l,int r){    int count = 0;    for(int i=l;i<=r;i++){        count += split(i);    }    return count;}int main(){    int a;    int b;    cin>>a>>b;    cout<<count(a,b);}