解题思路:
循环分解数字+计数
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);
}

本文介绍了一种通过编程方式解决特定范围内数字中特定数字出现次数的问题。使用C++实现了一个简单的程序,该程序能够计算从一个整数到另一个整数范围内所有数字中指定数字出现的总次数。
13万+

被折叠的 条评论
为什么被折叠?



