1 solutions
-
0
C++ :
#include<cstdio> #include<iostream> #include<cstring> //#include<> using namespace std; int main() { int l,r,tot=0; cin>>l>>r; for(int i=l;i<=r;i++) { int x=i; while(x>0) { if(x%10==2) tot++; x=x/10; } } cout<<tot<<endl; return 0; }Pascal :
var l,r:1..10000; i,j,h,c:longint; s:string; begin {assign(input,'two.in'); assign(output,'two.out'); reset(input); rewrite(output);} readln(l,r); c:=0; for i:=l to r do begin str(i,s); h:=length(s); for j:=1 to h do if s[j]='2' then c:=c+1; end; writeln(c); { close(input); close(output);} end.Java :
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String s = sc.nextLine(); String[] ss = s.split(" "); if(ss.length!=2){ return; } int a = Integer.parseInt(ss[0]); int b = Integer.parseInt(ss[1]); int count1 = getCount(a),count2 = getCount(b); while(a!=0){ int t = a%10; if(t==2){ count1 --; } a = a/10; } System.out.println(count2-count1); } } public static int getCount(int n){ int count = 0; int k = 1; while(k<=n){ int after = n%k; int before = n/10/k; int cur = n/k%10; if(cur>2){ count = count+ (before+1)*k; }else if(cur<2){ count = count + before*k; }else if (cur==2){ count = count + before*k+after+1; } k*=10; } return count; } }
- 1
Information
- ID
- 20017
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By