题目:P1980 [NOIP2013 普及组] 计数问题 试计算在区间1 到n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次? 在1到11 中,即在1、2、3、4、5、6、7、8、9、10、11 中,数字1 出现了4 次。
#include<bits/stdc++.h>
using namespace std;
int ans;
void look(int a,int b)
{
int temp;
while(a!=0)
{
temp=a%10;
if(temp==b)
{
ans++;
}
a/=10;
}
}
int main()
{
int n,x;
cin>>n>>x;
for(int i=1; i<=n; i++)
{
look(i,x);
}
cout<<ans;
return 0;
}
**学习心得**:
通过void构造了一个find函数,一开始还以为是C语言里设定好的find函数。
**位数运算**
**关键: %10除模取余得出位数,/10进入下一位**
**注意:每一位相等都要计入,所以要进入循环(以判断的那个数被除到0为止)**
*菜鸟一枚,若有错误请指出*。
题解引用自:https://blog.csdn.net/qq_43371778/article/details/106346415