请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。
比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,在数22中出现2次,所以数字2在该范围内一共出现了6次。
请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。
比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,在数22中出现2次,所以数字2在该范围内一共出现了6次。
输入共1行,为两个正整数L和R,之间用一个空格隔开。
输出共1行,表示数字2出现的次数。
2 22
6
2 100
20
1≤L≤R≤10000。
#include<stdio.h>
int Count(int a, int b)
{
int i = 0;
int sum = 0;
for(i=a;i<=b;i++)
{
int z = i;
int g = 0;
while(z)
{
g = z %10;
if(g==2)
{
sum++;
}
z = z /10;
}
}
return sum;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d",&a,&b);
int ret= Count(a,b);
printf("%d",ret);
return 0;
} #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int a, b, can = 0, n = 0, ci = 0;
scanf("%d %d", &a, &b);
//将检查循环重复在指定范围内次
for (int x = a; x <=b; x++) {
n = x;
//检查每个数位是否等于2 是则次数变量加一 当数为零则跳出循环
for (int y = 0; y < 10; y++) {
if (n == 0) {
break;
}
can = n % 10;
if (can == 2) {
ci++;
}
n /= 10;
}
}
printf("%d", ci);
return 0;
} #include <stdio.h>
int count_num(int a, int b){
int count = 0;
int tmp;
for (int i = a; i <= b; i++) {
tmp = i; //防止i变化,用临时变量进行后续操作
while (tmp > 0){
if (tmp == 2){
count++;
break;
}
if (tmp != 2){
if (tmp % 10 == 2){
count++;
tmp = tmp / 10;
}
else{
tmp = tmp / 10;
}
}
}
}
return count;
}
int main() {
int a, b;
scanf("%d %d",&a,&b);
int ret = count_num(a,b);
printf("%d",ret);
return 0;
}
//
// Created by Wilbur Lee on 11/7/2024.
//
#include <stdio.h>
//函数名的意思是2出现的次数
int Number2OfOccurrences(int L, int R)
{
int count = 0; //记录二出现的次数
for(int i = L; i <=R; i++)
{
int tmp = i;
while (tmp)
{
//判断余数为2,计数加一
if(tmp % 10 == 2)
count++;
tmp /= 10;
}
}
return count;
}
int main() {
int L, R;
scanf("%d %d", &L, &R);
int sum = Number2OfOccurrences(L, R);
printf("%d", sum);
} #include <stdio.h>
int FindTwo(int x)
{
int ret = 0;
while(x > 0)
{
if(x % 10 == 2)
{
ret++;
}
x /= 10;
}
return ret;
}
int main()
{
int a = 0;
int b = 0;
int i = 0;
int count = 0;
scanf("%d%d", &a, &b);
for(i = a; i <= b; i++)
{
count += FindTwo(i);
}
printf("%d\n", count);
return 0;
} #include <stdio.h>
int checkTwo(int l, int r) {
int tmp = 0, cnt = 0;
for (int i = l; i <= r; i++) {
tmp = i;
while (tmp) {
// Check LSB(Least Significant Bit)
if (tmp % 10 == 2) {
cnt++;
}
// Check the next digit of tmp
tmp /= 10;
}
}
return cnt;
}
int main() {
int l, r;
while (scanf("%d %d", &l, &r) != EOF) {
printf("%d\n", checkTwo(l, r));
}
return 0;
}
#include<stdio.h>
int Find_2(int num) {
int count = 0;
while (num) {
if (2 == num % 10)
count++;
num /= 10;
}
return count;
}
int main() {
int L, R, ret = 0;
scanf("%d %d", &L, &R);
for (int i = L; i <= R; i++)
ret += Find_2(i);
printf("%d\n", ret);
return 0;
} #include <stdio.h>
int judge(int num){
int flag = 0;
while(num){
int res = num % 10;
if(res == 2)
flag ++;
num /= 10;
}
return flag;
}
int main(){
int l, r, count = 0;
scanf("%d %d", &l, &r);
for(int i = l; i <= r; i++)
count += judge(i);
printf("%d", count);
return 0;
} #include<stdio.h>
int sum(int num,int a)
{
int n=0;
for( n=0;num>0;num/=10)
{
if(num<10)
{
if(num==a)
n++;
}
else
{
if(num%10==a)
n++;
}
}
return n;
}
int main()
{
int left,right,n=0;
scanf("%d %d",&left,&right);
for(int i=left;i<right+1;i++)
n+=sum(i,2);
printf("%d\n",n);
return 0;
}
//基本跟前面几道题都是通用的,小改即可