请统计给定单位(l,r)所有整数中,数字2出现次数
你题目的解答代码如下:
#include <iostream>
using namespace std;
int main()
{
int l,r,i,t,count=0;
cin >> l >> r;
for(i=l; i<=r; i++)
{
t = i;
while(t>0)
{
if(t%10==2)
count++;
t/=10;
}
}
cout << count;
return 0;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
#include <stdio.h>
int main()
{
int l,r,i,k,y;
while(scanf("%d%d",&l,&r)!=EOF)
{
k=0;
for(i=l;i<=r;i++)
{
y=1;
while(1)
{
if(i/y%10==2)
k++;
y*=10;
if(i/y==0)
break;
}
}
printf("%d\n",k);
}
return 0;
}