请统计某个给定范围[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 <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; }
#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; }
public class Program { public static void Main() { string[] inPut = System.Console.ReadLine().Split(" "); var(start, end) = (int.Parse(inPut[0]), int.Parse(inPut[1])); int count = 0; for (int i = start; i <= end; i++) { char[] array = i.ToString().ToCharArray(); for (int j = 0; j < array.Length; j++) if (array[j] == '2') count += 1; } System.Console.WriteLine(count); } }
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here while ((line = await readline())) { let arr = line.split(" ").map(Number); let l = arr[0]; let r = arr[1]; function main(a, b) { let count = 0; for (let i = a; i <= b; i++) { let x = i; while (x != 0) { let g = x % 10; x = parseInt(x/10) if(g==2){ count++ } } } return count } console.log(main(l,r)) } })();