请统计某个给定范围[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 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 <iostream> using namespace std; #include <iostream> using namespace std; int timesof2(int x) { int res = 0; while (x) { int e = x % 10; if(e == 2) res ++; x /= 10; } return res; } int main() { int l, r; cin >> l >> r; int cnt = 0; for(int i = l; i <= r; i ++ ) cnt += timesof2(i); cout << cnt << endl; }
#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 function(int x,int y); int function(int x,int y) { int c,i,num,n; int static count=0; for(num=x;num<=y;num++) { n=num; while(n>0) { i=n%10; if(i==2) { count++; } n/=10; } } return count; } int main() { int L,R; scanf("%d %d",&L,&R); int recive=function(L,R); printf("%d",recive); return 0; }
use std::io::{self, *}; fn main() { let stdin = io::stdin(); unsafe { for line in stdin.lock().lines() { let ll = line.unwrap(); let numbers: Vec<&str> = ll.split(" ").collect(); let a = numbers[0].trim().parse::<i32>().unwrap_or(0); let b = numbers[1].trim().parse::<i32>().unwrap_or(0); let c = count(a,b); print!("{}", c); } } } fn count(l: i32, r: i32) -> i32 { let mut c = 0; for num in l..=r { let s = num.to_string(); for char in s.chars() { if char == '2' { c += 1; } } } c }
#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 <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int count = 0; for (int i = a; i <= b; i++) { if (i % 10 == 2) count++; if (i / 10 % 10 == 2) count++; if (i / 100 % 10 == 2) count++; if (i /1000 % 10 == 2) count++; } cout << count << endl; }
#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; }